Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Ajax Call Synchronization in Selenium Web Driver

Recently many web applications are using Ajax technology to render information on the web page. When using Selenium Web Driver to automate test cases, Web Driver should wait until the active Ajax calls in the system complete. The WaitForAjaxCallsToComplete() method below will dynamically wait until there is no active Ajax calls in the system. Depending on your application, you can set the timeout in the App.Config or just give a hardcoded timeout.

 public void WaitForAjaxCallsToComplete()
        {
            try
            {
var wait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["TimeOut"].ToString())));
            for (int i = 0; i < 30; i++)
                wait.Until(x => (bool)((IJavaScriptExecutor)x).ExecuteScript("return jQuery.active==0"));
            //wait.Until(driver => ((IJavaScriptExecutor)_webDriver).ExecuteScript("return document.readyState").Equals("complete"));
            }
            catch (Exception)
            {

                Console.WriteLine("No Ajax call is detected");
            }
            
            }