FindBrokenLink.java import java.util.ArrayList; import java.util.List; 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.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.utility.GetReponseCode; public class FindBrokenLink { WebDriver driver; // It will open the browser & Application before Test @BeforeTest public void openBrowser() { System.setProperty("webdriver.chrome.driver", "D://Drivers//chromedriver.exe"); driver = new ChromeDriver(); //To maximize the Window driver.manage().window().maximize(); //To Open the Application driver.get("http://www.oyepages.com"); } @AfterTest public void tearDown() { if (driver != null) driver.close(); } @Test public void getBrokenLinks() { // Find all Elements on the page with 'a' tag ListallLinks = driver.findElements(By.tagName("a")); System.out.println("Total Number of Links : " +allLinks.size()); //Iterate over all available Links on webpage for (WebElement link : allLinks) { String linkURL = link.getAttribute("href"); //Call VerifyURL form GetResponse Code Class GetReponseCode.verifyUrl(linkURL); } System.out.println("Total Number of Links : " +allLinks.size()); GetReponseCode.getInvalidLinkCount(); } } GetReponseCode.java import java.net.URL; import javax.net.ssl.HttpsURLConnection; public class GetReponseCode { static int inValidLink; public static void verifyUrl(String linkURL) { try { URL url = new URL(linkURL); HttpsURLConnection urlConnect = (HttpsURLConnection) url.openConnection(); // Set Connection TimeOut urlConnect.setConnectTimeout(5000); // Open Communication link to resource URL urlConnect.connect(); // To verify the Response Code if (urlConnect.getResponseCode() == 200) { System.out.println( linkURL + " : " + urlConnect.getResponseCode() + " : " + urlConnect.getResponseMessage()); } else if (urlConnect.getResponseCode() == urlConnect.HTTP_NOT_FOUND) { System.out .println(linkURL + " : " + urlConnect.getResponseMessage() + " : " + urlConnect.HTTP_NOT_FOUND); inValidLink++; } } catch (Exception e) { } } public static void getInvalidLinkCount() { System.out.println("Total Invalid Links : " + inValidLink); } }