import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import Framework.Driver;
public class Brokenimages {
public static int invalidLink;
String currentLink;
String temp;
public static void main(String[] args) throws IOException {
WebDriver driver = new FirefoxDriver();
driver.get("Url");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
List<WebElement> ele=driver.findElements(By.tagName("a"));
System.out.println("size:" +ele.size() );
for(int i=0;i<ele.size();i++) {
System.out.println(ele.get(i).getAttribute("href"));
int statusCode=0;
try{
statusCode=getResponseCode(ele.get(i).getAttribute("href"));
// ele.get(i).click();
// driver.navigate().back();
}catch(Exception e)
{
e.printStackTrace();
}
if(statusCode==404) {
System.err.println("InvalidLink :"+ele.get(i).getAttribute("href"));
File scrFile4 = ((TakesScreenshot)Driver.driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile4, new File("D:\\tmp\\image_"+new Date().getTime()+" .png"));
}else {
System.out.println("ValidLinks "+ele.get(i).getAttribute("href"));
}
}
}
private static int getResponseCode(String attribute) throws IOException {
URL u = new URL(attribute);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod("GET");
h.connect();
return h.getResponseCode();
}
}
You can changed the Anchor tags and checking the all links like "img" and "src".
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import Framework.Driver;
public class Brokenimages {
public static int invalidLink;
String currentLink;
String temp;
public static void main(String[] args) throws IOException {
WebDriver driver = new FirefoxDriver();
driver.get("Url");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
List<WebElement> ele=driver.findElements(By.tagName("a"));
System.out.println("size:" +ele.size() );
for(int i=0;i<ele.size();i++) {
System.out.println(ele.get(i).getAttribute("href"));
int statusCode=0;
try{
statusCode=getResponseCode(ele.get(i).getAttribute("href"));
// ele.get(i).click();
// driver.navigate().back();
}catch(Exception e)
{
e.printStackTrace();
}
if(statusCode==404) {
System.err.println("InvalidLink :"+ele.get(i).getAttribute("href"));
File scrFile4 = ((TakesScreenshot)Driver.driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile4, new File("D:\\tmp\\image_"+new Date().getTime()+" .png"));
}else {
System.out.println("ValidLinks "+ele.get(i).getAttribute("href"));
}
}
}
private static int getResponseCode(String attribute) throws IOException {
URL u = new URL(attribute);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod("GET");
h.connect();
return h.getResponseCode();
}
}
You can changed the Anchor tags and checking the all links like "img" and "src".
No comments:
Post a Comment