java 如果测试用例失败,Selenium Web 驱动程序将无法关闭 Firefox 实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14562363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium web driver not able to close firefox instance if a Test cases is failed
提问by user1863204
i folks, i am using junit with selenium web driver 2.28. the problem is if i run a successful test case the web drives is able to close the firefox instance, but when a test case fails the selenium web driver is not able to close the firefox. i am using FF 15.0.1 with selenium-server-standalone-2.28.0.jar. please respond thanks Sahil
伙计们,我正在使用 junit 和 selenium web 驱动程序 2.28。问题是,如果我运行一个成功的测试用例,Web 驱动器能够关闭 firefox 实例,但是当测试用例失败时,selenium Web 驱动程序无法关闭 firefox。我正在使用 FF 15.0.1 和 selenium-server-standalone-2.28.0.jar。请回复谢谢 Sahil
private void startWebdriver() throws UIException{
//2) Prevent re-use.
if(UIHandlerWD.this.profile == null)
throw new
UIException(
UIException.Code.UI,
"Webdriver instance cannot be instantiated."
);
//3) Configure Selenium Webdriver.
if (this.profile.browserType.equalsIgnoreCase("*firefox")){
FirefoxProfile fProfile = new FirefoxProfile();
// profile.SetPreference("network.http.phishy-userpass-length", 255);
fProfile.setAcceptUntrustedCertificates(true);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setJavascriptEnabled(true);
dc.setCapability(FirefoxDriver.PROFILE, fProfile);
//this.webdriver = new FirefoxDriver(dc);
this.webdriver = new FirefoxDriver(dc);
}
else if (this.profile.browserType=="INTERNETEXPLORER")
this.webdriver = new InternetExplorerDriver();
else
throw new
UIException(
UIException.Code.UI,
"Unknown browser type '" + this.profile.browserType +"'."
);
//4) Start Webdriver.
this.webdriver.get(this.profile.getURL().toString());
this.webdriver.manage().timeouts().
implicitlyWait(5, TimeUnit.SECONDS);
this.webdriver.manage().timeouts().
pageLoadTimeout(this.profile.timeout, TimeUnit.SECONDS);
}
void stopWebdriver() {
if(this.webdriver != null){
try{
Thread.sleep(5000);
}
catch (Exception e) {
// TODO: handle exception
}
this.webdriver.close();
}
this.webdriver = null;
this.profile = null;
}
回答by Ardesco
Add webdriver.quit()
to an @AfterClass
method.
添加webdriver.quit()
到@AfterClass
方法中。
close() will shut the current active window. If the current active window is the last window it is functionally equivalent to performing a quit().
close() 将关闭当前活动窗口。如果当前活动窗口是最后一个窗口,它在功能上等同于执行quit()。
It does however need to have a valid active session to be able to do this. If your test has failed that session is probably dead, so when you call a close() it doesn't know where to send the command and throws an Exception.
然而,它确实需要有一个有效的活动会话才能做到这一点。如果您的测试失败,则该会话可能已死,因此当您调用 close() 时,它不知道将命令发送到何处并抛出异常。
quit() will end all sessions and shut down all clients, it's basically a clean up everything command. It will also not throw any Exceptions if all clients/sessions have already been closed/ended.
quit() 将结束所有会话并关闭所有客户端,它基本上是一个清理所有命令。如果所有客户端/会话都已经关闭/结束,它也不会抛出任何异常。