等待Ajax请求完成-Selenium WebDriver [英] Wait for ajax request to complete - selenium webdriver

查看:647
本文介绍了等待Ajax请求完成-Selenium WebDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在希望脚本等待ajax请求完成的位置.

I am stuck at point where i want my script to wait until ajax request is completed.

我尝试过: 1.在慢速网络模式下运行应用程序,单击创建"按钮并选中JQuery.active返回1.仅发生ajax调用.

I tried : 1. Running application in slow network mode, click create button and checked JQuery.active returns 1. There is only ajax call happening.

问题是整个页面没有刷新,只是页面上的某些数据每次都会更新.

Problem is whole page is not refreshed.Its just some data on page that is updated everytime.

我使用显式等待来等待加载程序/进度条消失,但是由于UI可能很快改变,我被要求专门等待ajax完成.

I used explicit wait to wait until loader/progress bar disappears but i am asked to specifically wait for ajax to complete as UI might change soon.

我尝试实施:

public boolean waitForJSandJQueryToLoad() {

    WebDriverWait wait = new WebDriverWait(driver, 30);

    // wait for jQuery to load
    ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
      @Override
      public Boolean apply(WebDriver driver) {
        try {
          return ((Long)((JavascriptExecutor)getDriver()).executeScript("return 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) {
        return ((JavascriptExecutor)getDriver()).executeScript("return document.readyState")
        .toString().equals("complete");
      }
    };

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

但是它不起作用.我的测试失败了,因为在ajax完成和数据更新之前调用了断言.

But its not working. My test are failing because assertions are called before ajax is completed and data is updated.

有人可以给我工作解决方案: 1.检查AJAX是否正在运行. 2.等待直到ajax完成. 3.等待数据更新/刷新,而不是整个页面.

Can someone give me working solution to : 1. Check AJAX is running. 2. Wait until ajax is completed. 3. Wait until data is updated/refreshed, not whole page.

谢谢!!

推荐答案

这是我用来等待Ajax完成的代码:

Here's the code I use to wait for Ajax to finish:

public static void waitForAjaxToFinish() {

    WebDriverWait wait = new WebDriverWait(driver, 5000);
    wait.until(new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver wdriver) {
            return ((JavascriptExecutor) driver).executeScript("return jQuery.active == 0").equals(true);
        }

    });
}

以及我用来等待JQuery处于活动状态的代码:

and the code I use to wait until JQuery is active:

public static void waitForJQueryToBeActive() {
    Boolean isJqueryUsed = (Boolean) ((JavascriptExecutor) driver)
            .executeScript("return (typeof(jQuery) != 'undefined')");
    if (isJqueryUsed) {
        while (true) {
            // JavaScript test to verify jQuery is active or not
            Boolean ajaxIsComplete = (Boolean) (((JavascriptExecutor) driver)
                    .executeScript("return jQuery.active == 0"));
            if (ajaxIsComplete)
                break;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }
}

希望这会有所帮助.

这篇关于等待Ajax请求完成-Selenium WebDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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