QaMantra: How to Find broken Links in Selenium

How to Find broken Links in Selenium

Link 

A link is an HTML object that allows you to jump to a new webpage when you click or tap it. Links means of navigating between pages on the web.

Broken Link 

A broken link is also called a dead link ,broken link or dead link is a link on a web page that no longer works because the website is down or page does not Exist ,When a broken link is clicked, an error message is displayed.


Reason for Broken Links 

  • 404  Page Not Found  : Destination Web Page has been Removed by owner 
  • 400 Bad Request  : The server cannot or will not process the request due to wrong requested url Address .
  • The use has restricted due to some firewall settings, firewall have blocked access to the Destination web Page
  • Misspellings in the link



How To Find Broken Links Through Script in Selenium

Manual checking of All links is a hectic work, because each webpage may have a thousands of links & manual process has to be repeated for all pages, Selenium Script will Automate process of Link Verification .


Step to Boken Link Verification
  • Collect all the links of web page on the basis of tag <a> .
  • Send HTTP request for each link .
  • verify HTTP response code for each link.
  • Find out whether the link is valid or broken based on HTTP response code.
  • It should be repeated for all.
Program

package com.abof.scripts;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class JavaProgram {
 /**
  * 
  * @author QaMantra
  */

 @Test
 public void main() throws InterruptedException {

  System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\workspace\\Abof\\ExeFile\\chromedriver.exe");
  WebDriver driver = new ChromeDriver();

  driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

  driver.get("https://www.Qamantra.com/");

  Thread.sleep(5000);
// Find Element Based on Tag <a> and Store as List of WebElement  List<WebElement> links = driver.findElements(By.tagName("a"));

System.out.println("Total links are " + links.size());

// used for loop to iterate to each WebElement of Link
  for (int i = 0; i < links.size(); i++) {
   WebElement element = links.get(i);

       // By using "href" attribute, we could get the url of the //requried Link
   
  String url = element.getAttribute("href");

// calling verifyLink() method here.Which is created below
   verifyLink(url);
  }
 }

  // verifyLink(String urlLink)Function verify broken links and //return the server response status.

 public void verifyLink(String urlLink) {
  try {

      // create object of the URL Class and pass the urlLink as //parameter
   URL link = new URL(urlLink);

// Create a connection using URL object (i.e., link)
  HttpURLConnection httpConn = (HttpURLConnection) link.openConnection();

   // Set the timeout for 3 seconds
   httpConn.setConnectTimeout(3000);

   // connect using connect method
   httpConn.connect();

 // use getResponseCode() to get the response code.
   if (httpConn.getResponseCode() == 200) {
System.out.println(urlLink + " - " + httpConn.getResponseMessage());
 System.out.println(httpConn.getResponseMessage());

   } else {
System.out.println(urlLink + " - " + httpConn.getResponseMessage());
System.out.println(httpConn.getResponseMessage());
   }

  }

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

  }
 }
}





No comments: