[Selenium+java] Handling AJAX call in Selenium webdriver

Source: Internet
Author: User
Tags assert gettext testng guru99

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

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'll updates the part/s of a Web page without reloading the whole page.

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




In this tutorial, you'll 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 f Rom the server without reloading the entire page.

For example if you click on the Submit button, JavaScript would make a request to the server, interpret the result and updat E The current screen without reloading the webpage.

    • An Ajax call was an asynchronous request initiated by the browser, does not directly result in a page transition. It means, if you fire a Ajax request, the user can still work on the application and the request is waiting for a respo Nse.
    • AJAX sends HTTP requests from the client to server and then process the server ' s response, without reloading the entire PA Ge. If you do a AJAX call, your is not pretty sure about the time taken by the server to send a response.

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

How to handle Ajax call Using Selenium webdriver

The biggest challenge in handling Ajax call was knowing the loading time for the Web page. Since the loading of the Web page would last for a fraction of seconds, it's difficult for the tester to test such AP Plication through Automation tool. For that, Selenium Webdriver have to use the-wait method on this Ajax call.

So by executing this wait command, selenium would suspend the execution of current Test case and wait for the expected or n EW value. When the new value or field appears, the suspended test cases'll get executed by Selenium Webdriver.

Following is 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 is sure about the exact wait time. So, your test would fail if the element won ' t show up within the wait time. Moreover, it increases the overhead because calling Thread.Sleep (t) makes the current Thread to being moved from the running Queue to the waiting queue.
    • After the time ' t ' reached, the current thread would move from the waiting queue to the ready queue, and then it takes some Time to is picked by the CPU and is running.
    1. Implicit Wait ()
    • This method tells Webdriver to wait if the element was not a available immediately, but this wait would be on place for the EN Tire time the browser is open. So any search for the elements on the page could take the time of the implicit wait is set for.
    1. Explicit Wait ()
    • Explicit wait is used to freeze the test execution till the time a particular condition is met or maximum time lapses.
    1. Webdriverwait
    • It can be used to any conditions. This can is achieved with webdriverwait in combination with Expectedcondition
    • The best-to-wait for a element dynamically is checking for the condition every second and continuing to the next Comm And in the script as soon as the condition is met.

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

    1. Fluent Wait
    • This was an implementation of the Wait interface has 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 whic H to check the condition.
Challenges in handling Ajax call in Selenium webdriver
    • The Using "pause" command for handling Ajax call was not completely reliable. Long pause time makes the test unacceptably slow and increases the testing time. Instead, "Waitforcondition" would be the more helpful in testing Ajax applications.
    • It's 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 is difficult for testing tools as such AJAX application often use different encoding o R serialization technique to submit POST data.
An Example for Ajax handling
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; @BeforeClasspublic void SetUp () {System.setproperty ("Webdriver.chrome.driver", ". \ \ Chromedriver.exe ")//create chrome instancedriver = new Chromedriver ();d river.manage (). window (). Maximize (); Driver.navigate (). to (URL);} @Testpublic 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 Callwebelement notextelement = driver.findelement (By.classname ("RadioButton")); String Textbefore = Notextelement.gettext (). Trim (),//click on the Radio Buttondriver.findelement (by.id ("yes")). Click ( );//click on Check buttondriver.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.ass Ertnotequals (Textbefore, Textafter); System.out.println ("Ajax call performed"); String expectedtext = "Radio button is checked and it's value is Yes",/*verify expected text with the text updated after Ajax Call*/assert.assertequals (Textafter, Expectedtext);d river.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 is difficult for testing tools as such AJAX application often use different encoding o R serialization technique to submit POST data.

[Selenium+java] Handling AJAX call in Selenium webdriver

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.