Over a million developers have joined DZone.

Options for Validating HTTP Codes in Selenium

There is no way to directly get HTTP status codes in the WebDriver API. But that doesn’t mean you have to leave Selenium or go without any status codes in your test scripts. Let me show you how it's done.

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

There is no way to directly get HTTP status codes in the WebDriver API (see the lengthy discussion in issue #141). But that doesn’t mean you have to leave Selenium or go without any status codes in your test scripts.

In fact, some of the supported methods – proxies and tightly coupled headless browsers – should make you feel at home if you’ve transitioned from Selenium RC, or are comfortable with traffic sniffing proxies.

Headless Scriptable WebKits that Play Nicely With Selenium

If you don’t want to invest the time configuring a proxy, consider setting up WebDriver to handle headless testers like HTMLUnitDriver, Jasmine, or PhantomJS.HTMLUnitDriver is well-supported in Selenium, but its JavaScript engine (Rhino) is not used by most browsers. However, if you know you won’t be testing any pages with JavaScript, then it’s no concern.

But if you are, use GhostDriver to enable PhantomJS as WebDriver’s backend.


This can seem like both the most straightforward and heavyweight solution. The Selenium core devs point out that Fiddler and BrowserMob are supported OOTB in WebDriver, and setup is relatively simple:


To take it a little further, Jim Evans, maintainer of the Selenium .NET bindings, has given the .NET crowd an excellent three-part guide on how to hook up Fiddler to a test page and Selenium, begin filtering for basic status codes and customize for redirect issues and cross-browser testing.

…Or Grab the Error Text From the Actual Browser

If that’s more than you need, then you can stay in WebDriver and use this common hack: Just grab the error code displayed by the actual browser. It is good enough for returning errors so long as you know the text to be displayed and where it will display, and aren’t interested in all of the routing through a redirect path.

Here’s a simple example:


So What Should You Do?

Parsing response data from the page is the easiest way to extend your current use of Selenium to grab some basic HTTP status code data; there are no proxies to set up, and no new test frameworks to learn.

But you will miss any errors not displayed on a page, likely including all 3** redirects. Using GhostDriver to handle PhantomJS will handle status code returns, and keep everything in CI in Selenium—and it’s in that sense the best tool for this job. But you may already have some testing proxies set up, since they do let you do a little more real-world test building, like white/black list and simulate load and slowdown conditions. And of course, that doesn’t mean you can’t use GhostDriver to drive PhantomJS with WebDriver in a proxy, which you can of course host and automate in Sauce and AWS.

More Resources:

Issue Tracker History of WebDriver HTTP code Issue

Rantings of a Selenium Contributor (on the HTTP status code controversy)

WebDriver Proxy Examples

SO: Checking HTTP Status Codes in Selenium

How to Get HTTP Status Codes in Selenium

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

selenium rc,frameworks,bindings,http,core,.net,selenium

Published at DZone with permission of Alex Entrekin, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}