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

0 votes
May 8, 2019 in Selenium by Megha
351 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 8, 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 29, 2019 in Selenium by Abha
• 27,930 points
213 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
• 27,930 points
706 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
• 27,930 points
11,877 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,510 points
3,476 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31 in Selenium by Sri
• 2,970 points
4,487 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,890 points
2,568 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 2 in Selenium by Sri
• 2,970 points
2,803 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, 2019 in Selenium by Pratibha
• 3,690 points
353 views