java - Java Selenium 测试 driver.close() 和 driver.quit() Firefox 问题
问题描述
我是 Selenium 测试的新手,我需要了解为什么我的解决方案不适用于 Firefox。我有一个 Base 类,它在 @Before 上建立驱动程序实例,并在 @After 上关闭并退出驱动程序。看起来像:
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();
}
}
正如您在方法调用 close() 和 quit() 方法后看到的那样。Chrome 驱动程序可以正常工作,但 Firefox 向我抛出异常 org.openqa.selenium.NoSuchSessionException 当我删除 close() 时它可以工作。我从 remove close() 中找到了一些解决方案来捕获异常,但我想知道最佳实践是什么。我会抓住异常。我对吗?喜欢
@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.");
}
}