2

I am testing an GWT based app. But my test are failing left and right when the application response is slow but the selenium execution speed is fast.

I have written a webdriver wait method to handle an element which loads after an Ajax call but that does not seem to be working as each time I have to figure out which element is appearing after the Ajax call.

I need a generic Wait method which would wait till the Ajax call gets complete.

1
  • I appreciate your difficulty, but there isn't a generic Selenium API for waiting for an Ajax call to complete.
    – user246
    Commented Dec 29, 2013 at 20:03

5 Answers 5

2

I have successfully used something similar to:

new WebDriverWait(driver, 180)).until(new ExpectedCondition<Boolean>()
{
    public Boolean apply(WebDriver driver) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        return (Boolean)js.executeScript("return jQuery.active == 0");
}};
2

Let me recommend you using Selenide library. It's just ideal for handling Ajax in tests.

E.g. examples above could be rewritten in much more concise way with Selenide:

$("#foo option").shouldBe(visible);

$("#foo").selectOption("Bar");
1

There is no need to worry about AJAX calls and writing wait methods. Webdriver has capabilities to manage the situation comes along AJAX stuff. The keyword is implicit wait. Please take a look at the articles below.

1

I think you're approaching this in the wrong way. It's the AJAX call itself, but the fact that your page elements are not in their ready state by the time you interact with them, right? So let's solve that issue instead.

Let's say you want to select an item from a dropdown, but the dropdown is populated by AJAX and you don't know if it'll be ready when you get to it. If you centralize your test steps into a helper class that can be called within the tests, your test would just call SelectFromFooDropdown("Bar"). Inside the SelectFromFooDropdown method, you could then write something like:

new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#foo option")));
new Select(driver.findElement(By.id("foo"))).selectByVisibleText("Bar");

Therefore, by the time you select, you know for sure it's populated.

0

I just changed Erki M. code a bit, it worked perfectly with me:

public static void waitForAjax(WebDriver driver) {
    new WebDriverWait(driver, 180).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            JavascriptExecutor js = (JavascriptExecutor) driver;
            return (Boolean) js.executeScript("return jQuery.active == 0");
        }
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.