Five Ways to Test AJAX Calls in Selenium WebDriver
Posted By : Hitesh Pandey | 27-Aug-2019
AJAX represents Asynchronous Javascript And XML. It is a strategy for non concurrently sending an XMLHttpRequest from the website page to the server and sending/recovering information to be utilized on the created site page. The customer side program speaks with the server by means of AJAX, which enables an activity to be performed without expecting to really revive the page. This implies an AJAX call has no noticeable yield in page change, so a client can in any case chip away at the page while a solicitation is being gone before.
AJAX Challenges When Testing with Selenium WebDriver
While AJAX calls improve proficiency and are profitable for clients, they are testing when testing with Selenium WebDriver. In AJAX, the code on the DOM page is produced after an activity, without really changing the page or even the DOM. This makes it extremely hard to know the genuine time when an AJAX call was finished and subsequently when a page is refreshed and with which esteems.
Therefore, you might invest a ton of energy holding up after the reconciliation of each page, which influences both the dependability and the speed of your framework. Misjudging your planning can likewise bomb your test, in the event that you hold up excessively long.
Defeating AJAX Call Challenges in Selenium WebDriver
However, there is an answer: by hanging tight for the components that will show up in the AJAX created pages, you will know when the page stacked. At that point, you can feel good going ahead in the test.
In this article, we will investigate a few valuable approaches to really deal with and manage these AJAX calls, by utilizing Wait techniques.
There are a few Wait techniques you can use to deal with AJAX calls.
1. Unequivocal Waits
Setting a specific wanted condition for the framework in the code. Simply after this condition happens, the activity can continue to the subsequent stage.
Code:
webdriver driver = new firefoxdriver(); driver.get("http://somedomain/url_that_delays_loading"); webelement mydynamicelement = (new webdriverwait(driver, 10)) .until(expectedconditions.presenceofelementlocated(by.id("mydynamicelement")));
2. Understood Waits
You can likewise go for Implicit Waits, where you can choose a specific measure of time you require WebDriver to survey the DOM for. For this situation, your WebDriver will continue searching for an element(s), on the off chance that it had been inaccessible right away. The default time is set as 0, which can be effectively balanced as you may like. What's more, this set hold up time endures as long as your program is open, so an opportunity to look for any component on the page will be the equivalent.
Code:
webdriver driver = new firefoxdriver(); driver.manage().timeouts().implicitlywait(10, timeunit.seconds); driver.get("http://somedomain/url_that_delays_loading"); webelement mydynamicelement = driver.findelement(by.id("mydynamicelement"));
3. Familiar Waits
In the past hold up techniques, you needed to physically set the ideal time per unit. In any case, there are situations when a component still stays inaccessible during this period. For this situation, you can utilize Fluent Waits. These enable you to design the component surveying system. A case of code that is set for 5-second pause and survey of each 100 milliseconds is given beneath:
Code:
fluentwait<by> fluentwait = new fluentwait<by>(by.tagname("textarea")); \\ characterize component for which you need to survey fluentwait.pollingevery(300, timeunit.milliseconds); \\ it will ping for each 5 sec fluentwait.withtimeout(1000, timeunit.milliseconds); \\ max break fluentwait.until(new predicate<by>() { open boolean apply(by by) { attempt { return browser.findelement(by).isdisplayed(); } get (nosuchelementexception ex) { return false; } } }); browser.findelement(by.tagname("textarea")).sendkeys("text to enter");
4. WebDriverWait
Utilize a mix of ExpectedCondition and WebDriverWait techniques and compose a code which will give the framework a chance to hang tight for the set measure of time until a component winds up accessible. What will happen is that the framework will begin checking the condition each second, and once the set criteria is met, it will continue to following advances.
Code:
open expectedcondition<webelement> visibilityofelementlocated(final by) {
return new expectedcondition<webelement>() {
open webelement apply(webdriver driver) {
webelement component = driver.findelement(by);
return element.isdisplayed() ? component : invalid;
}
};
}
open void performsomeaction() {
..
..
wait<webdriver> hold up = new webdriverwait(driver, 20);
webelement component = wait.until(visibilityofelementlocated(by.tagname("a")));
..
}
5. Thread.Sleep
What's more, when utilizing JAVA for your Selenium tests, you can go for the most well-known strategy for Thread.Sleep (<time in ms>). Be that as it may, this strategy isn't prescribed when computerizing tests, so it is encouraged to utilize this technique just in the event that you are not ready to discover another arrangement. The issue with this strategy is that the present string is suspended for an at first indicated measure of time, while in AJAX you can never realize the accurate hold-up time. Consequently, your test faces the danger of falling flat if your components neglect to end up accessible during the hold-up time.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Hitesh Pandey
Hitesh is a Quality Analyst and apart from his profession he has a passion of music and food.