Bii a ṣe le gba Koodu Ipo Idahun pẹlu Selenium WebDriver

Ni igbagbogbo nigba ti o n ṣiṣẹ awọn sọwedowo adaṣe pẹlu Selenium WebDriver, o tun fẹ lati ṣayẹwo koodu ipo idahun fun orisun kan, gẹgẹbi iṣẹ wẹẹbu tabi awọn oju-iwe wẹẹbu miiran lori aaye naa. O tun le ṣayẹwo fun awọn ọna asopọ ti o fọ lori aaye bi o ṣe n ṣe awọn iwe afọwọkọ Selenium WebDriver.

Jẹ ki a ṣe atunyẹwo oriṣiriṣi awọn koodu ipo HTTP:

2xx - O DARA
3xx - Àtúnjúwe
4xx - Oro ko rii
5xx - Aṣiṣe olupin


Ninu Selenium WebDriver ko si ọna taara lati ṣayẹwo koodu ipo idahun, nitorinaa a ni lati lo ile-ikawe miiran fun eyi. A le lo Onibara Apache Http tabi Mo fẹ lati lo Ile-ikawe ti o ni idaniloju TI isinmi lati Jayway

Lati gba koodu idahun ni lilo isinmi-isinmi a le lo:


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');
} }

Ijade:

200

Lati ṣayẹwo fun awọn ọna asopọ ti o fọ lori oju-iwe lakoko ṣiṣe awọn idanwo Selenium WebDriver, a le lo:

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();
} }

Siwaju sii kika: