I am trying to run selenium test classes parallel using testNG like this
thread-count="3" parallel="classes"
. But the issue is at closing the chrome driver and everything else works fine. while execution some of the browsers getting closed even before executing the all the test cases throwing SessionNotFoundException.
code snippet as follows in BrowserUtil.java class:
public static WebDriver driver=null;
private static volatile ThreadLocal<WebDriver> tLocal = new ThreadLocal<WebDriver>();
public static synchronized void create() {
WebDriver driver1=new ChromeDriver();
tLocal.set(driver1);
driver = getDriver();
driver.manage().window().maximize();
}
public static WebDriver getDriver() {
WebDriver driv = tLocal.get();
return driv;
}
public synchronized static void destroy()
{
logger.debug("----Closing browser----");
driver.close();
}
Test class as follows:
@BeforeClass
public void navigateToPage() throws Exception {
BrowserUtil.create();
}
@AfterClass
public void close() {
BrowserUtil.destroy();
}
driver.quit();
instead ofdriver.close()
, unless you have a specific reason to just useclose()
:) – mrfreester May 10 '19 at 21:56