Last Updated on 2022-07-15 by James Croft
It’s common in UI test automation that your test code can often respond faster than your app’s code. Wait conditions in Selenium provide the ability to simply wait for a scenario to be met before continuing your test.
Here you’ll find how wait conditions work in Selenium WebDriver with C#. You’ll also discover how you can simplify your wait conditions further using Legerity, an open-source library built on Selenium.
What are wait conditions in Selenium?
A wait condition provides a way to delay your tests running when you need a condition to be met first. A common scenario this might occur is when your application is waiting on the result of a network request.
When your UI test accesses an element that appears after a network request, without a wait, an exception is thrown. Using a wait condition with your Selenium WebDriver solves this challenge.
Wait conditions in Selenium expand across more than just looking for UI elements. They provide the ability to wait for a text match or wait to check the title of your application page. The possibilities for wait conditions are endless.
By using conditions with your Selenium WebDriver, you can pause your test until it is met, or it times out. A perfect way to help your UI tests continue to run with the unexpected around the edge challenges.
Best practice for wait conditions
When you’re writing your C# Selenium WebDriver tests, you might be tempted to put a Thread.Sleep() to solve a wait. Before you do, the WebDriver provides a wait condition that can do this for you.
Known as explicit wait conditions, these are driver executed functions that will stop execution until met or it times out. The difference between driver implicit wait conditions and explicit is that you have more control. However, they are only applied at the point of execution, not global to your test as are implicit wait conditions.
To use an explicit wait condition in Selenium, you’ll need to add the following using namespace to your C# class:
using OpenQA.Selenium.Support.UI;
And then, you can initialize and run your explicit wait condition with your specific requirements.
new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(driver => driver.Title.Equals("Homepage"));
In this example, the wait condition is setup using the Selenium WebDriver instance and a TimeSpan used as a timeout. To note, a zero second timeout will cause the condition to be expected immediately.
The Until() chain is where you can define your wait condition. Here is where you can validate the state of your application before continuing the test.
Improving your conditions with Legerity
Introduced in Legerity v0.7.0, new extension methods have been created for the Selenium WebDriver, IWebElement, and Legerity’s own IElementWrapper to simplify wait conditions.
Built as a wrapper around the WebDriverWait class, the WaitUntil() extension methods provides the ability to run wait conditions with timeout and retry attempts without the additional setup.
To use the Legerity extensions, you can initialize and run your wait until conditions simply calling the extension method.
// Wait on the WebDriver
driver.WaitUntil(driver => driver.Title.Equals("Homepage"), TimeSpan.FromSeconds(5));
// Wait on a WebDriver element
element.WaitUntil(element => element.IsVisible), TimeSpan.FromSeconds(5));
// Wait on a Legerity element wrapper
image.WaitUntil(image => image.Source.Equals("image.png"), TimeSpan.FromSeconds(5));
If you’re using the Legerity AppManager, you can also provide a wait condition when calling the StartApp to ensure your application has time to start before running your test code!
Out-of-the-box wait conditions in Legerity
As an additional extra, Legerity also comes predefined with some Selenium WebDriver conditions that you can pass to the WaitUntil() method. These are available via the WaitUntilConditions static class providing options for:
- TitleIs, for validating the window title matches a specified title
- TitleContains, for validating the window title contains a case-sensitive substring
- UrlIs, for validating the window’s URL matches a specified URL
- UrlContains, for validating the window’s URL contains a case-sensitive substring
- ElementExists, for validating that an element can be located on the current window
- ElementExistsInElement, for validating that an element can be located within another element
- ElementExistsInPage, for validating that an element can be located within the context of a Legerity page object
Get Legerity and start implementing wait conditions
Explicit waits are a powerful tool that you can take advantage of in your Selenium WebDriver tests. With the ease of extension methods in Legerity, you can go one step further and simplify your test approach.
Learn more about Legerity and discover how you can easily get up and running with UI testing in C#.
Discover more from James Croft
Subscribe to get the latest posts sent to your email.






