Tips to create robust selenium tests

Published Wednesday, October 19, 2011 9:49 PM

According to a survey of open source automation testing tools, Selenium is the most popular of them all. And it is getting more and more popular everyday. As a matter of fact, it is currently the only viable test tool to test website as a whole, because it can simulate running a website as a real user would do. And one set of tests can be run across differnt browsers.

However browser testing is complicated, the ubiqutous injection of asynchorons AJAX only makes the situation worse. On top of its own quirkiness, Selenium has to deal all of the quirkiness of differnt browsers, the possible failure of http requests, etc.

Therefore, browser simulation does come with a cost and not all users (e.g., me) are happy. Some of the biggest challenges that Selenium have now are:

  • Test brittleness
  • Infrastrucuture maintenance
  • Test run-time during
  • Documentation

Source: Top 5 Challenges for Selenium Users (San Francisco Meetup)

Some of the much complained test brittleness can be avoided or alleviated by using some of the tips (I learned them from the presentation at Top Selenium Tips From The Sauce Codebase, who distilled the essence from the sucesses or failures 8 million tests):

1) Use implicit waits

        public IWebElement ImplicitWaitForElement(IWebDriver webDriver, String id, int timeout = 10)




            webDriver.Url = "http://somedomain/url_that_delays_loading";

            IWebElement myDynamicElement = webDriver.FindElement(By.Id(id));

            return myDynamicElement;



2) Use explicit the right way

Never use static wait:

sleep (10)

(Why? Because a) it increases overhead, you make the webdriver wait no matter what; b) you can never predict what is the correct time interval.)

Instead, use a condition wait

  public IWebElement ExplicitWaitUntilForElement(IWebDriver webDriver, String id, int timeout = 10)


            webDriver.Url = "http://somedomain/url_that_delays_loading";

            var wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeout));

            return wait.Until((d) => d.FindElement(By.Id("someDynamicElement")));




 public void WaitforElementPresent(IWebDriver webDriver, String id, int timeout = 10)


            //var sw = new Stopwatch();



            for (int i = 0; i< 60; i++)




                    if ((webDriver.FindElement(By.Id(id)).Enabled) == true)





                catch (Exception)


                    // Continue the loop, though it may throw an exception at first





So the methods above instruct the web driver within a limited time range for an element to appear before giving up and report the error.

3) Create a magic login method

In a lot of selenium test cases, we call the same login method again to gain the entry to another password protected pages. Doing so has quite some drawbacks: a) Login failure will invariably cause the failure of the other tests; b) Overhead. If a login method takes, say 10 secs., then 100 tests would waste 1,000 secs on simply login.

So instead, the Sauce Lab teams to create a magic login page to allow automatic login, that is create VIP test account to allow unlimited access for inspection purposes.


Selenium -Open Source Test Automation: An Overview
WebDriver: Advanced Usage
Top Selenium Tips From The Sauce Codebase
Top 5 Challenges for Selenium Users (San Francisco Meetup)

Selenium - Page Object Pattern

by xxxd
Filed under:


No Comments

This site

This Blog



  • MaximumASP