如果测试用例失败,Selenium Web驱动程序无法关闭firefox实例

use*_*204 4 java junit4 selenium-webdriver

我大家,我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)

Ard*_*sco 18

添加webdriver.quit()@AfterClass方法

示例使用硒化物4.5我必须添加(根据此版本)没有关闭或处置这种依赖性

__PRE__

  • 关闭将关闭当前活动窗口,如果是最后一个窗口则执行quit().但是,它确实需要有一个有效的活动会话才能执行此操作.如果你的测试失败,那个会话可能已经死了,那么当你调用一个关闭它时,它不知道在哪里发送命令而且什么都不做.如果没有活动会话,Quit将关闭所有客户端,因此如果您发送退出但没有活动会话,它将只是清理. (4认同)