Warning: fopen(/tmp/SghXTngBZPli-OPNn8i.tmp): failed to open stream: Disk quota exceeded in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 139

Warning: unlink(/tmp/SghXTngBZPli-OPNn8i.tmp): No such file or directory in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 142
Site Loader
Auckland, New Zealand
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. This can be done using our IJavaScriptExecutor 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));
IJavaScriptExecutor jsScript = Driver as IJavaScriptExecutor;
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 then creating an instance of IJavaScriptExecutor class and using the ExecuteScript method we are waiting Until the jQuery.active == 0 returns true. 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. Thanks, Karthik KK

Post Author: Karthik kk

One Reply to “How to wait for all the Ajax request to complete in a page using Selenium”

Leave a Reply

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