0

I'm using Selenium ChromeDriver in c# and am trying to launch multiple browser runs in parallel.

I can launch them in parallel alright but noted that they are running on multiple tabs in one browser.. can I somehow specify (in ChromeOptions perhaps) to run each test in a newly spawned browser?

This is my code :

    [TestMethod]
    public void TestInParallel()
    {
        Parallel.ForEach(scenarios, (scenario) =>
        {
            using (var driver = new ChromeDriver(devOptions))
            {
                foreach (var action in scenario.Acties)
                {
                    driver.Navigate().GoToUrl(action.Url);
                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
                    }
                }
            }
        });
    }
  • 2
    Using multiple browser windows at the same time could give you unwanted results, for instance when using cookies, which are being shared among sessions. – Bouke Sep 7 '18 at 13:49
  • yes, I would like to run them in incognito modus for that. (devOptions.AddArgument("--incognito");) – AardVark71 Sep 7 '18 at 13:52
  • Even in incognito mode this is not possble, because sessions will also be shared in incognito modus. You can have a maximum of two concurrent sessions, one in regular mode and one in incognito mode. I seems possible with using different user profiles though, as explained here: stackoverflow.com/questions/13301965/… – Bouke Sep 7 '18 at 13:57
  • ouch....I couldn't believe it, but just spawned 4 incognito browsers and yes.... same sessionid is reused starting with the third browser.. – AardVark71 Sep 7 '18 at 14:18
  • Couldn't believe it either when I experienced it myself. – Bouke Sep 7 '18 at 14:20
1

I'm not sure how you tested your sessions issue but having two webdrivers in parallel is a normal case and there should not be issues with cookies or whatever. Below is my test written in Java but you can easily implement it in C#:

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", "E:/Dev/WebDrivers/chromedriver.exe");
    WebDriver driver1 = new ChromeDriver();
    WebDriver driver2 = new ChromeDriver();
    driver1.get("https://www.amazon.com/ref=nav_logo");
    driver2.get("https://www.amazon.com/ref=nav_logo");
    System.out.println("Driver1 cookies: " + driver1.manage().getCookies());
    System.out.println("Driver2 cookies: " + driver2.manage().getCookies());
    driver1.quit();
    driver2.quit();
}

So here you create two instances of WebDriver and each one will be operating with its own cookies set. I just checked that with amazon website since it uses sessions to track what you're doing on the site even if you're not logged in. Below is the output:

enter image description here

With this approach each webdriver opens their own window (not a tab).

  • The behavior I saw was visually.. I'm not running headless so I just see this happening. I'll do a test and print out the cookies – AardVark71 Sep 7 '18 at 15:08
  • You probably used the same webdriver instance. The idea is to instantiate two webdriver objects in memory. My example works for a regular chrome run. Not headless one. – Alexey R. Sep 7 '18 at 15:13
  • I'm using a "using" statement.. scope of each webdriver instance should be isolated. – AardVark71 Sep 7 '18 at 15:30
  • In ChromeOptions I used each time the same remote-debugging-port. Wonder if that matters – AardVark71 Sep 7 '18 at 15:31
0

I've asked myself an year ago the same question that you ask here.

What I came up is using soucelab example of using ThreadLocal for storing the webdriver objects and using Selenium-Grid to consume the webdriver requests.

I've done some load/stress test with that configuration to check how stable is selenium grid and the webdriver using that ThreadLocal approach.I've modified a little bit their samples to connect to my local selenium grid. And the result was that in testing 2 sites with 1000 iterations I get 4 failed test and 3 of them was that the site was not reachable (maybe they banned me for a certain moment of time) and 1 stale element reference which I don't know were it came from (my laptop was not in the best shape while doing that). The first failing test's was after 500-th iteration. So I can say that was pretty stable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.