Jun 4, 2015

Waiting for a Redirect Chain to Settle using Selenium WebDriver

Selenium WebDriver is a great framework for automating browser usage in automated testing and scripting. Some interactions with the browser can be tricky. Recently I came across a case where the login page redirected several times before landing on an application page. This post will describe how I waited for the application to settle before continuing.

I needed to change the URL after login and the redirects were causing issues occasionally. The issue was definately related to timing. It’s difficult to tell but I’m pretty sure after setting the URL using WebDriver the browser redirect took effect and nullified my URL change.

A typical solution, and simplier, than what is described here is to wait for an element on the target page to become available. Unfortunately, the application I was dealing with did not have a predictable landing page. It depends on the page the user last visited and the test data environment is such that I couldn’t create a new user with known state.

My solution is to create an ExpectedCondition implementation that waits for the application to stabilize with respect to redirects. Conditions are used with Selenium’s Wait interface to wait for a given condition to be “successful”. A condition can return any object. If the object is a Boolean then a return value of Boolean.TRUE is considered success. Other classes will be considered a success when the return value is non-null.

The following example waits for the body element to become visible:

My DocumentSettleCondition checks two properties. The first is the document.readyState property to be complete for the stabilization period. The second is the value of WebDriver.getCurrentUrl() to not change during the stabilization period. DocumentSettleCondition wraps another condition. After the document is considered settled, the value of the wrapped condition is returned. Since we don’t know what page we’re going to land on, the wrapped condition will be something generic, like By.cssSelector("body") or an element common across the application, from the header, footer, etc.

In my particular case, there is a “forwarding to the application” page that can wait past the settle condition. Checking for the body tag won’t work because the condition will settle on this page and send WebDriver the URL before the forwarding is done. So we can wait for this page not to be present for the settle time using the invisibilityOfElementLocated condition.

If this seems like it’s becoming complicated, I agree. Unfortunately that happens in web applications from time to time. If you have the ability to influence how the application is built, consider testing in the design. Sometimes you’re bolting testing on after the fact. Hopefully the DocumentSettleCondition will get you a little further along the path to success.

About the Author

Patrick Double profile.

Patrick Double

Principal Technologist

I have been coding since 6th grade, circa 1986, professionally (i.e. college graduate) since 1998 when I graduated from the University of Nebraska-Lincoln. Most of my career has been in web applications using JEE. I work the entire stack from user interface to database.   I especially like solving application security and high availability problems.

Leave a Reply

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

Related Blog Posts
Designing Kubernetes Controllers
There has been some excellent online discussion lately around Kubernetes controllers, highlighted by an excellent Speakerdeck presentation assembled by Tim Hockin. What I’d like to do in this post is explore some of the implications […]
React Server Components
The React Team recently announced new work they are doing on React Server Components, a new way of rendering React components. The goal is to create smaller bundle sizes, speed up render time, and prevent […]
Jolt custom java transform
Jolt is a JSON to JSON transformation library where the transform is defined in JSON. It’s really good at reorganizing the json data and massaging it into the output JSON you need. Sometimes, you just […]
Page Object Model for UI Testing