Verify broken images
public class BrokenLinks {
WebDriver driver= null;
@BeforeClass
public void launchbrowser()
{
System.setProperty("webdriver.chrome.driver", "C:/ChromeDriver/chromedriver_2.41.exe");
driver=new ChromeDriver();
driver.get("http://the-internet.herokuapp.com/");
}
@Test
public void brokenlink() throws ClientProtocolException, IOException
{
//Verify Broken images
WebElement links=driver.findElement(By.xpath(".//*[text()='Broken Images']"));
links.click();
List<WebElement> imagelist=driver.findElements(By.xpath(".//div/img"));
for(WebElement images:imagelist)
{
HttpClient client= HttpClientBuilder.create().build();
HttpGet request= new HttpGet(images.getAttribute("src"));
HttpResponse response = client.execute(request);
if(response.getStatusLine().getStatusCode()!=200)
{
System.out.println(response.getStatusLine().getStatusCode()+":- image is broken");
}
else
{
System.out.println(response.getStatusLine().getStatusCode()+":- image is working");
}
}
}
}
Output-
Broken URL
@Test
public void brokenURL() throws ClientProtocolException, IOException {
// Verify Broken url
String url1 = "http://the-internet._herokuapp.com/";
String url2 = "http://the-internet.herokuapp.om/";
String url3 = "http://the-internet.herokuapp.com/";
List<String> url = new ArrayList<String>();
url.add(url1);
url.add(url2);
url.add(url3);
int size = url.size() - 1;
for (int i = 0; i <= size; i++) {
try {
String urlcount = url.get(i);
URL obj = new URL(urlcount);
HttpURLConnection con = (HttpURLConnection) obj
.openConnection();
con.setRequestMethod("GET");
if (String.valueOf(con.getResponseCode()).equals("200")) {
System.out.println(String.valueOf(con.getResponseCode())
+ ":- image is Working");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output-
No comments:
Post a Comment