I am using Selenium Remote WebDriver. I read all links from csv file and run test against those links. But sometimes I get 404 response.
Is there any way in Selenium WebDriver to check that we get HTTP response 200?
I am using Selenium Remote WebDriver. I read all links from csv file and run test against those links. But sometimes I get 404 response.
Is there any way in Selenium WebDriver to check that we get HTTP response 200?
There is no way to get HTTP status codes directly in the WebDriver API. It has been a long-standing feature request, which will likely never be implemented in the project. The correct solution to your problem is to configure your browser to use a proxy which can intercept and log the network traffic, and have your code query that proxy for he result you're after.
Of course, if all you are interested in is checking a link to make sure it returns a 200 status code, you could easily just use an HTTP client library in whatever language you desire to navigate to the link. There's no need to use WebDriver unless you need to manipulate the resulting page in some way.
before using selenium, you could use something like:
public static boolean linkExists(String URLName){
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con =
(HttpURLConnection) new URL(URLName).openConnection();
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
Using it in this way:
WebDriver driver = new FirefoxDriver();
for(String url : csvArray){
if(linkExists(url)){
driver.get(url);
.
.
.
}
}
Our site has a custom error page for 404 responses. The page title on that page says "404 - Page Not Found". I use driver.Title and check for the text "not found".
Using C#, I wrote the following:
// Check for 404 page:
var pageNotFoundTitleText = "not found";
if (driver.Title.ToLower().Contains(pageNotFoundTitleText)) throw new Exception("### 404 - Page Not found: " + link);
You can do it using RestAssured import static com.jayway.restassured.RestAssured.given;
int returnCode = given().when().baseUri(url).get().getStatusCode();
if (returnCode == 200) {
webDriver.get(url);
helper.asserts.assertTrue(helper.finder.isElementPresent(By.className("ticketType")));
} else {
helper.asserts.fail("This url "+url+" is returning the following code: " + returnCode);
};
It's a way for that(i think:P).
You can use JavaScript for check http status on page. JS can be called in Java in the following way:
((JavascriptExecutor) webDriver).executeScript(js);
Best option is create dummy webpage on node hard disk with JS function and call that function in executeScript() on that page.
You can also try send all JS code in executeScript(), but i'm not sure that will work.
© 2022 - 2024 — McMap. All rights reserved.