Nzira yekuwana sei Kuteerera Kodhi Kodhi neSelenium WebDriver

Kazhinji paunenge uchimhanyisa otomatiki cheki neSelenium WebDriver, iwe zvakare unoda kutarisa kodhi yemamiriro ekupindura kwechinhu, senge webhu webhu kana mamwe mapeji ewebhu pane ino saiti. Iwe unogona zvakare kutarisa zvakaputsika zvinongedzo pane ino saiti sezvauri kuita Selenium WebDriver zvinyorwa.

Ngationgororei akasiyana emakodhi emamiriro eHTTP:

2xx - Zvakanaka
3xx - Kudzokorodza
4xx - Resource haina kuwanikwa
5xx - Server kukanganisa

MuSelenium WebDriver hapana nzira yakanangana yekutarisa kodhi yemamiriro ekupindura, saka isu tinofanirwa kushandisa rimwe raibhurari kune izvi. Tinogona kushandisa Apache HttpClient kana ini ndoda kushandisa REST-yakavimbiswa raibhurari kubva kuJayway

Kuti titore kodhi yekupindura tichishandisa REST-takavimbiswa isu tinogona kushandisa:

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

Kubuda:



200

Kuti utarise zvisungo zvakaputsika pane peji uchiita Selenium WebDriver bvunzo, tinogona kushandisa:

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

Kuenderera mberi kuverenga: