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

0 votes
May 8 in Selenium by Megha
73 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 by Pratibha
• 2,780 points

Related Questions In Selenium

0 votes
1 answer
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 in Selenium by Abha
• 22,960 points
243 views
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,090 points
568 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
896 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
561 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,780 points
64 views