Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.



Programming and Learning from SD

Our project uses KnockoutJS to data-bind data to our html elements (the same would be true for AngularJS binding). We’ve recently moved from testing against localhost to running our Selenium tests against an Azure hosted version of the same site. We’ve been noticing tests fail because of timing issues and the increased latency that weren’t there with the localhost tests.

Discovered Approaches

Use the driver.GetAndWaitForElement(By selector) method. This will help you make sure the DOM element is already there. However, with data binding, the element will probably be there, but might still be empty as it waits for the AJAX request to complete.

The Selenium driver.Manage() can be helpful too, but use GetAndWaitForElement for most options.

var initial = 5;
try
{
   // fail more quickly if it isn't found, container is a IWebElement
   driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1));
   return container != null ? container.FindElement(bySelector) : driver.FindElement(bySelector);
}
finally
{
   driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(initial));
}

Avoid Thread.Sleep as much as possible. This seems to be an easy and quick fix for timing issues, but it’s fragile and adds time to the build test run. Who really knows how long it will take? There are too many variables. Should you make it 1 second and risk a random failure or 5 seconds then multiple that times 100 and add on 5 minutes+ to your test run?

Retry the DOM query based on a known state. We’ve found that having a common loading element on pages with lists of data has been very helpful. Other times you don’t want a loading spinner annoying the user, so the item could be checked if it’s empty (or only has the default values) then queried again in a while loop after a second or two.

public void WaitForLoadingToFinish()
{
    IWebElement loadingDiv = this.GetIsLoadingDiv();
    var counter = 0;
    while ((loadingDiv != null && loadingDiv.Displayed) && counter <= 12)
    {
        Console.WriteLine("WaitForLoadingToFinish => " + counter);
        Thread.Sleep(TimeSpan.FromSeconds(1d));
        loadingDiv = this.GetIsLoadingDiv();
        counter++;
    }

    Thread.Sleep(500);
}

 

Conclusion

Tests failing because of timing issues, especially when they passed during development against a local server, is annoying and expensive. I’ve shared a few approaches we’ve found that help avoid some of these, without using Thread.Sleep.

Posted on Thursday, October 16, 2014 2:42 PM Knockout Js , UI Automated Testing , Selenium , AngularJS | Back to top


Comments on this post: Selenium and timing issues

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Aligned | Powered by: GeeksWithBlogs.net