The WebDriverWait

It won’t take very long for an automated tester to realize that their test become brittle when dealing with dynamic web pages. Elements the tester is trying to manipulate will change in the middle of your tests causing exceptions.

Take, for example, the test we created in the Hello World! post. The Help button is one of the last things to load on the Google search page.

Lets test that the text of the Help button is indeed “Help”. Add the following to the HelloWorld test right before the WebDriver disposal.

IWebElement imagesLink = webDriver.FindElement(By.XPath("//a[[text()='Images for Hello World!'\]]"));
Assert.AreEqual(true, imagesLink.Displayed);

Run the test and you’ll find that you receive a StaleElementReferenceException.

You may be tempted to use something like c# threading functionality to “pause” the test and wait for a dynamic website to settle. This should be avoided at all cost. “Pauses” of a constant length will waste valuable resource time and you’ll find that your tests still fail occasionally.

Instead, perform a “pause” of dynamic length. The WebDriverWait can be used for this purpose.

The WebDriverWait class is contained in the OpenQA.Selenium.Support.UI library, so import it in addition to the traditional Selenium library. The Support.UI library is contained in the Selenium WebDriver Support Classes nuget package.

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

Now, we can wrap the code we added to HelloWorld in a WebDriverWait.

new WebDriverWait(webDriver, TimeSpan.FromSeconds(5)).Until<bool>(dr =>
    {
        try
        {
            IWebElement imagesLink= dr.FindElement(By.XPath("//a[[text()='Images for Hello World!']]"));
            Assert.AreEqual(true, imagesLink.Displayed);
            return true;
        }
        catch
        {
            return false;
        }
    });

The WebDriverWait constructor takes two parameters.

  • IWebDriver webDriver – this is your WebDriver object.
  • TimeSpan Timeout – this is a time span which describes the maximum amount of time we will wait for the test to pass.

The Until method takes one parameter, a function. This syntax is referred to as a lambda expression.

The variable ‘dr’ becomes a reference to the calling object. In our case, the WebDriverWait.

What’s going to happen is code between the squiggly brackets is going to loop until either it returns true or the timeout is exceeded. We want to avoid throwing exceptions during this time so we catch all exceptions and return false instead.

With this technique, you can “wait” for an element to appear without wasting resource.

Ninja Level:
  • Make your timeout a global variable. This way if you need to increase/decrease your timeout, you only need to change it in one place.
  • You can change the object between the brackets to that of another object type. In this case, the function loops until a non-null value is returned.

Leave a Reply

Your email address will not be published. Required fields are marked *