Pablo Pablo - 2 months ago 18
Groovy Question

SessionNotFoundException opening a new browser with Selenium + geb

I want to close and start a new browser/window in my test, so I tried:

browser.close();
driver.close();
driver = new ChromeDriver()


output:

org.openqa.selenium.remote.SessionNotFoundException at myTest.groovy:169


Linea 169 is:

driver.close();


Everything I put just after the browser.close() will output the same error.

I've also tried storing cookies and config:
Set allCookies = driver.manage().getCookies();

for(Cookie cookie : allCookies)
{
driver.manage().addCookie(cookie);
System.out.println(cookie);
}
browser.driver = browser.config.driver */


My GebConfig.groovy:

driver = {
System.setProperty('webdriver.chrome.driver', '.\\drivers\\ChromeDriver\\chromedriver.exe')
newDriver = new ChromeDriver()
newDriver.manage().window().maximize();

autoClearCookies = false
return newDriver;

}


All I need to do is close current window, open a new one and start with another test.

Answer

Note that browser.close() calls driver.close() so you are essentially calling driver.close() two times.

What you really need is to clear the driver cache (I'm assuming that you are using the cache as it is the default) by calling CachingDriverFactory.clearCacheAndQuitDriver() and then to create a new instance of Browser using browser = new Browser() which will create a new instance of Driver.

Comments