Can anyone explain how an AJAX call can be handled in Selenium webdriver?

0 votes
May 8 in Selenium by Megha
37 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Hello Megha, to demonstrate how AJAX calls are handled in Selenium Webdrivers, I am sharing this code snippet. Please try this code by yourself and let me know if this could help you:

import java.util.concurrent.TimeUnit;

import java.util.function.Function;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.support.ui.WebDriverWait;

import managers.FileReaderManager;


public class Wait {

public static void untilJqueryIsDone(WebDriver driver){

untilJqueryIsDone(driver, FileReaderManager.getInstance().getConfigReader().getImplicitlyWait());

}


public static void untilJqueryIsDone(WebDriver driver, Long timeoutInSeconds){

until(driver, (d) ->

{

Boolean isJqueryCallDone = (Boolean)((JavascriptExecutor) driver).executeScript("return jQuery.active==0");

if (!isJqueryCallDone) System.out.println("JQuery call is in Progress");

return isJqueryCallDone;

}, timeoutInSeconds);

}

public static void untilPageLoadComplete(WebDriver driver) {

untilPageLoadComplete(driver, FileReaderManager.getInstance().getConfigReader().getImplicitlyWait());

}


public static void untilPageLoadComplete(WebDriver driver, Long timeoutInSeconds){

until(driver, (d) ->

{

Boolean isPageLoaded = (Boolean)((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");

if (!isPageLoaded) System.out.println("Document is loading");

return isPageLoaded;

}, timeoutInSeconds);

}

public static void until(WebDriver driver, Function<WebDriver, Boolean> waitCondition){

until(driver, waitCondition, FileReaderManager.getInstance().getConfigReader().getImplicitlyWait());

}


private static void until(WebDriver driver, Function<WebDriver, Boolean> waitCondition, Long timeoutInSeconds){

WebDriverWait webDriverWait = new WebDriverWait(driver, timeoutInSeconds);

webDriverWait.withTimeout(timeoutInSeconds, TimeUnit.SECONDS);

try{

webDriverWait.until(waitCondition);

}catch (Exception e){

System.out.println(e.getMessage());

}          

}

}
answered May 8 by Pratibha
• 2,520 points

Related Questions In Selenium

0 votes
1 answer

Can anyone help me that how to run Selenium WebDriver test cases in Chrome?

Download the executable driver from: ChromeDriver Download Before ...READ MORE

answered Mar 29, 2018 in Selenium by nsv999
• 5,050 points
153 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,050 points
164 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
553 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
152 views
0 votes
1 answer
0 votes
1 answer

Can someone please explain the Selenium Webdriver Architecture ?

Hey Keshav, there are four basic components ...READ MORE

answered May 13 in Selenium by Pratibha
• 2,520 points
24 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.