Sunday, 31 July 2016

Basic 'Broken Link Checker' using Selenium WebDriver


Link Health is very important for any website to reach its customers.
One can make sure if all the links of the website are broken or not, using various available open source or licensed tools.

Below is one of the basic way of validating the links on a particular page using Selenium WebDriver.
NOTE: Can be part of your various regression tests.


Sample Code:



Selenium alone is not used in the below example. 
Selenium is used to capture all the links & Java's HttpURLConnection class is used to check the link validity.

STEP 1: Open web page in any browser using selenium.
STEP 2: Capture all <img> & <a> elements with href.
STEP 3: Validate all the links using HttpURLConnection.


//STEP 1 & 2: Get all links from the given URL
public static List<WebElement> getAllLinks(String url){
WebDriver driver = null;
List<WebElement> elmList = new ArrayList<WebElement>();
List<WebElement> linksList = new ArrayList<WebElement>();

System.setProperty("webdriver.chrome.driver", "C:/foderpath/chromedriver.exe");
driver = new ChromeDriver();
driver.get(url);

elmList = driver.findElements(By.tagName("a"));
elmList.addAll(driver.findElements(By.tagName("img")));
for(WebElement elmnt : elmList){
   if (elmnt.getAttribute("href") != null)
   {
linksList.add(elmnt);
   }
}
return linksList;
}

//STEP 3: Validate the link health
public static void validateLink(List<WebElement> links){
int resp = 0;

for(WebElement elmnt : links){
String urllink = elmnt.getAttribute("href");
try {
URL url = new URL(urllink);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
// Get response code
resp = conn.getResponseCode();
conn.disconnect();

if(resp == 200)
System.out.println("Link is Working" + elmnt + "Response Code" + resp);
else
System.out.println("Link is Broken" + elmnt + "Response Code" + resp);

} catch (Exception e) {
e.printStackTrace();
}
}
}


Then call the above 2 methods in main.

public static void main(String[] args){
    String url = "https://www.google.co.in";

    List<WebElement> elmts = getAllLinks(url);
    validateLink(elmts);

}

No comments:

Post a Comment