Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting HTTP Status Code by Selenium WebDriver C#

I'm using Selenium, C#, NUnit to write automated tests.

Is it possible to get the HTTP status code with WebDriver so that tests that fail due to HTTP requests can be alerted to the user?

like image 350
user2184530 Avatar asked Aug 28 '13 08:08

user2184530


People also ask

How can I get HTTP status code?

Just use Chrome browser. Hit F12 to get developer tools and look at the network tab. Shows you all status codes, whether page was from cache etc.

Can we get the HTTP response code in Selenium with Java?

We can get the HTTP response code in Selenium webdriver with Java. Some of the response codes are – 2xx, 3xx, 4xx and 5xx. The 2xx response code signifies the proper condition, 3xx represents redirection, 4xx shows resources cannot be identified and 5xx signifies server problems.

How do I get a 200 status code?

The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.


1 Answers

Not without an external tool no.

You see, this has been brought up a number of times in the past and is one of the largest "issues" within Selenium's official issue tracker. The particular issue has been bounced around and essentially decided that it's outside the scope of Selenium.

This however, does not mean that it is not possible. Thankfully, you are using C#, so it's a little easier than you may think.

Recently, one of the Selenium developers wrote a blog post outlining exactly how to do this in C#. It is a three part blog post to help explain each step and uses an external tool, called Fiddler (which, by the way, is one awesome tool).

Fiddler is a proxy and has a C# API that allows you to intercept requests. It therefore means you can simply "point" Selenium to use that proxy, and then hook into Fiddler's API to check exactly what status code the request has.

I am merely linking to the blog post rather than giving you code to this since it's pretty thoroughly explained in those posts.

Perhaps try it, and then come back if you get any issues implementing his design. Incidentally the guy is also a member here at StackOverflow, so may also see your post.

like image 89
Arran Avatar answered Sep 22 '22 13:09

Arran