Problem Solver

How To Get The HTTP Status Code In Selenium WebDriver

By , 16 November 2012

How To Get The HTTP Status Code In Selenium WebDriver
How To Get The HTTP Status Code In Selenium WebDriver

One of the first things you'll notice when you start testing your web application with Selenium WebDriver is that there is no API to get the HTTP status code for a page. If you want to know why, you can go and read WebDriver issue #141. Personally I don't care why - I just want to test my HTTP reponse codes (especially 403 Permission Denied).

There is a fairly simple workaround you can use for WebDriver, but firstly lets look at how to do it without WebDriver. Nobody said you HAVE to use Selenium right?

How To Get The HTTP Status Code In Selenium WebDriver

Apache HTTPClient

This is a Java library use for general HTTP network communication and includes supports sessions via cookies. Their fluent API makes it relatively simple to get the response code for a URL.

    public int getResponseCode(String url) {
        try {
            return Request.Get(url).execute().returnResponse().getStatusLine()
                    .getStatusCode();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

You can easily add the HTTPClient fluent api to your Java project via maven:

    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>fluent-hc</artifactId>
      <version>4.2.1</version>
      <scope>test</scope> 
    </dependency>

The disadvantage of HTTPClient is it is doesn't include a browser like Selenium, although you can create sessions which might be useful for testing more complex use cases.

HTMLUnit

HTMLUnit is a headless Java browser and is actually one of the browsers supported by WebDriver. It's very easy to get the response code with HTMLUnit:

    public int getResponseCode(String url) {
        try {
            WebClient client = new WebClient();
            client.setThrowExceptionOnFailingStatusCode(false);
            return client.getPage(url).getWebResponse().getStatusCode();
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

This library is also available from Maven.

    <dependency>
      <groupId>net.sourceforge.htmlunit</groupId>
      <artifactId>htmlunit</artifactId>
      <version>2.10</version>
      <scope>test</scope>
    </dependency>

Selenium WebDriver

Finally we get to WebDriver. If you've written a few tests with WebDriver already you're probably used to this API, only there is no native support for inspecting response headers. The simple workaround is to write the response code to your response output.

For example, on my 403 Permission Denied page, I have:

   <h1 id="web_403">403 Access Denied</h1>

which can be easily checked via the WebDriver API:

    public boolean is403(WebDriver driver) {
        try {
            driver.findElement(By.id("web_403"));
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

You can generalise the idea for most HTTP responses that don't cause a redirect by adding a meta field to your <head> section. In JSF / Servlets 3.0 you do it like this:

    <meta id="web_response" name="response" 
          content="${facesContext.externalContext.response.status}"/>

 

This can be tested easily with WebDriver:

    /**
     * Selenium doesn't give you access to the response headers, so we parse 
     * the content. This relies on the web_response meta tag in the head 
     * section of the output.
     */
    public int getResponseCode(WebDriver driver) {
        return Integer.parseInt(driver.findElement(By.id("web_response"))
                .getAttribute("content"));
    }

Note for Servlets before version 3.0 you will need to implement your own response.getStatus() method and I'm told that in PHP you use $_SERVER['REDIRECT_STATUS'] for versions < 5.4 and http_response_code()for newer versions. Let me know how it is done in your language in the comments below.

This solution works pretty well and is a lot more productive than bashing on the Selenium developers for the gaping hole in their API.

So get testing slacker!

 

 

 

About Roger Keays

How To Get The HTTP Status Code In Selenium WebDriver

Roger Keays is an artist, an engineer, and a student of life. Since he left Australia in 2009, he has been living as a digital nomad in over 40 different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, and finding good food. Click here to subscribe to his weekly blog, or stalk him on Facebook and Twitter.

Leave a Comment

Please visit https://RogerKeays.com/how-to-get-the-http-status-code-in-selenium-webdriver to add your comments.

Comment posted by: roy, last year
good
Comment posted by: roy, last year
good
Comment posted by: roy, last year
good
Comment posted by: Ajay, 2 years ago

The above code only works well with success code ie. 200.for all other codes exception is thrown without returning the response code.

I have tried with Apache HTTPClient and for web driver code getting error

Comment posted by: kav123, 3 years ago

I badly need the HTTP header request that I make for a url setting proxy and user agent.

How do you get it done in java? I see the above code for apache HTTP client, which I can't do it in Firefox web driver. My application is built on firefox completely. I need to test this to see whether my HTTP request is correct or not?

Have you worked around to get it done in firefox?

 

Comment posted by: Adam Goucher, 3 years ago

Well, clearly you would know better than I. In the meanwhile, I'll be busy succeeding with it.

Comment posted by: , 3 years ago

When compared to the fact that we're using HTTP and HTML to build complex applications, this doesn't seem like such an egregious hack.

Comment posted by: Yaci, 3 years ago

 @Adam: BrowserMob is

  1. Not very reliable
  2. Still in beta phase
  3. It's an abandoned project (AFAIK the main developer Patrick Lightbody left Neustar and the future of the project is unknown)

It's better to use Firebug+Netexport, but this works only on Firefox. Sure you're right - using a proxy should be the correct way to go, but as long as there are no reliable solutions we have to rely on hacks like the one above.

Comment posted by: Adam Goucher, 3 years ago

Or. You could route your scripts through a proxy that has an API that exposes this to you. Like say, the BrowserMob Proxy, which has a nice integration (as far as these things go) with WebDriver -- and will work with any browser, and any language.

But hey, if you want to do more work than necessary whilst trolling, I suppose that is up to you.

Join Over 1000 Subscribers

I write every Sunday about travel, psychology, and technology. Thousands of people just like you have already subscribed—and for good reason. It'll change your life. And it's free :)

Get My New Book

“A spellbinding true story of love, passion and adventure. One can’t help but be swept away by 100% Love Guaranteed.” —Dr Tammie Matson, author of Elephant Dance.

Chat For A While

Have Your Say

I want to read more about ...
Travel
Psychology
Technology
Productivity
Investment
Dating