0

I want to check status code of these 3 sites: http://google.com, http://birgun.net, http://roche.com. But I need to only check with Selenium Java. I have no idea for checking status codes with Selenium. Can you help me for writing to code? I've checked How to get HTTP Response Code using Selenium WebDriver but I couldn't satisfy, couldn't understand it.

1 Answers1

0

As it is stated in the question you refer it is not possible. Selenium works on UI level. You can apply browsermob-proxy that you can integrate your Selenium test with. In that proxy you can add filter that would check your responses for required status code.

Below is the "fishbone" for sch test.

@Test
public void statusCode(){
    // ...
    proxy.addResponseFilter((httpResponse, httpMessageContents, httpMessageInfo) -> {
        if (httpMessageInfo.getOriginalUrl().equals(getCurrentUrl(driver)) 
                && !httpResponse.getStatus().equals(HttpResponseStatus.OK)){
            // TODO: do smth;
        }
    });
    // ...
}

private String getCurrentUrl(WebDriver driver){
    return driver.getCurrentUrl();
}

Note that you need to narrow the requests scope that you're watching for status code of since proxy processes all the HTTP calls from your page including calls to resources and AJAX calls..

Here you can find example on how to check the page for not loaded images. This is very similar to your case. You can examine it in order to get the general idea of the approach and modify it so that it would meet your need.

Alexey R.
  • 4,490
  • 1
  • 7
  • 22