Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Selenium Ajax Synchronization

This method below applies to Selenium .Net

When you test web applications, sometimes you need to wait an active AJAX process is completed. You can achieve a WaitForAjax method with few lines of code as listed below.

In this method, when you call the WaitForAjax method, you can define maximum timeout time for the AJAX to be completed. For example, if the active AJAX should be completed within 40 seconds, you can call the method : WaitForAjax(40);

This method below tells you how much actual time the AJAX synchronization took by looking at the message on the console.

public void WaitForAjax(int timeoutInSeconds)
        {
            TimeSpan duration;
            DateTime currentTime = DateTime.Now;
            Console.WriteLine("Ajax is synchronizing  ...............");
            while (true)
            {

                bool complete = (bool)((IJavaScriptExecutor)_webDriver).ExecuteScript("return jQuery.active==0");
                DateTime newCurrentTime = DateTime.Now;
                duration = newCurrentTime - currentTime;
                if (complete || duration.Seconds >= timeoutInSeconds)
                    break;
                Thread.Sleep(1000);

            }
            Console.WriteLine("Total elapsed time is : " + duration.Seconds);
        }