There is a data driven web application developed using JSF. Are there any tools which can help finding links which lead to a 404 page ? Currently we get to know only when somebody clicks manually and gets a 404 which might be due to a query that did not work or any other reasons too. The links are dynamic ones with hidden parameters, so i presume a static link checker will not work. Selenium could be one possibility but we would have to write code to cover every possible path.
Any suggestions ?
You can use the following code to get all the links in a web page and check each link using HttpURLConnection
to check the status of the link.
WebDriver driver = new FirefoxDriver();
driver.get("urlOfWebsite");
List<WebElement> links = driver.findElements(By.tagName("a"));
ArrayList<String> linksInvalid = new ArrayList<>();
ArrayList<String> linksResulting404 = new ArrayList<>();
for (WebElement link : links) {
URL u;
try {
u = new URL(link.getAttribute("href"));
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
huc.connect();
int code = huc.getResponseCode();
if (code == 404 || code == 400) {
//Add link text and href to linksResulting404 list
linksResulting404.add(link.getText() + " - "
+ link.getAttribute("href"));
}
} catch (MalformedURLException e) {
//Add link text and href to linksResulting404 list
linksInvalid.add(link.getText() + " - "
+ link.getAttribute("href"));
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Invalid links : ");
for (String linkInvalid : linksInvalid) {
System.out.println(linkInvalid);
}
System.out.println("\nLinks resulting in 404/400");
for (String linkResulting404 : linksResulting404) {
System.out.println(linkResulting404);
}
Let me know if this helps you.