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.
Asked
Active
Viewed 96 times
1 Answers
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.
- 8,057
- 2
- 11
- 27
-
I'm sorry I couldn't understand the code. Can you write all code for me for these 3 sites that I've shared with you. – ArthurMorgan Nov 02 '20 at 18:15
-
If I fetch body of HTML I can say this site is up. How can I write it with Java? – ArthurMorgan Nov 02 '20 at 18:21