I am new in Selenium testing and I need to understand why my solution don't work with Firefox. I have a class Base which establishes the driver instance at @Before and close and quit driver on @After. It looks like:
package tests;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import services.WebDriverService;
import java.io.IOException;
public class Base
{
protected WebDriver driver;
protected String baseUrl = "http://seleniumweb2";
@Before
public void before() throws IOException {
this.driver = (new WebDriverService()).getDriver();
}
@After // It can not be AfterClass because of closing browsers on remote server
public void after()
{
driver.close();
driver.quit();
}
}
As you can see after method calls close() and quit() methods. Chrome driver works without any problems but Firefox throws me an exception org.openqa.selenium.NoSuchSessionException As I remove close() it works. I found a few solutions from remove close() to catch Exception but I would like to know what is the best practice. I would catch the exception. Am I right? Like
@After // It can not be AfterClass because of closing browsers on remote server
public void after()
{
try {
driver.close();
driver.quit(); // Is necessary to stop server node session!!!
} catch (Exception e) {
System.out.println("driver.quit() throws exception while closing browser window.");
}
}