How to find broken links on a web page?
package selenium;
import java.io.IOException;
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.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class LinkChecker {
WebDriver driver;
@BeforeClass
public void setUp(){
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//drivers//chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS);
driver.get("http://newtours.demoaut.com");
}
@Test
public void findBrokenLinks() throws IOException{
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
System.out.println("Total number of links present on the page is "+allLinks.size());
for(WebElement l1 : allLinks){
String testLink = l1.getAttribute("href");
URL testURL = new URL(testLink);
HttpURLConnection huc = (HttpURLConnection)testURL.openConnection();
huc.setConnectTimeout(5000);
huc.connect();
if(huc.getResponseCode() == 200){
System.out.println(l1.getText() +" -- "+huc.getResponseMessage());
System.out.println(testURL +" -- "+huc.getResponseMessage());
}else if(huc.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND){
System.out.println(l1.getText() +" -- "+huc.getResponseMessage());
System.out.println(testURL +" -- "+huc.getResponseMessage());
}
}
}
@AfterClass
public void tearDown(){
driver.quit();
}
}
import java.io.IOException;
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.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class LinkChecker {
WebDriver driver;
@BeforeClass
public void setUp(){
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//drivers//chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS);
driver.get("http://newtours.demoaut.com");
}
@Test
public void findBrokenLinks() throws IOException{
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
System.out.println("Total number of links present on the page is "+allLinks.size());
for(WebElement l1 : allLinks){
String testLink = l1.getAttribute("href");
URL testURL = new URL(testLink);
HttpURLConnection huc = (HttpURLConnection)testURL.openConnection();
huc.setConnectTimeout(5000);
huc.connect();
if(huc.getResponseCode() == 200){
System.out.println(l1.getText() +" -- "+huc.getResponseMessage());
System.out.println(testURL +" -- "+huc.getResponseMessage());
}else if(huc.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND){
System.out.println(l1.getText() +" -- "+huc.getResponseMessage());
System.out.println(testURL +" -- "+huc.getResponseMessage());
}
}
}
@AfterClass
public void tearDown(){
driver.quit();
}
}
Output:
======
Total number of links present on the page is 16
Home -- OK
http://newtours.demoaut.com/mercurywelcome.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
Flights -- OK
http://newtours.demoaut.com/mercuryreservation.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
Hotels -- OK
http://newtours.demoaut.com/mercuryunderconst.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
Car Rentals -- OK
http://newtours.demoaut.com/mercuryunderconst.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
Cruises -- OK
http://newtours.demoaut.com/mercurycruise.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
Destinations -- OK
http://newtours.demoaut.com/mercuryunderconst.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
Vacations -- OK
http://newtours.demoaut.com/mercuryunderconst.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
SIGN-ON -- OK
http://newtours.demoaut.com/mercurysignon.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
REGISTER -- OK
http://newtours.demoaut.com/mercuryregister.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
SUPPORT -- OK
http://newtours.demoaut.com/mercuryunderconst.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
CONTACT -- OK
http://newtours.demoaut.com/mercuryunderconst.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
your destination -- OK
http://newtours.demoaut.com/mercuryunderconst.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
featured vacation destinations -- OK
http://newtours.demoaut.com/mercuryunderconst.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
Register here -- OK
http://newtours.demoaut.com/mercuryregister.php?osCsid=0abb018c00f50b41e685e734eb37627c -- OK
Business Travel @ About.com -- Not Found
http://businesstravel.about.com/mbody.htm?PM=78_101_T&cob=home -- Not Found
PASSED: findBrokenLinks
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Excellent post with lots of information about Selenium testing Technology.
ReplyDeleteSelenium Training in Chennai