TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Handle Ajax Calls using Selenium Webdriver
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Selenium Tutorial

Handle Ajax Calls using Selenium Webdriver

Last updated: Jun 01, 2024 3:54 pm
By Meenakshi Agarwal
Share
12 Min Read
Handle Ajax Calls using Selenium Webdriver
Handle Ajax Calls using Selenium Webdriver
SHARE

In this Selenium tutorial, we’ll share some of the best techniques to handle AJAX calls. You can apply them to your existing or new Selenium Webdriver projects. If your code manages AJAX elements correctly, then it won’t throw false-positive failures due to operation timed-out.

Contents
What is AJAX and how does it work?How to Handle AJAX Calls in Selenium?Using <Thread.sleep(time in ms)>Using JavaScript to handle AJAX callsOther Ways to Handle Ajax CallsUse Implicit Wait to Handle AJAX CallsUse Explicit Wait to Handling AJAXFluent Wait to Handle AJAX CallsUsing WebdriverWaitSample Code for Handling AJAX CallsAJAX Call Test Case ScenarioDemo Site UsedTest Case Description

Introduction to Ajax

AJAX is an advanced communication technique used by Web applications to exchange data from the server without affecting the state of the currently opened web page. It saves the reloading of the page on the client side and cuts down the number of requests on the server making it respond faster.

Because of these apparent advantages, many websites use AJAX calls to improve their user experience. However, using the AJAX controls poses a challenge for the Selenium Webdriver as it gets only a fraction of a second to act on the changes made by the AJAX calls.

What is AJAX and how does it work?

AJAX stands for “Asynchronous JavaScript and XML.” It is a popular term in the context of dynamic web applications. Many people confuse it with a programming language. But it’s a technology that works with the combination of JS, CSS, and, XML. It enables a web page to request a limited amount of information from the server without the need to refresh the whole page.

You can consider an example when a user submits a form, the JavaScript dispatches the request to the server, processes the response, and updates a part of the screen without refreshing the browser.

Handle Ajax Calls Using Selenium Webdriver,
How Ajax Call Communicates with the Web Server.

Some of the points which can be helpful in the testing of AJAX applications are as follows.

  • When a browser makes an AJAX call, it may not result in page navigation. So you need to be careful what part of the page is getting changed by the server response.
  • AJAX calls are asynchronous and don’t block the execution. So, the user can still use the application until the server processes his request. For a tester, it would mean that he can’t estimate the actual time the server would take to deliver the response.

How to Handle AJAX Calls in Selenium?

You would now have an idea of the hurdles that a tester could face while automating a web page that makes AJAX calls. The main issue is how to handle the AJAX call when you are not sure of the loading time of the webpage. Sometimes the change would be so sudden that it would disappear in a flash. In this situation, you’ve to devise a strategy that should be dynamic and perceptive.

So, let’s discuss the options that we can deploy to handle AJAX calls in Selenium Webdriver.

Using <Thread.sleep(time in ms)>

<Thread.sleep()> is an obvious choice for handling AJAX calls. But it may not give you the best result. Instead, a test could break intermittently if the server response time exceeds the time specified in sleep. Additionally, the test has to wait for the given time even in a situation of a timely response. Though keeping all the odds aside, this method does work, and we’ve tested it as working.

Using JavaScript to handle AJAX calls

This method is only useful if the web application has jQuery in use to handle AJAX calls. Since jQuery uses a mechanism that keeps the no. of active AJAX calls in check, we can utilize this information to find out their final status.

Here is a sample code to showcase the handling of AJAX controls using Selenium Webdriver. You can integrate it into your test execution class.

	public void waitForAjaxControls(int timeoutInSeconds) {
		System.out
				.println("Querying active AJAX controls by calling jquery.active");
		try {
			if (browser instanceof JavascriptExecutor) {
				JavascriptExecutor jsDriver = (JavascriptExecutor) browser;
				for (int i = 0; i < timeoutInSeconds; i++) {
					Object numberOfAjaxConnections = jsDriver
							.executeScript("return jQuery.active");
					// return should be a number
					if (numberOfAjaxConnections instanceof Long) {
						Long n = (Long) numberOfAjaxConnections;
						System.out
								.println("Number of active jquery AJAX controls: "
										+ n);
						if (n.longValue() == 0L)
							break;
					}
					Thread.sleep(1000);
				}
			} else {
				System.out.println("Web driver: " + browser
						+ " can't run javascript.");
			}
		} catch (InterruptedException e) {
			System.out.println(e);
		}
	}

Other Ways to Handle Ajax Calls

Here are some more ways to achieve this.

Use Implicit Wait to Handle AJAX Calls

An implicit wait is a Webdriver mechanism to query the DOM for a specified time duration while locating one or more elements until they become available. The default timeout value is 0.

Once you define it, the implicit wait is available for the lifetime of the Webdriver instance.

WebDriver browser = new FirefoxDriver();
browser.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
browser.get("https://techbeamers.com");
WebElement ajaxControl = browser.findElement(By.id("DummyElement"));

Use Explicit Wait to Handling AJAX

It is yet another Webdriver’s built-in feature to handle AJAX calls. Just like the <Thread.sleep()>, you can get it working when no other tricks other work.

WebDriver browser = new FirefoxDriver();
browser.get("https://techbeamers.com");
WebElement ajaxControl = (new WebDriverWait(browser, 15))
		.until(ExpectedConditions.presenceOfElementLocated(By
				.id("DummyElement")));

Fluent Wait to Handle AJAX Calls

It’s an implementation of the Webdriver’s Wait interface which brings both the timeout and polling interval to use. The Fluent wait makes use of the timeout to wait for a condition, as well as the frequency for the no. of attempts.

For example, you can see in the last section where we’ve demonstrated the combined use of <FluentWait> and the <WebdriverWait>.

Using WebdriverWait

It is one of the best Webdriver strategies to handle the AJAX controls. It allows you to specify a condition to check at regular intervals and break to the next step as soon as it gets timed out.

Apart from the <WebdriverWait>, we also use the <ExpectedCondition> to get the entire mechanism in place. We’ve covered the detailed example in the next section.

Sample Code for Handling AJAX Calls

So far, you’ve seen six strategies to work with the AJAX controls using Selenium Webdriver. In most of these methods, we’ve used a variety of waits to handle the AJAX calls.

  • Thread.sleep()
  • Implicit Wait
  • Explicit Wait
  • Fluent Wait
  • WebdriverWait

We’ll now give you a fully working demo of using the <FluentWait> and <WebdriverWait> for handling the AJAX controls.

We’ve used w3schools.com’s demo website to test the handling of AJAX calls in the below Selenium code.

AJAX Call Test Case Scenario

Demo Site Used

We’ve used the following demo URL for our testing which is using the AJAX calls.

  • [https://www.w3schools.com/xml/tryit.asp?filename=tryajax_callback]

Test Case Description

  1. Open the demo AJAX application demo website.
  2. Following AJAX controls would appear in an IFRAME.
    1. A demo paragraph element that contains some default text.
    2. A simple button control to make the AJAX calls.
  3. When you click the button control, the AJAX call takes place.
    1. The default text disappears from the screen.
    2. Two new paragraphs are displayed on the screen.
  4. You now need to validate the two conditions.
    1. The new text in the first paragraph shouldn’t match the default text.
    2. The text in the second paragraph should match the expected value.

Note: We’ll read the default text from the demo paragraph as it would appear first on the screen. For the second paragraph, we’ve hardwired the value in the sample code which we copied from the demo test site.

Now, you’ll see the sample source code below. It should be self-explanatory as we’ve added comments for each step.

Please set the above Demo URL in the <AJAX_TEST_URL> placeholder given before executing the code.

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AjaxTesting {

	private String ajaxTestSite = "<AJAX_TEST_URL>";

	WebDriver browser;
	WebDriverWait wait;

	@BeforeTest
	public void startTest() {

		// Launch the demo web page to handle AJAX calls using Webdriver.
		browser = new FirefoxDriver();
		browser.manage().window().maximize();
		browser.navigate().to(ajaxTestSite);
	}

	@Test
	public void test_AjaxCalls() {

		/* Wait for the AJAX controls to appear. */
		browser.switchTo().frame(browser.findElement(By.id("iframeResult")));
		By container = By.xpath(".//*[@id='demo']");
		wait = new WebDriverWait(browser, 5);
		wait.until(ExpectedConditions.presenceOfElementLocated(container));

		/* Read the text that appears in the AJAX text control. */
		WebElement ajaxControl = browser.findElement(container);
		String ajaxTextFirstPara = ajaxControl.getText().trim();

		/* Click on the AJAX button control. */
		browser.findElement(By.xpath("html/body/button")).click();

		/* Wait for the new content to appear in the AJAX text control. */
		By newAjaxcontrol = By.xpath(".//*[@id='demo']/p");
		Wait<WebDriver> newwait = new FluentWait<>(browser)
				.withTimeout(60, TimeUnit.SECONDS)
				.pollingEvery(5, TimeUnit.SECONDS)
				.ignoring(NoSuchElementException.class);
		newwait.until(ExpectedConditions
				.presenceOfElementLocated(newAjaxcontrol));

		/*
		 * Wait for the second paragraph value to get visible in the AJAX text
		 * control.
		 */
		WebElement secondParagraph = browser.findElement(By
				.xpath(".//*[@id='demo']/p[2]"));
		wait.until(ExpectedConditions.visibilityOf(secondParagraph));

		/* Get the text from the first paragraph after the AJAX call. */
		String ajaxNewTextFirstPara = browser
				.findElement(By.xpath(".//*[@id='demo']/p[1]")).getText()
				.trim();

		/* Get the text from the second paragraph after the AJAX call. */
		String ajaxTextSecondPara = secondParagraph.getText().trim();
		String expectedTextInSecondPara = "AJAX is a technique for creating fast and dynamic web pages.";

		/* Verify the first paragraph text shouldn't match the new text. */
		Assert.assertNotEquals(ajaxTextFirstPara, ajaxNewTextFirstPara);

		/* Verify the second paragraph text must match with the new text. */
		Assert.assertEquals(ajaxTextSecondPara, expectedTextInSecondPara);
	}

	@AfterTest
	public void endTest() {
		browser.quit();
	}
}

If you wish to test the sample program then, add the above Java code to a TestNG project in Eclipse. Whenever you run this program, it’ll execute successfully and produce the following report.

Test Report - Handle AJAX Calls Using Selenium Webdriver
Test Summary – Handle AJAX Calls Using Selenium Webdriver.

If you are reading this post and would like to appreciate our work or ask questions then, you are most welcome to share. If you know an alternative technique for handling the AJAX calls then, we would love to hear it from you.

Before you leave, render your support for us to continue. If you like our tutorials, share this post on social media like Facebook/Twitter.

All the very Best,
TechBeamers.

You Might Also Like

20 Demo Sites for Automation Testing

Page Object Model (POM) and Page Factory Guide in Selenium Java

Selenium 4 Relative Locators Guide

Selenium Version 4 Features – What’s New?

How to Inspect Element in Safari, Android, and iPhone

TAGGED:Advanced Selenium Tutorial
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article 3 Unique Ways to Generate Reports in Selenium Webdriver How to Generate Reports in Selenium
Next Article Handle date time picker calendar in selenium webdriver Handle jQuery and Kendo Date Time Picker

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x