我正在使用WebDriver运行测试,当测试失败时,浏览器不会关闭。在Windows机器上,这是一个很大的问题,因为我有几个Firefox仍然在后台运行。请建议
以下是代码:
public static WebDriver driver;
private String sTestCaseName;
@BeforeMethod
public void beforeMethod() throws Exception {
DOMConfigurator.configure("log4j.xml");
sTestCaseName = Constant.Login_Name;
Log.startTestCase(sTestCaseName);
new BaseClass(driver);
}
@Test(description = "Login", enabled = true)
public void TestLogin_Success() throws Exception {
try {
driver = new FirefoxDriver();
LoginBuilder.Execute(driver);
Log.info("Successfully Login!");
} catch (Exception e) {
Log.error(e.getMessage());
throw (e);
}
}
@Test(description = "Login_Failed", enabled = true)
public void TestLogin_Failed() throws Exception {
try {
driver = new FirefoxDriver();
LoginBuilder.Execute_Failed(driver);
Log.info("Unsuccessfully Login!");
} catch (Exception e) {
Log.error(e.getMessage());
throw (e);
}
}
@AfterMethod
public void afterMethod() {
Log.endTestCase(sTestCaseName);
driver.close();
}
答案 0 :(得分:0)
将finally块添加到try catch块。 关闭那里的WebDriver。
示例代码段
try {
....
....
} catch(Exception e) {
....
....
} finally {
....
driver.close();
}
有关Dispose,Close和Quit的更多信息 - Difference between webdriver.Dispose(), .Close() and .Quit()
答案 1 :(得分:0)
致电driver.quit()
而非driver.close()
@AfterMethod
public void afterMethod() {
Log.endTestCase(sTestCaseName);
driver.quit();
}
答案 2 :(得分:0)
为什么不尝试使用@AfterClass