简体   繁体   中英

Selenium Webdriver - wait for page to completely load in Java&JavaScript(ajax/jquery/animation etc.)

I am trying to build a better method for waiting for a page to load after each click. At the moment what i have used is this:

    public boolean waitForJSandJQueryToLoad() {
    WebDriverWait wait = new WebDriverWait(webDriver, EnvironmentUtils.getPageLoadTimeout().intValue());
    // wait for jQuery to load
    ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            try {
                if (!((Boolean) callJS("return (window.jQuery != null) && (jQuery.active === 0);"))) {
                    log.info("JQUERY.ACTIVE IS WORKING AT THE MOMENT! jQuery.active= " + callJS("return jQuery.active"));
                }
                return (Boolean) callJS("return (window.jQuery != null) && (jQuery.active === 0);");
            } catch (Exception e) {
                // no jQuery present
                return true;
            }
        }
    };
    // wait for Javascript to load
    ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            if (!callJS("return document.readyState").toString().equals("complete")) {
                log.info("document.readyState is not complete at the moment! status is->" + callJS("return document.readyState").toString());
            }
            return callJS("return document.readyState").toString().equals("complete");
        }
    };

    ExpectedCondition<Boolean> animationLoad = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            if (!callJS("return $(\":animated\").length").toString().equals("0")) {
                log.info("Animation is currently executing on-page. value ->" + callJS("return $(\":animated\").length").toString());
            }
            return callJS("return $(\":animated\").length").toString().equals("0");
        }
    };

    return wait.until(jQueryLoad) && wait.until(jsLoad) && wait.until(animationLoad);
}

in some cases my tests still fails after clicking on some button and waiting for a table to load afterwards but when I ran countRowsInTable method(which count rows in a table by selenium command) it brings out zero while the actual visual is not zero at all, the command of count rows is working properly, here's the code if you want to check it:

public int countDisplayedRowsInTable(String tableId) {
    waitForJSandJQueryToLoad();
    int count = 0;
    List<WebElement> rows = webDriver.findElements(By.xpath("//table[@id='" + tableId + "']/tbody/tr"));
    for (WebElement row : rows) {
        if (row.isDisplayed())
            count++;
    }
    return count;
}

I am pretty sure that i covered anything with my waitForJSandJQueryToLoad method, i hope that you may give me additional statement that maybe i missed. Thanks in advance.

I think you can define an explicit wait that waits for a certain condition to occur before moving on in the code.

    public int countDisplayedRowsInTable(String tableId) {
        String e = "//table[@id='" + tableId + "']";

        // Wait for the table to load
        WebElement table = (new WebDriverWait(driver, intCustomWait))
          .until(ExpectedConditions.elementToBeClickable(By.xpath(e));

        // Get all the rows from within the table
        List<WebElement> rows = table.findElements(By.xpath("./tbody/tr"));
        if(rows.size() > 0)
        {
            int count = 0;
            for (WebElement row : rows) {
                if (row.isDisplayed())
                count++;
            }
            return count;
        }
        else return -1;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM