Selenium web driver not able to close firefox instance if a Test cases is failed

谁说我不能喝 提交于 2019-12-10 07:47:07

问题


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

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;
}

回答1:


Add webdriver.quit() to an @AfterClass method.

close() will shut the current active window. If the current active window is the last window it is functionally equivalent to performing a 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.

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.



来源:https://stackoverflow.com/questions/14562363/selenium-web-driver-not-able-to-close-firefox-instance-if-a-test-cases-is-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!