Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for network to be idle with selenium webdriver?

Puppeteer has capabilities to wait for network being completely idle, is it possible to achieve the same explicit waiting using python selenium web driver or directly using some javascript work arounds by executing some javascript from the driver context?

waiting for jQuery.active / page ready state etc is common practice, but could it be done that we can query the pending requests and wait until the network is completely idle?

like image 617
symon Avatar asked Feb 13 '19 23:02

symon


1 Answers

So I see 2 questions in this question:

  1. Does Selenium have a way to monitor all web based API calls ? I haven't seen that in any of the Selenium implementations till 3.x.x
  2. Can we implement such a method - Yes. We can increment a counter for every request triggered and decrease it for every request fulfilled, only moving forward with the test script, once the counter is zero. I can envision this being implemented through Chrome Dev protocols (Selenium 4+ ) or a web proxy (browsermob).

Note 1: Selenium officially recommends using fluent waits or explicit waits for such issues.

Note 2: I've seen some crude and abstract implementations of waiting for the document ready state as :

public void waitForPagetoLoad() throws InterruptedException {
    long millis = 1000;
    Thread.sleep(millis);
    do {
        Thread.sleep(millis);
        millis = millis + 2000;
    } while (!(((JavascriptExecutor) driver).executeScript("return document.readyState").toString()
            .equals("complete")) || millis <= 10000);
    Reporter.log("Web page loading is complete in " + (millis / 1000) + "  Seconds");

}
like image 173
Robin Avatar answered Oct 19 '22 03:10

Robin