0

For my test using Selenium and Python, I need to open 2 sessions of the browser with 2 users and check updates are made in the other window when changes are made in window 1. I am able to open a 2nd session with a different user by opening it as chrome incognito mode but am not sure how to switch back and forth between the 2 open browser sessions.

I have tried the following extract as an example, but as the 2nd window is a new window using driver.get again the [-1] does not appear to take the focus back to window_before:

window_after = driver.window_handles[0]
print('windows after', window_after)
time.sleep(2)
driver.switch_to.window(driver.window_handles[-1])
Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
  • Please share the whole code in which you are opening 2 sessions – Chanda Korat Apr 13 '18 at 11:52
  • Possible duplicate of [How to switch between different chrome browser window opened by different WebDriver using selenium in Python?](https://stackoverflow.com/questions/49216418/how-to-switch-between-different-chrome-browser-window-opened-by-different-webdri) – undetected Selenium Apr 13 '18 at 14:24

1 Answers1

1

You should instantiate a second driver for the second user session:

driver1 = webdriver.Chrome()
driver2 = webdriver.Chrome()

All of the windows for a particular driver instance share the same cookies; therefore, you can’t open a new window and expect to create a second user session. The second window will “see” the same cookies as the first, causing them to be sent to your website, wherein your app will see the visitor as already logged in under the first user session.

Creating a second driver instance gives you a second, isolated pool of cookies (as well as local and session storage), allowing you to safely and reliably create a second user session.

While you may have found a way to open an incognito/private window within the same driver instance, this is not supported by the WebDriver specification (on which Selenium is built) and thus is unlikely to be portable across browsers and platforms.


capybara-py makes this easy—transparently instantiating a new driver instance as needed:

import capybara

def test_concurrent_editors(page):
    page.visit("/")
    page.click_link("Sign In")
    page.fill_in("Username", value="alice@example.com")
    page.fill_in("Password", value="s33krit")
    page.click_button("Sign In")

    # ... do things with the default session

    with capybara.using_session("other user"):
        page.visit("/")
        page.click_link("Sign In")
        page.fill_in("Username", value="bob@example.com")
        page.fill_in("Password", value="p@ssw0rd")
        page.click_button("Sign In")

        # ... do things with the "other user" session

    # .... do more things with the default session

    with capybara.using_session("other user"):
        # ... do more things with the "other user" session

(Note: It comes with a pytest plugin that exposes the page fixture and resets all sessions in between each test.)

Ian Lesperance
  • 4,961
  • 1
  • 26
  • 28
  • I am setting up the driver in the fixture so that driver can be passed in as a parameter do you know a neat way of passing 2 instances of the driver for 1 test – Rizwen Butt Apr 13 '18 at 15:29
  • @pytest.fixture() def driver(): return webdriver.Chrome('C:/Users/.... – Rizwen Butt Apr 13 '18 at 15:34
  • You can define a second fixture for that, specifying both as arguments to the test functions that require it. But I also included some information on capybara-py, a library that makes it so you don't even have to think about that. – Ian Lesperance Apr 13 '18 at 17:11