How To Find Broken Links


There always a problem to find the broken links/Images present in the application.This post helps you to find the Broken links.And it captures all the valid links and invalid links in an Excel Sheet
Here i am using java.net.HttpURLConnection.This can be done in many ways too,This code can be used for any project though.

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Brokenlinks {

WebDriver driver;

@BeforeTest
public void openSite() throws InterruptedException {
FirefoxProfile profile= new FirefoxProfile();
profile.setAssumeUntrustedCertificateIssuer(true);
profile.setAcceptUntrustedCertificates(true);
driver = new FirefoxDriver(profile);
driver.get(“https://seleniumbycharan.wordpress.com”);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

@AfterTest
public void tearDown() {
driver.close();
}

public static boolean getResponseCode(String urlString) throws IOException {
boolean isbool = false;
try {

HttpResponse h = new DefaultHttpClient().execute(new HttpGet(
urlString));
int statusCode = h.getStatusLine().getStatusCode();
if (statusCode == 404 || statusCode == 505 || statusCode == 400
|| statusCode == 500 || statusCode == 204) {
isbool = false;
} else {
isbool = true;
}

} catch (Exception e) {
System.out.println(e.getMessage());
}
return isbool;
}

@Test(priority = 1)
public void findBrokenLinks() throws Exception {
Thread.sleep(3000);

FileOutputStream f = new FileOutputStream(
“D:/Workspace/Reports/ProjectLinks.xls”);
WritableWorkbook wb = Workbook.createWorkbook(f);
WritableSheet s = wb.createSheet(“Links_urls”, 0);

WritableCellFormat cellFormat_Result_pass = new WritableCellFormat();
cellFormat_Result_pass.setBackground(Colour.LIGHT_GREEN);
WritableCellFormat cellFormat_Result_fail = new WritableCellFormat();
cellFormat_Result_fail.setBackground(Colour.RED);
WritableCellFormat cellFormat_Result2 = new WritableCellFormat();
cellFormat_Result2.setBackground(Colour.GREY_25_PERCENT);
List<WebElement> ele = driver.findElements(By.tagName(“a”));
System.out.println(“number of links present : ” + ele.size());
boolean isvalidlink = false;
for (int i = 0; i < ele.size(); i++) {
String url = ele.get(i).getAttribute(“href”);
s.addCell(new Label(0, i, ele.get(i).getText()));

if (url != null) {
isvalidlink = getResponseCode(url);
if (isvalidlink) {
System.out.println(i + ” : ” + ele.get(i).getText() + ” : ”
+ “valid links : ” + url);
s.addCell(new Label(1, i, url));
s.addCell(new Label(2, i, “PASS”, cellFormat_Result_pass));
} else {
System.out.println(i + ” : ” + ele.get(i).getText() + ” : ”
+ “broken links : ” + url);
s.addCell(new Label(1, i, url));
s.addCell(new Label(2, i, “FAIL”, cellFormat_Result_fail));

}

} else {
System.out.println(i + ” : ” + ele.get(i).getText() + ” : ”
+ “no url”);
s.addCell(new Label(1, i, “No url attached”));
s.addCell(new Label(2, i, “NO URL”, cellFormat_Result2));

}

}

wb.write();

wb.close();
}
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s