How to get selenium to wait for ajax response

1 min


647
2 comments, 1 share, 647 points
How to get selenium to wait for ajax response
How to use Selenium Grid

How to get selenium to wait for ajax response?

As we all like modern application BUT modern applications are not easy to test. When we are starting to write automation tests for application ajax based we ask ourself: How to get selenium to wait for ajax response?

So here one mistake can be made – to try to handle de situation by adding Thread.sleep(). You don’t want to do that –  because basically hardcode the time to wait and your tests will fail on the second try.

Ajax wait for visual indicator

While the UI visually indicates that AJAX activity is occurring  in this case you should be able to wait until such an indicator changes.

You can use:

new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(locator));
Java code

You can see more details about ExpectedConditions class here . You can clearly see in the code that they handle both executions there NoSuchElementException and StaleElementReferenceException.

Ajax wait without visual indicator

You need to wait for Javascript and jQuery to finish loading. You can execute Javascript to check if jQuery.active is 0 and document.readyState is complete, which means the JS and jQuery load is complete.

Java solution

public boolean waitForAjax() {

    WebDriverWait wait = new WebDriverWait(driver, 30);

    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) {
     
          return true;
        }
      }
    };


    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);
}
Java code

C# solution

public static void WaitForAjax(this IWebDriver driver, int timeoutSecs = 15, bool throwException=false)
{
  for (var i = 0; i < timeout; i++)
  {
    var ajaxIsComplete = (bool)(driver as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0");
    if (ajaxIsComplete) return;
    Thread.Sleep(1000);
  }
  if (throwException)
  {
    throw new Exception("WebDriver timed out");
  }
}
C# solution

Ruby  with capybara solution:


          def wait_for_ajax
            Capybara.default_max_wait_time = 45
            Timeout.timeout(Capybara.default_max_wait_time) do
              loop until finished_all_ajax_requests?
            end
          end

          def finished_all_ajax_requests?
            page.evaluate_script('jQuery.active').zero?
          end
Ruby solution

Like it? Share with your friends!

647
2 comments, 1 share, 647 points

What's Your Reaction?

cute cute
0
cute
scary scary
0
scary
wtf wtf
0
wtf
geek geek
4
geek
WIN WIN
0
WIN
Love Love
0
Love
OMG OMG
0
OMG
Angry Angry
0
Angry
Lol Lol
0
Lol
Test engineer

2 Comments

Your email address will not be published. Required fields are marked *

Choose A Format
Trivia quiz
Story