javascript - Selenium Webdriver - 等待页面在 Java&JavaScript(ajax/jquery/animation 等)中完全加载

标签 javascript java jquery ajax selenium

我正在尝试构建一种更好的方法来等待每次点击后加载页面。 目前我使用的是这样的:

    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 命令计算表中的行数)时,它会显示零,而实际的视觉效果不是完全为零,count rows命令工作正常,如果你想检查它,这里是代码:

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 方法涵盖了所有内容,我希望您能给我一些我可能错过的补充说明。 提前致谢。

最佳答案

我认为您可以定义一个显式等待,等待特定条件发生,然后再继续代码。

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

关于javascript - Selenium Webdriver - 等待页面在 Java&JavaScript(ajax/jquery/animation 等)中完全加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38333904/

相关文章:

javascript - 推送到数组只是读取Javascript中的第二个索引

javascript - 实现 Backbone 名单应用的最佳方式

javascript - 丰富的选项卡和嵌入的内容

php - jQuery - 通过 PHP 保存到 SQL

jquery - 使用html表格行作为复选框的方法?

javascript - @符号在这里是什么意思

Javascript - 如何根据深度嵌套的数字对嵌套对象中的值进行排序?

java - jackson 用原始类型数组反序列化包装类

java - 规划实体与规划值(value)必须是一对一的关系吗?

java - JPA/EJB3 关系