Sida loo helo Code Status Status ka leh Selenium WebDriver

Marar aad u tiro badan markaad ku shaqeyneyso jeeg otomaatig ah Selenium WebDriver, waxaad sidoo kale dooneysaa inaad hubiso nambarka jawaabta ee kheyraadka, sida adeegga websaydhka ama bogagga kale ee bogga. Waxaad sidoo kale ka hubin kartaa xiriiriyeyaasha jaban bogga maadaama aad fulinayso qoraallada Selenium WebDriver.

Aynu dib u eegno xeerarka xaaladda HTTP ee kala duwan:

2xx - Waayahay
3xx - Toosinta
4xx - Kheyraad lama helin
5xx - Cilad Server

Selenium WebDriver ma jiro hab toos ah oo lagu hubiyo lambarka jawaabta, markaa waa inaan u isticmaalnaa maktabad kale tan. Waan isticmaali karnaa Macmiilka Apache Http ama waxaan door bidaa inaan isticmaalo Maktabadda lagu kalsoon yahay ee laga helo Jayway

Si aad u hesho nambarka jawaabta adoo isticmaalaya REST-hubi waxaan isticmaali karnaa:

import io.restassured.RestAssured; public class HttpResponseCode {
public int httpResponseCodeViaGet(String url) {

return RestAssured.get(url).statusCode();
}
public int httpResponseCodeViaPost(String url) {
return RestAssured.post(url).statusCode();
}
public static void main(String args[]) {
new HttpResponseCode().httpResponseCodeViaGet('http://www.google.com');
} }

Wax soo saarka:



200

Si loo hubiyo xiriiriyeyaasha jabay bogga inta lagu gudajiro tijaabooyinka Selenium WebDriver, waxaan isticmaali karnaa:

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.List; public class HttpResponseCode {
WebDriver driver;
int statusCode
public void checkBrokenLinks() {
driver = new FirefoxDriver();
driver.get('https://devqa.io');

//Get all the links on the page
List links = driver.findElements(By.cssSelector('a'));

String href;

for(WebElement link : links) {

href = link.getAttribute('href');

statusCode = new HttpResponseCode().httpResponseCodeViaGet(href);

if(200 != statusCode) {


System.out.println(href + ' gave a response code of ' + statusCode);

}
}
}
public static void main(String args[]) {
new HttpResponseCode().checkBrokenLinks();
} }

Akhrin Dheeraad ah: