1
votes

I have a specific situation. I have a web app tests written in java (selenium webdriver) that are organized and executed with testng in parallel. The process looks like this:

  • Every testng test (not test method) is run in parallel.
  • Every testng test contains one class and the class has more then one test method
  • Every testng test has the same listener (ItestListener) that starts the webdriver that is later shared between all tests methods in this particular test. This listener also closes the webdriver at the end of this test.

I am trying now to run this suite on Selenium Grid. Problem that I am facing is that after finishing the test webdriver instance stays in Selenium Grid active and blocks execution of next test until the timeout kicks in and kills the test. I am guessing that the problem is this handling of webdriver through the test listener and sharing one driver between test methods.

I am doing this simply because I want to save some time on opening and closing the webdriver before and after every test method.

My question is: Does anyone have an idea if this behavior is possible when using Selenium Grid and if yes, how this should be handled? So does anyone have this situation that he is using Grid and he has the test methods that are sharing webdriver and everything is working correctly? If so, I would like to hear how did you achieve it.

Thanks everyone in advance! Luka

Edit: TestListener code

public class WebDriverTestListener extends TestListenerAdapter {

@Override
public void onStart(ITestContext testContext) {
    super.onStart(testContext);
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    WebDriver driver = null;
    try {
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    testContext.setAttribute("webdriver", driver);      
}

@Override
public void onFinish(ITestContext testContext) {
    super.onFinish(testContext);
    WebDriver driver = (WebDriver) testContext.getAttribute("webdriver");
    driver.close();
}

}

1
Show your testlistener codeniharika_neo

1 Answers

0
votes

The problem was in the onFinish method. I used driver.close() which doesn't kill the webdriver instance, just closes the currently active window. Instead of driver.close(), should be used driver.quit(). After this change everything works correctly.