package ; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class CheckLinks { public static int invalidLink; String currentLink; String temp; public static int responsecode; public static boolean isValid = false; public static void main(String[] args) throws IOException { // Launch The Browser WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Enter Url driver.get(“”);
//Check css & favicon try { List no = driver.findElements(By.tagName(“link”)); int nooflinks = no.size(); System.out.println(“Total CSS & Favicon Icons: ” + nooflinks); for (WebElement pagelink : no) { String linktext = pagelink.getText(); String link = pagelink.getAttribute(“href”); URL u = new URL(link); HttpURLConnection h = (HttpURLConnection) u.openConnection(); h.setRequestMethod(“GET”); h.connect(); int responsecode = h.getResponseCode(); System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode); //System.out.println(link); } } catch (Exception e) {
}
//Check Javascripts try { List no = driver.findElements(By.tagName(“script”)); int nooflinks = no.size(); System.out.println(“Total Javascripts: ” + nooflinks); for (WebElement pagelink : no) { String linktext = pagelink.getText(); String link = pagelink.getAttribute(“src”); URL u = new URL(link); HttpURLConnection h = (HttpURLConnection) u.openConnection(); h.setRequestMethod(“GET”); h.connect(); int responsecode = h.getResponseCode(); System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode); //System.out.println(link); } } catch (Exception e) {
}
//Check Images try { List no = driver.findElements(By.tagName(“img”)); int nooflinks = no.size(); System.out.println(“Total Images: ” + nooflinks); for (WebElement pagelink : no) { String linktext = pagelink.getAttribute(“alt”); String link = pagelink.getAttribute(“src”); URL u = new URL(link); HttpURLConnection h = (HttpURLConnection) u.openConnection(); h.setRequestMethod(“GET”); h.connect(); int responsecode = h.getResponseCode(); System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode); //System.out.println(link); } } catch (Exception e) {
}
//Check hyperlinks. try { List no = driver.findElements(By.tagName(“a”)); int nooflinks = no.size(); System.out.println(“Total hyperlinks: ” + nooflinks); for (WebElement pagelink : no) { String linktext = pagelink.getText(); String link = pagelink.getAttribute(“href”); URL u = new URL(link); HttpURLConnection h = (HttpURLConnection) u.openConnection(); h.setRequestMethod(“GET”); h.connect(); int responsecode = h.getResponseCode(); System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode); //System.out.println(link); } } catch (Exception e) {
} System.out.println(“— End of Test —“);
driver.close(); driver.quit(); } }
package <Your Package>;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CheckLinks {
public static int invalidLink;
String currentLink;
String temp;
public static int responsecode;
public static boolean isValid = false;
public static void main(String[] args) throws IOException
{
// Launch The Browser
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Enter Url
driver.get(“<URLre>”);
//Check css & favicon
try
{
List<WebElement> no = driver.findElements(By.tagName(“link”));
int nooflinks = no.size();
System.out.println(“Total CSS & Favicon Icons: ” + nooflinks);
for (WebElement pagelink : no)
{
String linktext = pagelink.getText();
String link = pagelink.getAttribute(“href”);
URL u = new URL(link);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod(“GET”);
h.connect();
int responsecode = h.getResponseCode();
System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode);
//System.out.println(link);
}
}
catch (Exception e)
{
}
//Check Javascripts
try
{
List<WebElement> no = driver.findElements(By.tagName(“script”));
int nooflinks = no.size();
System.out.println(“Total Javascripts: ” + nooflinks);
for (WebElement pagelink : no)
{
String linktext = pagelink.getText();
String link = pagelink.getAttribute(“src”);
URL u = new URL(link);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod(“GET”);
h.connect();
int responsecode = h.getResponseCode();
System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode);
//System.out.println(link);
}
}
catch (Exception e)
{
}
//Check Images
try
{
List<WebElement> no = driver.findElements(By.tagName(“img”));
int nooflinks = no.size();
System.out.println(“Total Images: ” + nooflinks);
for (WebElement pagelink : no)
{
String linktext = pagelink.getAttribute(“alt”);
String link = pagelink.getAttribute(“src”);
URL u = new URL(link);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod(“GET”);
h.connect();
int responsecode = h.getResponseCode();
System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode);
//System.out.println(link);
}
}
catch (Exception e)
{
}
//Check hyperlinks.
try
{
List<WebElement> no = driver.findElements(By.tagName(“a”));
int nooflinks = no.size();
System.out.println(“Total hyperlinks: ” + nooflinks);
for (WebElement pagelink : no)
{
String linktext = pagelink.getText();
String link = pagelink.getAttribute(“href”);
URL u = new URL(link);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod(“GET”);
h.connect();
int responsecode = h.getResponseCode();
System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode);
//System.out.println(link);
}
}
catch (Exception e)
{
}
System.out.println(“— End of Test —“);
driver.close();
driver.quit();
}
}