Eli*_*ahu 0 java selenium exception selenium-webdriver
我的main()
函数包含几个子函数,每个子函数测试几个特性/场景等.
在测试运行期间可能会发现几种错误.
在每种情况下都没有理由继续运行测试,因此我发送报告电子邮件并使用driver.close();
或driver.quit();
命令关闭程序.
浏览器已关闭,但代码仍然尝试运行,因此以下子函数中的第一个命令仍在尝试执行操作:导航到某处,查找对象等但由于浏览器已关闭,因此抛出异常如下:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Run Code Online (Sandbox Code Playgroud)
要么
Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.
Run Code Online (Sandbox Code Playgroud)
那么如何告诉我的程序停止运行并正确关闭/退出浏览器?
使用try/finally而不是try/catch.
您不想捕获异常然后关闭驱动程序,您只想在测试结束时关闭驱动程序,无论它在哪里.
示例测试方法可能如下所示:
public void exampleMethod() {
WebDriver driver = new FirefoxDriver();
try {
//Steps in your test
}
finally {
driver.quit();
}
}
Run Code Online (Sandbox Code Playgroud)