0

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")
Automation Engr
  • 444
  • 2
  • 4
  • 26

3 Answers3

1

Probably as when a session is created it stores the the session-id as cookie in the browser.

Now to remove this we need to clear the cookies which are stored in the chrome webdriver. 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.

Dev
  • 197
  • 11
0

The scope of a fixture function indicates the number of times a fixture function is invoked. 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.


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. You may opt to drop 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.

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:

    @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()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

Are you using 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.

Håkan
  • 13
  • 6