0

I am in a situation where I need to test a website in Chrome where I have two different users logged in at once and then I want to switch between each instance and test different functions

Is there a way to open a second chrome instance which will be free of any cookies etc (so the user logged into first instance won't be still logged in) and then I can login as second user on the second instance and keep both instances with both users logged in?

Or would I have to resort to opening up a second browser ie Firefox and do it that way? Our suite is only currently configured to run with Chrome and ChromeDriver so I would have to update it so it could also open a firefox instance with geckordriver and test that.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
David
  • 21
  • 5
  • Using a separate Webdriver is the answer. Cookies (thereby sessions) are maintained/stored by the browser and bound to a URL. So if both windows point to the same URL they both have the same session. – Randy Casburn Dec 19 '20 at 00:20
  • yep... use 1 thread per driver per browser. – pcalkins Dec 19 '20 at 00:25

1 Answers1

0

Selenium driven ChromeDriver initiated Browsing Context is opened with a specific set of command line switches being scoped out from a newly scoped_dir. As an e.g. on :

C:\Users\username\AppData\Local\Temp\scoped_dir9640_113432031

So to keep the two user sessions seperate you can initiate two different Chrome based user sessions as follows:

// initiates the first Chrome session
WebDriver driver1 = new ChromeDriver();

// initiates the second Chrome session
WebDriver driver2 = new ChromeDriver();

When you intent to switch between these instances you have to bring back Selenium's focus on the required session using the following line of code:

// to focus on the first Chrome session
((JavascriptExecutor) driver1).executeScript("window.focus();");

// to focus on the second Chrome session
((JavascriptExecutor) driver2).executeScript("window.focus();");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352