There are many situations where we might need to wait for page to fully load before performing any operations in the UI control of the page using Selenium, but in modern application we might not just have to wait for the page to load but also the behind the scenes operation, which is nothing but the Asynchronous Java script (Ajax) calls to complete (Which performs data binding of control once it gets the response from the data source, it happens even after page load).
Our test will fail tentatively due to these situations. Hence its always wise idea to wait for Ajax call to complete.
interface, which we already discussed in our previous posts.
The idea is simple, if all the Jquery
executions are completed, then it will return jQuery.active == 0
which we can use in our Wait.Until
method to wait till the script return as true.
Hence we can write the code as shown in the below snippet
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(20));
wait.Until((d) => (bool)jsScript.ExecuteScript("return jQuery.active == 0"));
As shown in the above C# code snippet, we are first creating an instance of WebDriverWait
class and using the ExecuteScript method we are waiting Until the jQuery.active == 0
This way we can wait for all the Ajax calls to complete in a page using Selenium
This method will come in handy in many situations.