How to wait until all ajax requests are complete in Selenium IDE? How to wait until all ajax requests are complete in Selenium IDE? selenium selenium

How to wait until all ajax requests are complete in Selenium IDE?


The answer turned out to be rather simple - all you have to do is check the number of active requests (selenium.browserbot.getUserWindow().$.active) with the waitForEval command, and set the value to 0 - and it all happens in the ide.


I haven't used IDE in a while. This is what I use for WebDriver. But the algorithms translate; JavaScript is JavaScript. That being said, it depends on your framework.

For Angular, I use this:

public boolean waitForAngularToLoad(WebDriver driver, int waitTimeInSeconds) {    WebDriverWait wait = new WebDriverWait(driver, waitTimeInSeconds, 2000L);    ExpectedCondition<Boolean> libraryLoad = new ExpectedCondition<Boolean>() {      public Boolean apply(WebDriver driver) {        try {          return ((Boolean)((JavascriptExecutor)driver).executeScript(                  "return angular.element(document.body).injector().get(\'$http\').pendingRequests.length == 0;"                  ));        }        catch (Exception e) {            // Angular not found            log.info("Not found: " + "return angular.element(document.body).injector().get(\'$http\').pendingRequests.length == 0;");            return true;        }      }    };    // wait for browser readystate complete; it is arguable if selenium does this all the time    ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {      public Boolean apply(WebDriver driver) {        return ((JavascriptExecutor)driver).executeScript("return document.readyState;")        .toString().equals("complete");      }  };  return wait.until(libraryLoad) && wait.until(jsLoad);}

For Prototype I use:

public boolean waitForPrototypeToLoad(WebDriver driver, int waitTimeInSeconds) {    WebDriverWait wait = new WebDriverWait(driver, waitTimeInSeconds, 2000L);    // wait for jQuery to load    ExpectedCondition<Boolean> libraryLoad = new ExpectedCondition<Boolean>() {      public Boolean apply(WebDriver driver) {        try {          return ((Boolean)((JavascriptExecutor)driver).executeScript("return Ajax.activeRequestCount == 0;"));        }        catch (Exception e) {            // Prototype  not found            log.info("Not found: " + "return Ajax.activeRequestCount == 0;");            return true;        }      }    };    // wait for browser readystate complete; it is arguable if selenium does this all the time    ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {      public Boolean apply(WebDriver driver) {        return ((JavascriptExecutor)driver).executeScript("return document.readyState;")        .toString().equals("complete");      }  };  return wait.until(libraryLoad) && wait.until(jsLoad);}

For jQuery, I use this (you have to customize the wait for spinner logic, everybody does it differently):

public boolean waitForJSandJQueryToLoad(WebDriver driver, long waitTimeInSeconds) {    WebDriverWait wait = new WebDriverWait(driver, waitTimeInSeconds, 2000L);    /*     * If you are curious about what follows see:     *  http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedCondition.html     *      * We are creating an anonymous class that inherits from ExpectedCondition and then implements interface     * method apply(...)     */    ExpectedCondition<Boolean> libraryLoad = new ExpectedCondition<Boolean>() {      public Boolean apply(WebDriver driver) {        boolean isAjaxFinished = false;        boolean isLoaderSpinning = false;        boolean isPageLoadComplete = false;        try {          isAjaxFinished = ((Boolean)((JavascriptExecutor)driver).executeScript("return jQuery.active == 0;"));        } catch (Exception e) {            // no Javascript library not found            isAjaxFinished = true;        }        try { // Check your page, not everyone uses class=spinner            // Reduce implicit wait time for spinner            driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);//              isLoaderSpinning = driver.findElement(By.className("spinner")).isDisplayed(); // This is the default            // Next was modified for GoComics            isLoaderSpinning = driver.findElement(By.cssSelector("#progress_throbber > ul > li:nth-child(1) > img[alt='spinner']")).isDisplayed();            if (isLoaderSpinning)                log.info("jquery loader is spinning");        } catch (Exception f) {            // no loading spinner found            isLoaderSpinning = false;        } finally { // Restore implicit wait time to default            driver.manage().timeouts().implicitlyWait(new DriverFactory().getImplicitWait(), TimeUnit.SECONDS);        }        isPageLoadComplete = ((JavascriptExecutor)driver).executeScript("return document.readyState;")                .toString().equals("complete");        if (!(isAjaxFinished & !(isLoaderSpinning) & isPageLoadComplete))            log.info(isAjaxFinished + ", " + !(isLoaderSpinning) +", " + isPageLoadComplete);        return isAjaxFinished & !(isLoaderSpinning) & isPageLoadComplete;      }    }; // Terminates statement started by ExpectedCondition<Boolean> libraryLoad = ...  return wait.until(libraryLoad); }


In the end, solving the problem arose addition of AJAX. I'm testing it for a few days - an ingenious solution. Appendix on that at every step made selenium automatically waits for the execution of AJAX.

https://addons.mozilla.org/pl/firefox/addon/sideex/