简体   繁体   English

Python Selenium - 如何使用套件中的相同驱动程序实例清除用户登录 session

[英]Python Selenium - How to clear user logged-in session using the same driver instance within the suite

Scenario: I need to Test multiple user logins to an application in a single Test file场景:我需要在单个测试文件中测试多个用户登录到应用程序

Issue: When the second user login in the same test class is tried the Automation script fails as the previous users session is not wiped out问题:当第二个用户登录同一个测试 class 时,自动化脚本失败,因为以前的用户 session 没有被清除

Caveats:注意事项:

  1. The application does not have a logout feature yet/UI logout process has many complications应用程序还没有注销功能/UI 注销过程有很多复杂性
  2. I have put the webdriver Initialization in the conf test and reusing the driver instance in all of the tests when the test run is performed我已将 webdriver Initialization 放在 conf 测试中,并在执行测试运行时在所有测试中重用驱动程序实例

Below is the code structure:下面是代码结构:

Conftest file:测试文件:

@pytest.fixture(scope="session")
def driver_initializer(request):
    webdriver = Webdriver("chrome")
    session = request.node
    for item in session.items:
        classobj = item.getparent(pytest.Class)
        setattr(classobj.obj, "driver", webdriver)

Test Class which uses the driver instance from conftest测试使用来自 conftest 的驱动程序实例的 Class

@pytest.mark.usefixtures("driver_initializer")
class TestClass:
 def test_method(self):
   self.driver.get("url")

Probably as when a session is created it stores the the session-id as cookie in the browser.可能就像在创建 session 时一样,它将会话 ID 作为 cookie 存储在浏览器中。

Now to remove this we need to clear the cookies which are stored in the chrome webdriver.现在要删除它,我们需要清除存储在 chrome webdriver 中的 cookies。 To do this you can do something like this..要做到这一点,你可以做这样的事情..

def test_some_test_name(self):
  <test code>
  self.driver.delete_all_cookies()

and after this you can log in again with new user.之后,您可以使用新用户再次登录。

The scope of a fixture function indicates the number of times a fixture function is invoked.夹具 function 的 scope 表示夹具 function 被调用的次数。 A pytest fixture with scope as session is created only once for the entire Selenium test automation session. A pytest fixture with scope as session is created only once for the entire Selenium test automation session. Session scope is ideal for usage as WebDriver handles are available for the Selenium test automation session. Session scope 非常适合使用,因为 WebDriver 句柄可用于 Selenium 测试自动化 Z21D6F40CFB5119A9E5442。


In your usecase, as you intend to test multiple user logins to an application you can't share the session as the application does not have a logout feature yet.在您的用例中,当您打算测试多个用户登录到一个应用程序时,您不能共享session ,因为该应用程序还没有注销功能。 You may opt to drop scope="session"您可以选择放弃scope="session"

Additionally, once you complete the user login test, you can quit the session using self.driver.quit() and also force delete all the cookies.此外,完成用户登录测试后,您可以使用self.driver.quit()退出 session 并强制删除所有 cookies。

The resulten files can be:结果文件可以是:

  • Conftest file:测试文件:

     @pytest.fixture() def driver_initializer(request): webdriver = Webdriver("chrome") session = request.node for item in session.items: classobj = item.getparent(pytest.Class) setattr(classobj.obj, "driver", webdriver)
  • Test Class which uses the driver instance from conftest:测试使用来自 conftest 的驱动程序实例的 Class:

     @pytest.mark.usefixtures("driver_initializer") class TestClass: def test_method(self): self.driver.get("url") # perform your test self.driver.quit() self.driver.delete_all_cookies()

Are you using Django?您使用的是 Django 吗? Then you could try something like this:然后你可以尝试这样的事情:

from django.contrib.sessions.models import Session  
Session.objects.all().delete()

That would delete all the sessions from the database and hence log all users out.这将从数据库中删除所有会话,从而注销所有用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 selenium python 中打开已经登录的 chrome 浏览器 - How to open a already logged-in chrome browser in selenium python 如何在Firefox中使用python建立硒Webdriver套件的会话 - how to establish a session thourghout the selenium webdriver suite by using python in firefox 使用 selenium:在 whatsapp 中关闭 Python 驱动程序后如何保持登录状态 - Using selenium: How to keep logged in after closing Driver in Python in whatsapp 使用硒:如何在 Python 中关闭驱动程序后保持登录状态 - Using selenium: How to keep logged in after closing Driver in Python Python Selenium-使用在另一个文件中启动的相同驱动程序实例 - Python Selenium - Using same driver instance which is initiated in another file 我应该将登录的用户数据存储在 `g` 上还是在 `session` 上? - Should I store logged-in user data on `g` or on `session`? 如何使用此Python代码获取当前登录用户的名称? - How can one obtain the name of the currently logged-in user with this Python code? Python,获取当前登录用户的Windows特殊文件夹 - Python, get windows special folders for currently logged-in user Python 在不同对象中使用相同的 selenium 驱动程序实例 - Python use same selenium driver instance in different objects 使用createview和modelform在django中自动将登录用户设置为作者 - Automatically set logged-in user as the author in django using createview and modelform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM