Skip to main content

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();

  }
 }
}





Comments

Popular posts from this blog

How to read data from Excel using Column Name in Java Selenium

Java provides different API to Read and Write a File with help of Selenium. In this Post, we will learn how can we read and write from Excel file by using Java IO package,JXL and Apache POI library.             If your are using any Automation Framework you must know About Read and Write fro Excel, We have to use This Concept while working in Data Driven Framework . Step to Read Data from Excel Using Apache POI jar To Read or Write From Excel,Apache Community provides a very widely used library POI. This library have features to read and write both XLS and XLSX file format of Excel. HSSF implementation is provided by POI library to read 'XLS' Files and XSSF implementation of POI library to read 'XLSX' File ,Lets go to Deep Die ....... Types of Classes and InterFace in Apache POI    Excel Classes       Read Data from Excel File Download Apache POI jar - To Know more About Read Article  Go To Java Project  Right Click on Java Project Cr

How to read and write from Excel File in Selenium : Apache POI and JXL

Java provides different API to Read and Write a File with help of Selenium. In this Post, we will learn how can we read and write from Excel file by using Java IO package,JXL and Apache POI library.             If your are using any Automation Framework you must know About Read and Write fro Excel, We have to use This Concept while working in Data Driven Framework . Step to Read Data from Excel Using Apache POI jar To Read or Write From Excel,Apache Community provides a very widely used library POI. This library have features to read and write both XLS and XLSX file format of Excel. HSSF implementation is provided by POI library to read 'XLS' Files and XSSF implementation of POI library to read 'XLSX' File ,Lets go to Deep Die ....... For user who is using Maven Project can copy Dependency from here  org.apache.poi poi 3.17         For user Who is using Simple Java Project Can know How To Download Apache POI Jar and Add To JAVA Build

Scope of Automation Testing in Software Testing Industry

Scope of Automation Testing in Software Testing  Before To Know Scope of Automation testing we Should Know About Use of Automation Testing ,Usages of Automation testing : 1. To Avoid Repetitive Task we Use Automation Testing (For Ex- A manual Tester Test a Login Functionality Multiple Times in a day ) 2.Regression Testing (To ensue the previous functionalities have not affected with new deployed functionality ) ,If  your Application is Too big Then only Automation Test Script can ensure about Your Previous functionality Because Manually Tester can not Test previous functionality when application is so big . 3.Performance Testing is also a key reason of Existence of Automation Testing because performance of  Application is major concern in now days and performance can not be test manually ,Manual Tester can perform Load Test with Multiple Threads. Scope of Automation Testing : If You understand usage of Automation testing then you would able to easily kn