Wednesday, 29 December 2021

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

Caveats:

  1. The application does not have a logout feature yet/UI logout process has many complications
  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

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

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


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

No comments:

Post a Comment