Selenium Webdriver - 等待页面完全加载Java和JavaScript(ajax / jquery / animation等) [英] Selenium Webdriver - wait for page to completely load in Java&JavaScript(ajax/jquery/animation etc.)

查看:89
本文介绍了Selenium Webdriver - 等待页面完全加载Java和JavaScript(ajax / jquery / animation等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一种更好的方法,以便在每次单击后等待页面加载。
目前我使用的是:

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

在某些情况下,单击某个按钮并等待表之后加载,但是当我运行countRowsInTable方法(通过selenium命令计算表中的行)时它会显示零,而实际的视觉效果根本不为零,计数行的命令工作正常,如果你想要的话这里是代码检查它:

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

我很确定我用waitForJSandJQueryToLoad方法覆盖任何东西,我希望你可以给我一些额外的声明,也许我错过了。
提前谢谢。

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

这篇关于Selenium Webdriver - 等待页面完全加载Java和JavaScript(ajax / jquery / animation等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆