[英]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:注意事项:
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.