masterwok
masterwok

Reputation: 5131

Selenium WebDriver in Parallel - Closing WebDriver instance interrupts other tests

I am having trouble getting RemoteWebDriver to close sessions. The problem occurs when running tests in parallel and one of the instances of RemoteWebDriver calls the close method.

For example:

  1. Test A starts and connects to the selenium server.
  2. Test B starts and connects to the selenium server.
  3. Test A call close method.
  4. Test B throws an exception saying that the connection may have died.

If I comment out the call to close() then both tests run to completion successfully however both browser windows stay open after.

I am currently running the latest version of Firefox and 2.29.0 of seleium-server. Selenium is being used to scrape customer invoices off sites and is running on a JBoss server.

The code that creates the connection is the following:

public WebDriver getRemoteWebDriver() {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    WebDriver driver = null;
    URL url = null;

    // Connect to the selenium server
    try {
        url = new URL("http://127.0.0.1:4441/wd/hub");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    capabilities.setBrowserName("firefox");
    capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
    capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);

    driver = new RemoteWebDriver(url, capabilities);
    driver = new Augmenter().augment(driver);           // Used to take screens
    driver.manage().timeouts().implicitlyWait(SELENIUM_IMPLICIT_WAIT_SECONDS, TimeUnit.SECONDS);    // Set implicit wait time
    driver.manage().deleteAllCookies();                                                                             // Make sure we have clean session

    return driver;
}

My question is: How do I properly create and close instances of RemoteWebDriver in parallel on one machine? Any help is much appreciated. Thank you.

Upvotes: 0

Views: 1317

Answers (1)

Kurt Du Bois
Kurt Du Bois

Reputation: 7655

If you're running this test from a unit test, you can define a beforeclass and afterclass-hook. This way your selenium driver can start up before running all tests and shut down afterwards.

Upvotes: 1

Related Questions