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. He has no fixed address and has left footprints on 40-something different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, the proper use of semicolons, and finding good food.

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: Codista, 5 years ago

What a stupid idea!!! Changing the front-end to solve a problem of the backend... You should review your concepts

Comment posted by: Kirk Spriggs, 6 years ago
https://www.track-r.net is the future of Google rank trackers, we wanted to see if you'd like to try it for free for a week. 7 days to see the data it brings you. You can use that and our free advice on how to increase your Google rankings so you bring in more customers. Sounds good? Course it does, it's free. No credit card details, just register and start straight away. If you like it, keep it, if it's not for you that's cool. Hope to see you join the 3,000 others since the start of April 2018!
Comment posted by: roy, 9 years ago
good
Comment posted by: Ajay, 10 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, 11 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, 12 years ago

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

Comment posted by: , 12 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, 12 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, 12 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.