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:
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
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.