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
Explore your Kubernetes cluster with k9s
If you use Kubernetes, you have probably become adept at crafting commands with kubectl (pronounced “koob-cuddle”). Sure, we prefer to interact with the cluster with declarative configs via a deployment pipeline, but sometimes you need […]
Deploying NextJs v9 on Google App Engine
NextJS v9 offers new exciting features like “File system-based dynamic routing” and “Automatic Static Optimization”, but deploying on Google App Engine introduced some new challenges.  In this post I will walk you through the steps. […]
Managing your Helm deployments with Helmfile
As we’ve been using Kubernetes to build software delivery platforms for our clients, we’ve found Helm to be a reasonable solution to templating and managing deployments to Kubernetes.  Although templating alternatives such as Kustomize have […]
AWS CodeBuild Test Reports for Gradle builds
Although AWS documentation has instructions for adding Test Reports for a maven build they currently lack instructions for a gradle build. You can find the maven instructions here: https://aws.amazon.com/blogs/devops/test-reports-with-aws-codebuild/ Assuming you have your gradle wrapper […]