Selenium Tips: Improving your waiting skills

Posted Oct 7th, 2009

Waiting for elements to appear in your page is something we do pretty much everywhere in our tests, but the way you wait for them to do it can impact in the efficiency of your scripts in interesting ways. Let's analyze a test that has a step like the following:

waitForElementPresent css=h2:contains('Sauce Labs') 60000

This waits for the title "Sauce Labs" to appear, which will work as expected most of the times. But, what if you app fails and the title's text loaded is incorrect? It will take the script to eat the whole 60 second timeout before noticing and reporting it. Then, a much better approach to write this kind of waiting is:

waitForElementPresent css=h2.titles_class 60000
assertText css=h2.titles_class Sauce Labs

It will now detect the title is loaded at the moment it does, no matter which content it has, and then move on to assert for it's text. As you can see, a simple change in your scripts like this one will make them both run faster and report errors more accurately. Stay tuned, we'll be bringing more tips to improve your Selenium tests every week.

Written by

Santiago Suarez Ordoñez