Selenium - Script should wait till browser is closed manually

依然范特西╮ 提交于 2019-12-08 19:47:28

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);
        }
    }
murali selenium

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

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.

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