0

Please check the below code which i tried Here i want to check if all links are getting opened that should not contain any 404 page

public void alllinks() {

        suites.setupEnviroment();
        WebDriver driver = suites.getWebDriver();
        driver.get(suites.WEB_PATH5);
        Dimension d = new Dimension(1455, 900);
        driver.manage().window().setSize(d);

        try {
            List<WebElement> links = driver.findElements(By.tagName("a"));
            ArrayList<String> targets = new ArrayList<String>();
            // collect targets locations
            for (WebElement link : links) {
                targets.add(link.getAttribute("href"));
            }
            for (String target : targets) {
                driver.get(target);
                try {
                    ((WebDriver) links).getPageSource().contains("404");
                } catch (Exception e) {
                    System.out.println("error");
                }

                // do what is needed in the target
            }

            Logger.getLogger("results").log(new LogRecord(Level.INFO,
                    MethodHandles.lookup().lookupClass().getCanonicalName() != null ? "success" : "failure"));
            driver.close();

        } catch (Exception e) {

            Logger.getLogger("results").log(new LogRecord(Level.INFO,
                    MethodHandles.lookup().lookupClass().getCanonicalName() == null ? "success" : "failure"));
            }

Thanks in advance!

1 Answers1

0

this sample should do the job. Adapt it to your needs.

public class FindBrokenLinks {
    private WebDriver driver;
    private int invalidLinks = 0;

    @BeforeClass
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("http://google.com"); // change the url
    }

    @Test
    public void checkForBrokenLinks() {
        try {
            List<WebElement> links = driver.findElements(By.tagName("a"));
            for (WebElement link : links) {
                if (link != null) {
                    checkLink(link);
                }
            }
            System.out.println("Total broken links: " + invalidLinks);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @AfterClass
    public void tearDown() {
        if (driver != null)
            driver.quit();
    }

    public void checkLink(WebElement linkElement) throws IOException {
        HttpURLConnection connection = null;
        try {
            String link = linkElement.getAttribute("href");
            URL url = new URL(link);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            int responseCode = connection.getResponseCode();

            // change the code for your needs.
            if (responseCode == 404) {
                // you can trow error also ...
                System.out.println("Found invalid link: " + link);
                invalidLinks++;
            }
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
            if (connection != null) {
                connection.getErrorStream().close();
            }
        }
    }
}
Sers
  • 12,047
  • 2
  • 12
  • 31
Infern0
  • 2,565
  • 1
  • 8
  • 21