Check Broken Links on a website using Selenium

It's a routine Sanity test to check broken links on any we application. Here's automated piece of code that can help you achieve listing of all Broken Links of a specific web URL:

BROKEN LINK CHECKER
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class BrokenLinkChecker {

    public static WebDriver BrokenLinkCheckerTest(WebDriver driver) {

        String url = "";
        HttpURLConnection huc = null;
        int respCode = 200;

        List<WebElement> links = driver.findElements(By.tagName("a"));
        Iterator<WebElement> it = links.iterator();

        while (it.hasNext()) {
            url = it.next().getAttribute("href");
            System.out.println(url);
            if (url == null || url.isEmpty()) {
                System.out.println("URL is either not configured for anchor tag or it is empty");
                continue;
            }

            try {
                huc = (HttpURLConnection) (new URL(url).openConnection());
                huc.setRequestMethod("HEAD");
                huc.connect();
                respCode = huc.getResponseCode();
                if (respCode >= 400) {
                    System.out.println("FAILURE----" + url + "   ---- is a broken link --- ");
                } else {
                    System.out.println(url + " is working as expected");
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return driver;

    }
}

 TESTNG CLASS (BrokenLinkTest ) TO INVOKE ABOVE CLASS (BrokenLinkChecker)
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import common.Modules.BrokenLinkChecker;
import utility.InitialiseBrowsersAndCaptureScreenshot;
import utility.listeners;

// listeners to add logs in log file
@Listeners({ listeners.class })
public class BrokenLinkTest  {
    public WebDriver driver;

   
    @BeforeMethod
    @Parameters({ "URL", "browser" })
    public void OpenHomePage(String URL, String browser) throws IOException {
        driver = initialiseDriver(browser);
        driver.navigate().to(URL);
        driver.manage().window().maximize();
    }

    @AfterMethod
    // scope is within the class
    public void CloseCurrentSession() throws InterruptedException {
        driver.close();
    }
   
    @Test
    public void CheckAllbrokenlinksOnTheProvidedURL()
     {
        BrokenLinkChecker.BrokenLinkCheckerTest(driver);
    }
}

 TESTNG.XML TO INVOKE ABOVE TEST (BrokenLinkTest)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
 <listeners>  
      <listener class-name="utility.listeners"/>
 </listeners>
<test name="Check Broken links on Website">
     <parameter name="URL" value="https:/www.ishandevshukl.com"/>
    <parameter name="browser" value="Chrome"/>
    <classes>
      <class name="BrokenLinkTest"/>
    </classes>
  </test> 
</suite> 

HAPPY TESTING
Check Broken Links on a website using Selenium Check Broken Links on a website using Selenium Reviewed by Ishan Dev Shukl on May 13, 2019 Rating: 5

No comments:

Thanks a lot for your valuable Comment!

Powered by Blogger.