Quantcast
Channel: Geekswithblogs.net
Viewing all articles
Browse latest Browse all 6441

Selenium and timing issues

$
0
0

Originally posted on: http://geekswithblogs.net/Aligned/archive/2014/10/16/selenium-and-timing-issues.aspx

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.

publicvoid 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.


Viewing all articles
Browse latest Browse all 6441

Trending Articles