How to use Google Chrome Profiles for parallel JUnit tests with Selenium and Java

78 views Asked by At

I extended the Java framework using Gradle, Selenium, JUnit, and Java for Google Sheets Addons testing. The old approach allowed for running 8 tests in parallel, but with Profiles, it does not.

Path is: C:\\Users\\name\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Default\\

options.addArguments("--user-data-dir=" + configReader.getProfilePath());

It works well in a single test, but not with two or more Profile session (log in) break down with random time delays. If the previous test browser did not close, the next session is impossible:

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: session not created: Chrome failed to start: exited normally.
  (session not created: DevToolsActivePort file doesn't exist)
  (The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

I cannot choose the right place and way to use quit(); That closes the browser, but testers need to view the results in the browser.

Tried using dedicated ports, but no good results. It opens windows equal to the number of test cases that are running, but randomly puts scenarios, and the queue is not followed. Some windows remain empty.

        for (int i = 0; i < 8; i++) {
            int port = 9900 + i;
           options.addArguments("--remote-debugging-port=" + port);
        }

...
    public class WebDriverService {
    private WebDriver driver;
    private WebDriverWait wait;
    public static String PROFILE_EXIST_MESSAGE;

    WebDriverService() {
        ConfigReader configReader = ConfigReader.getInstance();
        System.setProperty("webdriver.chrome.driver", configReader.getChromedriverPath());
        ChromeOptions options = new ChromeOptions();
        if (configReader.isChromeOptionNoSandbox()) {
            options.addArguments("--no-sandbox");
        }

        options.addArguments("--window-size=1000,1000");
        options.addArguments("--user-data-dir=" + configReader.getProfilePath());

        if (configReader.isHeadlessChrome()) {
            options.addArguments("--headless", "--disable-gpu",
                    String.format("--user-agent=\"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36\"",
                            configReader.getHeadlessChromeVersion())
            );
        }

        driver = new ChromeDriver(options);
        wait = createWait();
...

    public void openLink(String link) {
        getDriver().get(link);
    }


    public void quit()
    {
        driver.quit();
    }
...
}```

0

There are 0 answers