Saturday 1 February 2014

Handling AJAX controls in WebDriver



          Web applications today are being designed in such a way that they appear the same as
desktop applications. Web developers are accomplishing this by using AJAX within their web
applications. AJAX stands for Asynchronous JavaScript and XML due to the fact that it relies
on JavaScript creating asynchronous calls and then returning XML with the data that the user
or application requires to carry on. AJAX does not rely on XML anymore, as more and more
people move over JSON, JavaScript Object Notation, which is more lightweight in the way
that it transfers the data. It does not rely on the extra overhead of opening and closing tags
that is needed to create valid XML.
  Most of the time WebDriver automatically handle the Ajax controls and calls/ Incase if it is not able to handle, you can follow the below way to handle

     Method 1 :
for (int second = 0;; second++) {
      if (second >= 60) fail("timeout");
      try { if (isElementPresent(By.<locatorType>("<locator Value"))) break; } catch (Exception e) {}
      Thread.sleep(1000);
    }

Example :
    

Method 2:

    //Waiting for Ajax Control
    WebElement AjaxElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By. <locatorType>("<locator Value>")));


Ex :

No comments:

Post a Comment