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

0 votes
May 8, 2019 in Selenium by Megha
1,174 views

1 answer to this question.

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 9, 2019 by Pratibha
• 3,690 points

Related Questions In Selenium

+1 vote
2 answers
0 votes
1 answer

How a prompt alert can be handled in Python Selenium Webdriver?

Hey Duran, you can handle a prompt ...READ MORE

answered Jul 30, 2019 in Selenium by Abha
• 28,140 points
1,276 views
0 votes
2 answers

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

You first need to download chrome driver ...READ MORE

answered Aug 26, 2019 in Selenium by Abha
• 28,140 points
1,660 views
0 votes
1 answer

How can we read data from an excel sheet in Selenium webdriver?

Hi Tarun, to read data from an ...READ MORE

answered May 8, 2019 in Selenium by Abha
• 28,140 points
17,155 views
0 votes
2 answers

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,500 points
12,617 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,572 views
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,910 points
6,629 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,519 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 14, 2019 in Selenium by Pratibha
• 3,690 points
1,090 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP