Pytest - issue running tests in parallel as browser windows share login state

Viewed 12

I'm trying to run tests in parallel using pytest-xdist (also tried pytest-parallel). The problem is, that the application being tested shares the login state between browser windows, which causes the tests to fail.

Steps:

  • I run pytest -m random_test -n 2 (on Linux)
  • The first test begins with a signin step in browser window 1
  • The second test begins the signin step (for a different user) in browser window 2 and overrides the signed in user in browser window 1
  • Test 1 fails, as the signed in user has been overridden with user from test 2

Question: Apart from Selenium Grid, is there any other way to prevent one browser window from sharing its login state with another?

What I tried so far:

  • pytest-xdist and pytest-parallel
  • --forked and --boxed with pytest-xdist
  • Changing scope of the selenium_setup fixture
  • Running tests that do not require login - these run fine
  • Open two incognito browser windows manually - the signin in one window shares its state with another incognito window

Any thoughts would be very welcome. Below is the simplified setup being used:

conftest.py:

@pytest.fixture(scope="session")
def selenium_setup(): 
    driver_options = DriverOptions()
    profile = driver_options.set_chrome_profile()
    selenium_util = SeleniumUtility(browser_name=SELENIUM_DRIVER, profile=profile, browser_options=options)
    driver = SeleniumBase(driver=selenium_util.driver)
    yield driver

selenium_base.py

def set_chrome_profile(self):
        cwd = os.getcwd()
        profile = webdriver.ChromeOptions()
        profile.add_argument("no-sandbox")
        profile.add_argument("--disable-extensions")
        profile.add_argument('--disable-notifications')
        return profile

class SeleniumBase(SeleniumUtility):

def __init__(self, driver):
    self.driver = driver
    self.driver.set_window_size(1920, 1080)
    self.driver.delete_all_cookies()

selenium_utils.py

def __init__(self)
if self.browser_name == 'Chrome':
   if not self.profile:
      self.profile = webdriver.ChromeOptions()
      self.profile.add_argument("no-sandbox")
      self.profile.add_argument("--disable-extensions")
      self.profile.add_argument('--disable-notifications')
      self.driver = webdriver.Chrome(options=self.profile, chrome_options=self.browser_options)

sample test

@pytest.mark.random_test
def test_something(selenium_setup):
    process = Process(selenium_setup)
    process.signIn_as_user1()
    assert process.verify_user1_signed_in()

@pytest.mark.random_test
def test_something_2(selenium_setup):
    process = Process(selenium_setup)
    process.signIn_as_user2()
    assert process.verify_user2_signed_in()
    
            
0 Answers
Related