Webinar: Hot Topics in Test Design Automation with Bloor Research Register Now.

Selenium ExpectedConditions: How to Wait in Your Test

Selenium is one of the most prominent choices when people need a test automation tool. Well, strictly speaking, Selenium is…

By Testim,

Selenium is one of the most prominent choices when people need a test automation tool. Well, strictly speaking, Selenium is a browser automation tool. But more often than not, people end up using the automation power of Selenium to perform complex end-to-end testing. When doing such tests, you often need to use wait commands. This post is all about a particular wait feature in Selenium, Selenium ExpectedConditions.

We’ll structure the post into two main parts. The first one is about fundamentals. We’ll start with a brief overview of Selenium. After that, we’ll explain what waits are and why they’re so important. Finally, we’ll offer a brief introduction to ExpectedConditions in Selenium, including an example.

Selenium ExpectedConditions: What Is It All About?

Before we show you how to use Selenium ExpectedConditions features, let’s start with some basics on Selenium itself.

A Quick Overview of Selenium WebDriver

Selenium comes in three main versions. Selenium IDE is a browser extension you use to create simple record-and-playback tests. If you need to do grid testing—i.e., testing across different browsers and operating systems—then Selenium Grid is the flavor of Selenium that can help you.

But when you think “Selenium,” you’re probably thinking of Selenium WebDriver. This is the third and most powerful version of Selenium, which allows you to drive—that is, to control—the browser in complex ways. Rather than being a tool, Selenium is more like an API that supports several programming languages. You pick your favorite language from the pool of supported languages and write your interactions.

What Are Wait Commands in Selenium, and Why Do They Matter?

When you’re writing testing scripts that exercise a web app through its UI, you often run into time-related issues. That happens because different elements on the page may load at different times. If you try to interact with an element by clicking a button, your test script will fail when that element isn’t visible yet.

Expand Your Test Coverage

Fast and flexible authoring of AI-powered end-to-end tests — built for scale.
Start Testing Free

That’s where wait commands come in handy. They allow you to instruct your test script to wait for a certain amount of time before progressing. So, wait commands are essential to ensure you test your web application in a way that’s as close as possible to how a real user would.

An Introduction to Selenium ExpectedConditions

The different wait commands available in Selenium can be split into two main categories: implicit waits and explicit waits. (There is a third category, fluent waits, but you could argue that they’re a specialized type of explicit waits.)

Implicit waits allow your test script to wait for a specific amount of time before going on.

Explicit waits are more flexible. They enable you to wait until certain expectations are met. For instance, you can instruct your test to wait until a given button becomes visible, a text file receives a certain string of text, or a given element is no longer present.

Selenium ExpectedConditions is the feature of Selenium WebDriver that allows you to use explicit waits. There is a long list of expected conditions you can use, and you’ll see some of them up next.

Selenium ExpectedConditions: an Example in Java

The first part of the post is over. You now understand what ExpectedConditions are and why they matter so much when writing tests in Selenium.

Obtaining the Requirements

Though Selenium WebDriver works with many languages, we’ll use Java in this tutorial. To follow along, you’ll need to have the Java SDK installed, as well as Maven.

Selenium WebDriver itself is distributed as an executable, according to your browser and operating system. Click here to download the driver for the Chrome browser. Make sure you download the driver that matches the version of Chrome you have installed. After downloading the executable, save it somewhere and add its location to the system’s path. You’ll need an IDE or text editor in which to write the code. Though I’ll use the free Visual Studio Code, you’re welcome to use whatever editor or IDE you’re most comfortable with.

After everything is in place, start a new Java project using Maven as the build system. Add the following to your pom.xml file:

Starting the Example

Let’s start with our example. We’ll navigate to Testim’s site, click on a link that will take us to another page. There, we’ll retrieve the text of an element that contains the contact email.

We’ll start by adding the imports we’ll need:

Now for the actual code. This is what the main method should look like:

Let’s explain what the code above does. In the first line, we instantiate a new instance of the ChromeDriver class, assigning it to a variable named “driver.” With this driver object, we navigate to Testim’s site (https://testim.io).

After accessing the site, we then use XPath to retrieve the button we want. The “button” is actually a link, specifically the second link inside the div, which has the class btn-group.

We then perform a click on the button. That brings us to yet another page, where we want to retrieve the text from the element with the class contact-email. After doing so, we print the obtained text to the console.

Adding a Wait Using ExpectedConditions

The example above works perfectly. But let’s imagine that the contact email element took some time to become visible. That would create the need for us to wait. As mentioned, wait commands in Selenium come in two main categories: implicit and explicit.

The explicit waits are the ones we want to use here. They allow us to instruct our script to wait until certain conditions are met. How would we change our example to introduce an explicit wait?

Here’s the new version using ExpectedConditions:

In this new version of the code, we use a new object from type WebDriverWait. We pass both our driver and a timeout value in seconds and then use Selenium ExpectedConditions to define that our script should wait until the element identified by the class name contact-email becomes clickable.

What Are the Other Expected Conditions?

The list of all the possible conditions you can wait on is long. Besides that, it varies according to the language binding you’re using. Here are just a few:

  • alert is present
  • element exists
  • element is visible
  • title contains
  • element is clickable
  • title is
  • element staleness
  • element is selected
  • visible text

To see all of the available conditions for Java, refer to the ExpectedCondition class documentation.

Test Automation: There Is Life After Selenium

In today’s post, we’ve covered ExpectedConditions in Selenium. You’ve learned about the importance of waits in automated web testing.

As it turns out, though, Selenium isn’t the only alternative when it comes to automated end-to-end tests in web applications. Despite its popularity and many advantages, the tool also has its cons, such as the tendency to generate flaky tests.

Testim Automate is an AI-powered test automation tool you can use to create more robust tests and don’t result in heavy test maintenance. Testim Automate also offers several types of wait you can leverage in your testing.

Create a free Testim account today to give it a try.

What to read next

How to Fix Selenium’s Element Is Not Clickable at Point

How to Check If an Element Exists in Selenium

 

Testim's latest articles, right in your inbox.

From our latest feature releases, to the way it impacts the businesses of our clients, follow the evolution of our product

Blog Subscribe