Sei sulla pagina 1di 7

12/13/2018 Handling AJAX Call in Selenium Webdriver

(https://www.guru99.com/)

Home (/) Testing

SAP Web Must Learn! Big Data

Live Projects AI Blog (/blog/)

Handling AJAX Call in Selenium Webdriver


Ajax is a technique used for creating fast and dynamic web pages. This technique is
asynchronous and uses a combination of Javascript and XML .

It will updates the part/s of a web page without reloading the whole page.

Some of the famous applications that uses AJAX technique are Gmail, Google Maps,
Facebook, Youtube, etc.

In this tutorial, you will learn-

What is Ajax

How to handle Ajax call Using Selenium Webdriver

Challenges in Handling Ajax Call in Selenium Webdriver

What is Ajax
AJAX stands for Asynchronous JavaScript & XML, and it allows the Web page to retrieve
small amounts of data from the server without reloading the entire page.

For example, when you click on submit button, JavaScript (/interactive-javascript-


tutorials.html)will make a request to the server, interpret the result and update the current
screen without reloading the webpage.

https://www.guru99.com/handling-ajax-call-selenium-webdriver.html 1/7
12/13/2018 Handling AJAX Call in Selenium Webdriver

(/images/4-2016/Ajax_call_selenium_webdriver.png)

An Ajax call is an asynchronous request initiated by the browser that does not directly
result in a page transition. It means, if you fire an Ajax request, the user can still work on
the application while the request is waiting for a response.
AJAX sends HTTP requests from the client to server and then process the server's
response, without reloading the entire page. So when you make an AJAX call, you are
not pretty sure about the time taken by the server to send you a response.

From a tester's point of view, if you are checking the content or the element to be displayed ,
you need to wait till you get the response. During AJAX call the data is stored in XML format
and retrieved from the server.

How to handle Ajax call Using Selenium Webdriver


The biggest challenge in handling Ajax call is knowing the loading time for the web
page. Since the loading of the web page will last only for a fraction of seconds, it is difficult
for the tester to test such application through automation tool. For that, Selenium Webdriver
has to use the wait method on this Ajax Call.

So by executing this wait command, selenium will suspend the execution of current Test
Case (/test-case.html)and wait for the expected or new value. When the new value or field
appears, the suspended test cases will get executed by Selenium Webdriver.

Following are the wait methods that Selenium Webdriver can use

1. Thread.Sleep()

Thread.Sleep () is not a wise choice as it suspends the current thread for the specified
amount of time.
In AJAX, you can never be sure about the exact wait time. So, your test will fail if the
element won't show up within the wait time. Moreover, it increases the overhead because
https://www.guru99.com/handling-ajax-call-selenium-webdriver.html 2/7
calling
12/13/2018 Thread.sleep(t) makes the current
Handling thread toSelenium
AJAX Call in be moved from the running queue to
Webdriver

the waiting queue.


After the time 't' reached, the current thread will move from the waiting queue to the ready
queue, and then it takes some time to be picked by the CPU and be running.

2. Implicit Wait()

This method tells webdriver to wait if the element is not available immediately, but this
wait will be in place for the entire time the browser is open. So any search for the
elements on the page could take the time the implicit wait is set for.

3. Explicit Wait()

Explicit wait (/implicit-explicit-waits-selenium.html) is used to freeze the test execution till


the time a particular condition is met or maximum time lapses.

4. WebdriverWait

It can be used for any conditions. This can be achieved with WebDriverWait in
combination with ExpectedCondition
The best way to wait for an element dynamically is checking for the condition every
second and continuing to the next command in the script as soon as the condition is met.

But the problem with all these waits is, you have to mention the time out unit. What if the
element is still not present within the time? So there is one more wait called Fluent wait.

5. Fluent Wait

This is an implementation of the Wait interface having its timeout and polling interval.
Each FluentWait instance determines the maximum amount of time to wait for a
condition, as well as the frequency with which to check the condition.

Challenges in Handling Ajax Call in Selenium Webdriver


Using "pause" command for handling Ajax call is not completely reliable. Long pause
time makes the test unacceptably slow and increases the Testing (/software-
testing.html)time. Instead, "waitforcondition" will be more helpful in testing Ajax
applications.
It is difficult to assess the risk associated with particular Ajax applications
Given full freedom to developers to modify Ajax application makes the testing process
challenging
Creating automated test request may be difficult for testing tools as such AJAX
application often use different encoding or serialization technique to submit POST data.

An Example for Ajax HANDLING


https://www.guru99.com/handling-ajax-call-selenium-webdriver.html 3/7
12/13/2018 Handling AJAX Call in Selenium Webdriver
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Ajaxdemo {

private String URL = "http://demo.guru99.com/test/ajax.html";

WebDriver driver;
WebDriverWait wait;

@BeforeClass
public void setUp() {
System.setProperty("webdriver.chrome.driver",".\\chromedriver.exe");
//create chrome instance
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to(URL);
}

@Test
public void test_AjaxExample() {

By container = By.cssSelector(".container");
wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(container));

//Get the text before performing an ajax call


WebElement noTextElement = driver.findElement(By.className("radiobutton"));
String textBefore = noTextElement.getText().trim();

//Click on the radio button


driver.findElement(By.id("yes")).click();

//Click on Check Button


driver.findElement(By.id("buttoncheck")).click();

/*Get the text after ajax call*/


WebElement TextElement = driver.findElement(By.className("radiobutton"));
wait.until(ExpectedConditions.visibilityOf(TextElement));
String textAfter = TextElement.getText().trim();

/*Verify both texts before ajax call and after ajax call text.*/
Assert.assertNotEquals(textBefore, textAfter);
System.out.println("Ajax Call Performed");

String expectedText = "Radio button is checked and it's value is Yes";


https://www.guru99.com/handling-ajax-call-selenium-webdriver.html 4/7
12/13/2018 Handling AJAX Call in Selenium Webdriver

/*Verify expected text with text updated after ajax call*/


Assert.assertEquals(textAfter, expectedText);
driver.close();
}

Summary:

AJAX allows the Web page to retrieve small amounts of data from the server without
reloading the entire page.
To test Ajax application, different wait methods should be applied
ThreadSleep
Implicit Wait
Explicit Wait
WebdriverWait
Fluent Wait
Creating automated test request may be difficult for testing tools as such AJAX
application often use different encoding or serialization technique to submit POST data.

 Prev (/ssl-certificate-error-handling-selenium.html) Report a Bug

Next  (/listeners-selenium-webdriver.html)

YOU MIGHT LIKE:

SELENIUM SELENIUM SELENIUM

(/selenium-webtable.html) (/use-autoit- (/implicit-explicit-waits-


(/selenium- selenium.html) selenium.html)
webtable.html) (/use-autoit- (/implicit-explicit-
selenium.html) waits-
How to Handle Web Table in
Selenium WebDriver How to use AutoIT with selenium.html)

(/selenium-webtable.html) Selenium Implicit Wait & Explicit Wait


(/use-autoit-selenium.html) in Selenium
(/implicit-explicit-waits-
selenium.html)

SELENIUM SELENIUM SELENIUM

(/handling-iframes- (/find-element- (/testng-report.html)


selenium.html) selenium.html) (/testng-report.html)
https://www.guru99.com/handling-ajax-call-selenium-webdriver.html 5/7
(/handling-iframes-
12/13/2018 (/find-element- TestNG Report Genera on
Handling AJAX Call in Selenium Webdriver

selenium.html) selenium.html) in Selenium WebDriver


Handling iFrames in Find Element and (/testng-report.html)
Selenium Webdriver: FindElements in Selenium
switchTo() WebDriver
(/handling-iframes- (/find-element-
selenium.html) selenium.html)

Selenium Tutorials
42) SSL Certificate Error Handling (/ssl-certificate-error-handling-selenium.html)

43) Handling Ajax call (/handling-ajax-call-selenium-webdriver.html)

45) Execute JavaScript based code (/execute-javascript-selenium-webdriver.html)

46) Using Selenium with Python (/selenium-python.html)

47) Use intelliJ & Selenium (/intellij-selenium-webdriver.html)

52) Flash Testing with Selenium (/flash-testing-selenium.html)

54) Core Extensions (/selenium-core-extensions.html)

55) Using Apache Ant with Selenium (/using-apache-ant-with-selenium.html)

56) Using Selenium with Github (/selenium-github.html)

57) Handling Cookies (/handling-cookies-selenium-webdriver.html)

58) Using SoapUI with Selenium (/using-soapui-selenium.html)

59) XSLT Report in Selenium (/xslt-report-selenium.html)

60) Firefox Profile (/firefox-profile-selenium-webdriver.html)

61) Breakpoints and Startpoints (/breakpoints-startpoints-selenium.html)

62) Selenium Interview Questions (/top-100-selenium-interview-questions-answers.html)

63) Cucumber Selenium (/using-cucumber-selenium.html)

64) Drag & Drop Selenium (/drag-drop-selenium.html)

65) Selenium C# Webdriver (/selenium-csharp-tutorial.html)

66) Creating Object Repository (/object-repository-selenium.html)

67) Scroll UP or Down a page (/scroll-up-down-selenium-webdriver.html)

68) Sikuli Tutorial (/sikuli-tutorial.html)

71) Selenium vs HP UFT (QTP) (/alm-qtp-selenium-difference.html)

72) Selenium Alternatives (/selenium-alternatives.html)


https://www.guru99.com/handling-ajax-call-selenium-webdriver.html 6/7
12/13/2018 Handling AJAX Call in Selenium Webdriver

 (https://www.facebook.com/guru99com/) 
(https://twitter.com/guru99com) 
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)

(https://forms.aweber.com/form/46/724807646.htm)

About
About US (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact US (/contact-us.html)

Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)

Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/)
Quiz (/tests.html)
Review (/best-ergonomic-mouse.html)

Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)

© Copyright - Guru99 2018


Privacy Policy (/privacy-policy.html)

https://www.guru99.com/handling-ajax-call-selenium-webdriver.html 7/7

Potrebbero piacerti anche