Checking HTTP Status Code in Selenium

This might not be the best use of Selenium for this type of test. There is unnecessary need to load a browser when you could do and have a faster running test

[Test]
[ExpectedException(typeof(WebException), UserMessage = "The remote server returned an error: (404) Not Found")]
public void ShouldThrowA404()
{
    HttpWebRequest task; //For Calling the page
    HttpWebResponse taskresponse = null; //Response returned
    task = (HttpWebRequest)WebRequest.Create("http://foo.bar/thiswontexistevenifiwishedonedayitwould.html");
    taskresponse = (HttpWebResponse)task.GetResponse();
}

If your test is redirecting to another page during a 404 Selenium could check the final page has what you expect.

Leave a Comment