Handling-of-AJAX-Call-in-Selenium-Webdriver

Handling of AJAX Call in Selenium Webdriver

Table of Contents

What is Ajax?

Selenium supports Ajax, which stands for Asynchronous JavaScript and XML, so we can say that it is a combination of JavaScript and XML. It is a technique used for creating fast and dynamic web pages. It works with a group of technologies like JavaScript, HTTP requests, XML, HTML, CSS, etc. Ajax will send HTTP requests from the client to the server and receive data asynchronously without reloading the entire web page. 

How does Ajax works?

For example, when you click on a submit button, JavaScript will make a request to the server, interpret the result and current screen will get the update without reloading the webpage. The wait command may not work to handle the AJAX controls. It’s just because the actual page is not going to refresh. Sometimes it may load in a second and sometimes take much longer. We have no control overloading time. The best approach is to use dynamic waits i.e. WebDriver Wait in combination with ExpectedCondition to handle this kind of situation in selenium.

Handling Ajax call Using Selenium Webdriver

Selenium sometimes fails to get the element which got appear after the Ajax call. The reason is that in Ajax, you can never be sure about the exact wait time. Waiting for the allocated time for the element, we could not predict the Ajax call time so after waiting our test script fails. The biggest challenge in handling Ajax call is to know the webpage loading time. Since the loaded web page will last only for a few seconds, it is difficult for the tester to test such applications through an automation tool. For this, the Selenium Webdriver has to use the wait command on Ajax Call. So by wait command execution, selenium will suspend the current Test Case execution and wait for the expected value. When the expected value appears, the Selenium Webdriver will execute suspended test cases.

The Following are the wait methods that Selenium Webdriver uses.

  1. Thread.Sleep(): It is not a current choice to use as it suspends the current thread for the specified amount of time and makes the current thread to be moved from the queue which is running to the waiting queue.
  2. Implicit Wait(): This method tells the Webdriver to wait for some time if the element is not available immediately, but this will wait for the entire time until the browser is open. So any elements on the web page could take the time the implicit wait is set for.
  3. Explicit Wait(): It is used to freeze the test execution time till a particular condition is met or maximum time lapses.
  4. Webdriver Wait(): It can be used for any type of condition. It can be achieved with WebDriverWait in combination with ExpectedCondition.

The problem with all the above waits is that you have to mention the time out of the unit. What happens if the element is still not present within the time? So we have one more wait called Fluent Wait.

  1. Fluent Wait: This interface has its timeout and polling interval. Each Fluent Wait determines the maximum amount of time we need to wait for a condition, as well as to check the frequency of the condition and also repeat the waiting process as per the defined interval.

Handling of AJAX calls using Implicit wait in Selenium Webdriver.

An implicit wait will query the DOM for a specific period while locating one or more elements until they become available. The default value timeout is 0.

The implicit wait is available for the lifetime of the Webdriver instance once you define it.

System.setProperty("webdriver.gecko.driver","F:\\drivers\\geckodriver.exe");  
 
WebDriver driver = new FirefoxDriver();
browser.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
browser.get("//www.w3schools.com/");
WebElement ajaxControl = browser.findElement(By.id("DummyElement"));

Handling of AJAX calls using Explicit wait in Selenium Webdriver.

It is another built-in feature in Webdriver to handle AJAX calls. Whenever no other tricks work it will work just like Thread.sleep(). 

System.setProperty("webdriver.gecko.driver","F:\\drivers\\geckodriver.exe"); 
WebDriver driver = new FirefoxDriver();
browser.get("//www.w3schools.com/");
WebElement ajaxControl = (new WebDriverWait(browser, 15))
.until(ExpectedConditions.presenceOfElementLocated(By
.id("DummyElement")));

Check out more articles on our H2K Infosys blog.

Sample program of Implicit wait:

package SeleniumTester;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class AjaxDemoImplicit {
@Test
public void locatorDemo() throws Exception {
System.setProperty("webdriver.gecko.driver","F:\\drivers\\geckodriver.exe");  
     WebDriver driver = new FirefoxDriver();
//Implicit Wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.facebook.com");
driver.close();
driver.quit();
}
}

Sample program of Explicit wait:

package SeleniumTester;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class AjaxDemoExplicit {
@Test
public void locatorDemo() throws Exception {
System.setProperty("webdriver.gecko.driver","F:\\drivers\\geckodriver.exe");  
     WebDriver driver = new FirefoxDriver();
     //Implicit Wait
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     driver.get("https://www.facebook.com");

     //Explicit Wait Declaration
     WebDriverWait wait = new WebDriverWait(driver, 40);
     wait.until(ExpectedConditions.titleContains("Facebook – log in or sign up"));

     System.out.println("Explicit wait result- " +wait);
     driver.close();
     driver.quit();
     }

}

Conclusion:

  • AJAX allows us to retrieve small amounts of data from the Web page to the server without reloading the entire page.
  • To test the Ajax application, we need to apply different wait methods
    • ThreadSleep
    • Implicit Wait
    • Explicit Wait
    • WebdriverWait
    • Fluent Wait
  • For testing tools creating automation test request may be difficult as AJAX application frequently use different encoding or serialization technique to submit the data.
Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class