/**
* Validate invalid images on the webpage*/
public void validateInvalidImages() {
try {
int invalidImageCount = 0;
List "<"imagesList">" = driver.findElements(By.tagName("img"));
System.out.println("Total no.of images are :: " + imagesList.size());for (WebElement imgElement : imagesList) {
if (imgElement != null) {
invalidImageCount = verifyimageActive(imgElement);
}
}
System.out.println("Total no.of invalid images are :: " + invalidImageCount);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void verifyimageActive(WebElement imgElement) {
int invalidImageCount =0;
try {
HttpClient client = HttpClientBuilder.create().build();HttpGet request = new HttpGet(imgElement.getAttribute("src"));
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 200){
invalidImageCount++;
}
} catch (Exception e) {
e.printStackTrace();
}
return invalidImageCount;
}
/**
* Validate brokenLinks on the webpage*/
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class VerifyLinks {
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.google.co.in/");
List
System.out.println("Total links are "+links.size());
for(int i=0;i
WebElement ele= links.get(i);
String url=ele.getAttribute("href");
verifyLinkActive(url);
}
}
public static void verifyLinkActive(String linkUrl)
{
try
{
URL url = new URL(linkUrl);
HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
httpURLConnect.setConnectTimeout(3000);
httpURLConnect.connect();
if(httpURLConnect.getResponseCode()==200)
{
System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage());
}
if(httpURLConnect.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND)
{
System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage() + " - "+ HttpURLConnection.HTTP_NOT_FOUND);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}