Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
I appreciate your difficulty, but there isn't a generic Selenium API for waiting for an Ajax call to complete. – user246 Dec 29 '13 at 20:03

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 == 0");
share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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("foo"))).selectByVisibleText("Bar");

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

share|improve this answer

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 == 0");
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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