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
Feature Flags in Terraform
Feature flagging any code can be useful to developers but many don’t know how to or even that you can do it in Terraform. Some benefits of Feature Flagging your code You can enable different […]
Infrastructure as Code – The Wrong Way
You are probably familiar with the term “infrastructure as code”. It’s a great concept, and it’s gaining steam in the industry. Unfortunately, just as we had a lot to learn about how to write clean […]
Snowflake CI/CD using Jenkins and Schemachange
CI/CD and Management of Data Warehouses can be a serious challenge. In this blog you will learn how to setup CI/CD for Snowflake using Schemachange, Github, and Jenkins. For access to the code check out […]
How to get your pull requests approved more quickly
TL;DR The fewer reviews necessary, the quicker your PR gets approved. Code reviews serve an essential function on any software codebase. Done right, they help ensure correctness, reliability, and maintainability of code. On many teams, […]