Selenium - Script should wait till browser is closed manually

半城伤御伤魂 提交于 2019-12-10 11:46:33

问题


I am using Java for selenium automation I have a scenario where I need to should move to next scenario in cucumber feature file only when I close the chrome browser manually for the current scenario.

Please help me to implement this


回答1:


You can also do like this

    WebDriver driver = new ChromeDriver();

    waitForDriverToClose(driver);

waitForDriverToClose

private static void waitForDriverToClose(WebDriver driver) {
        try {
            WebDriverWait wait = new WebDriverWait(driver, 20);
            wait.until(ExpectedConditions.not(new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver driver) {
                    try {
                        driver.getTitle();
                        return true;
                    } catch (Exception ex) {
                        System.out.println("Couldn't Connect Driver / Driver Closed");
                        return false;
                    }
                }
            }));
        } catch (org.openqa.selenium.TimeoutException ex) {
            System.out.println("Timeout Trying Again");
            waitForDriverToClose(driver);
        }
    }



回答2:


You can try something like looping until you got browser died or not reachable exception..

 WebDriver driver = new FirefoxDriver();

    driver.get("http://www.google.com");

    Boolean check = false;

    while (!check) {

        try {
            driver.getTitle();
            Thread.sleep(200);
        } catch (Exception e) {
            //you can verify correct exception here ie not reachable, dead etc..
            check = true;
        }

    }

    System.out.println("after browser close");
    //continue your code here



回答3:


If you manually close the browser while your selenium tests are running you will get: org.openqa.selenium.NoSuchWindowException: Window not found. The browser window may have been closed.

You could try and catch this exception and then go from there.



来源:https://stackoverflow.com/questions/38093559/selenium-script-should-wait-till-browser-is-closed-manually

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