Uyifumana njani iKhowudi yokuPhendula ngeSelenium WebDriver

Rhoqo xa usebenzisa iitshekhi ezizenzekelayo ngeSelenium WebDriver, ufuna ukukhangela ikhowudi yempendulo yesixhobo, njengenkonzo yewebhu okanye amanye amaphepha ewebhu kule ndawo. Ungajonga amakhonkco aphukileyo kwindawo njengoko usenza i-Selenium WebDriver script.

Makhe siqwalasele iikhowudi zeHTTP ezahlukeneyo:

2xx-Kulungile
3xx-Ukuhlengahlengiswa
4xx - Ubuncwane abufunyenwanga
5xx-Impazamo yeseva


KwiSelenium WebDriver akukho ndlela ngqo yokujonga ikhowudi yempendulo, ke kufuneka sisebenzise enye ilayibrari yoku. Singasebenzisa Iapache HttpClient okanye ndikhetha ukusebenzisa REST-eqinisekisiwe ilayibrari evela eJayway

Ukufumana ikhowudi yokuphendula sisebenzisa i-REST-eqinisekisiwe sinokusebenzisa:


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

Iziphumo:

200

Ukujonga amakhonkco aphukileyo kwiphepha ngelixa usenza iimvavanyo zeSelenium WebDriver, sinokusebenzisa:

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

Ukuqhubeka kokufunda: