I am attempting to run my selenium tests in parallel using TestNG as the runner. The parallel requirement is set in the testng.xml file like so.
<test name="smoke tests" parallel="methods" thread-count="2">
The problem I have is I want to "quit" the browser after each test. I then start a new browser and run the next test. This has worked well for me until I tried to run the tests in parallel. It seems that the methods share threads and if you quit the browser that thread is now gone. TestNG tries to run the next test on the thread that was quit and I get a SessionNotFoundException error.
I have tried parallel="tests" but that doesn't work and the tests are run sequentially instead of in parallel. Is there a way to run each test in a new thread and not reuse threads or am I out of luck?
Here is my @BeforeMethod
private static ThreadLocal<WebDriver> driverForThread;
@BeforeMethod
public static void setUpMethod() {
log.info("Calling setup before method");
driverForThread = new ThreadLocal<WebDriver>() {
@Override
protected WebDriver initialValue() {
WebDriver driver = loadWebDriver();
driver.manage().window().maximize();
return driver;
}
};
}
And my @AfterMethod
@AfterMethod
public static void afterMethod() {
getDriver().quit();
}
public static WebDriver getDriver() {
return driverForThread.get();
}
Here is a sample test
@Test
public void shouldBeAbleToGetSessionIDTest() {
login = new LoginPage(getDriver()).get();
home = login.loginWithoutTelephony(username, password);
String sessionID = home.getSessionID();
Assert.assertNotNull(sessionID);
log.info("The session text is: " + sessionID);
}