0

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 ?

Chakra
  • 2,525
  • 8
  • 43
  • 82

1 Answers1

0

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.

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
  • Good work by you, but when you say "links in a web page" it means that these links are hard coded, static URL's. But this application has links which appear only when one navigates to a dynamically generated page. So i think i need something like Selenium tests which will simulate a manual user. – Chakra Jun 20 '14 at 08:15
  • so you want to validate links on each and every page of your website? If so refer to the answer which I gave for http://stackoverflow.com/questions/24257802/how-to-browse-a-whole-website-using-selenium/24316831#24316831 . I am sure you will be able to figure it out. Let me know if this works. – StrikerVillain Jun 20 '14 at 09:57