Is Page Fully Loaded Yet?

First of all, why would we even care? Remember webdriver’s rule of thump: Webdriver is not able to interact with web elements that are not visible & enabled yet.

So it would be nice if we can find out a way to check a page’s loading status: If a page is fully loaded, it is safe to interact with the web elements on this page.

But is there any general and always-applicable answer to this question? Nope. With javascript running with page load, modern web becomes quite complicated. A page could have some everlasting script running underneath the page to send/receive data with servers periodically and changes the html every time they run(Thus never done!). One typical example of this would be a page with embedded player playing music all day long.

All we need to do is understand the individual cases and make sure that the element we are about to interact with is fully loaded.

Ways to check a page’s loading status:

1. If the page doesn’t have everlasting script running:
Check document.readyState value using javascript. The value shall be ‘complete’ when it is fully loaded.
This check is implement already in driver.get(url). WebDriver will not give control back to you till the page is completely loaded.

As a result of this implementation, if AUT has request-hanging issue, it will be reflected in your test suites execution time.

But this will not work if page has redirects via AJAX requests and running scripts – those can’t be caught by Selenium, it doesn’t wait for them to finish. Also, you can’t reliably catch them via readyState – it waits for a bit, which can be useful, but it will signal complete long before all the AJAX content is downloaded.

Your suggested solution only waits for DOM readyState to signal complete. But Selenium by default tries to wait for those (and a little bit more) on page loads via the driver.get() and element.click() methods. They are already blocking, they wait for the page to fully load and those should be working ok.

Problem, obviously, are redirects via AJAX requests and running scripts – those can’t be caught by Selenium, it doesn’t wait for them to finish. Also, you can’t reliably catch them via readyState – it waits for a bit, which can be useful, but it will signal complete long before all the AJAX content is downloaded.

There is no general solution that would work everywhere and for everyone. What we can do is to rely on WebDriver to do his part, then use implicit waits which solves most of the problems, then use explicit waits for elements you want to assert on the page,

2. If the page does have everlasting script running:
Only because you have such a script running, doesn’t mean that the whole page is affected. If the section that your automation trying to interact with is not affected by the everlasting script, then a regular step 1 check is sufficient

If the section you are trying to interact with does affected, and there is no way to decide whether or not the loading is complete or not, wait till the individual element is visible using fluentWait or WebDriverWait.

When do we need to wait for page to load?

When some action you do on the page triggers a page reloading, like a ajax call, or navigation to another page, or
Also, it is also possible that a web page is constantly requires new data from servers and update itself automatically periodically, with no user interaction at all. An example would be a page that contains a player playing song track one after another.

This ‘check page loading status + fluentwait on individual element’ combination strategy would keep your code simplistic and resilient, and it is the key to get rid of the flakiness of your code!

Leave a Reply

Your email address will not be published. Required fields are marked *