在调用driver.quit之后,Webdriver没有关闭

时间:2016-06-02 08:27:49

标签: java selenium automation

我正在使用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();
    }

3 个答案:

答案 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