Last active
April 30, 2024 12:29
-
-
Save roydekleijn/7c7098ead88d0b5a42534a6b8184e3da to your computer and use it in GitHub Desktop.
checkPendingRequests - checks for pending HTTP requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import org.openqa.selenium.JavascriptExecutor; | |
import org.openqa.selenium.support.events.EventFiringWebDriver; | |
public class WaitUtil { | |
public static void checkPendingRequests(final String context, final EventFiringWebDriver driver) { | |
final int timeoutInNumberOfTries = 50; | |
try { | |
if (driver instanceof JavascriptExecutor) { | |
final JavascriptExecutor jsDriver = driver; | |
System.out.println("Number of active calls " + context + ": "); | |
boolean timeout = true; | |
for (int i = 0; i < timeoutInNumberOfTries; i++) { | |
Thread.sleep(100); | |
final Object numberOfAjaxConnections = jsDriver.executeScript("return window.openHTTPs"); | |
// return should be a number | |
if (numberOfAjaxConnections instanceof Long) { | |
final Long n = (Long) numberOfAjaxConnections; | |
System.out.println(" " + n); | |
if (n.longValue() == 0L) { | |
timeout = false; | |
break; | |
} | |
} else { | |
// If it's not a number, the page might have been freshly loaded indicating the monkey | |
// patch is replaced or we haven't yet done the patch. | |
monkeyPatchXMLHttpRequest(driver); | |
} | |
} | |
if (timeout) { | |
throw new RuntimeException("Pending XHR requests even after 50 times checking (100 msec) for:" + context); | |
} | |
} else { | |
System.out.println("Web driver: " + driver + " cannot execute javascript"); | |
} | |
} catch (final InterruptedException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
private static void monkeyPatchXMLHttpRequest(final EventFiringWebDriver driver) { | |
try { | |
if (driver instanceof JavascriptExecutor) { | |
final JavascriptExecutor jsDriver = driver; | |
final Object numberOfAjaxConnections = jsDriver.executeScript("return window.openHTTPs"); | |
if (numberOfAjaxConnections instanceof Long) { | |
return; | |
} | |
final String script = " (function() {" + "var oldOpen = XMLHttpRequest.prototype.open;" + "window.openHTTPs = 0;" + | |
"XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {" + "window.openHTTPs++;" + | |
"this.addEventListener('readystatechange', function() {" + "if(this.readyState == 4) {" + "window.openHTTPs--;" + "}" + | |
"}, false);" + "oldOpen.call(this, method, url, async, user, pass);" + "}" + "})();"; | |
jsDriver.executeScript(script); | |
} else { | |
System.out.println("Web driver: " + driver + " cannot execute javascript"); | |
} | |
} catch (final Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I do this in python?