1. Find all links using findElements() method.
2. Check each link response using java.net.HttpURLConnection class. (if page return 404 code - link is broken)
All these steps realized and described below:
[code lang="java"]
@Test
public void saveAllLinks(){
FirefoxDriver firefoxDriver = new FirefoxDriver();
// checking links on "http://docs.seleniumhq.org/download"
firefoxDriver.navigate().to("http://docs.seleniumhq.org/download");
// The "A" tag is a hyperlink tag need to find all these tags on page
List linksList = firefoxDriver.findElements(By.tagName("a"));
for(WebElement linkElement: linksList){
String link =linkElement.getAttribute("href");
if(link!=null){
if(!isLink(link)){
continue;
}
verifyLinkActive(link);
}
}
firefoxDriver.quit();
}
/**
* Methods verify some specific link attributes
* It can be used for find particular type links.
* @param link - link(URL)
* @return - true/false
*/
public boolean isLink(String link){
return link.contains("http://") && !link.contains("mailto");
}
public void verifyLinkActive(String linkUrl){
try {
URL url = new URL(linkUrl);
HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
httpURLConnect.setConnectTimeout(3000);
httpURLConnect.connect();
if(httpURLConnect.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND){
System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage()
+ " - "+ HttpURLConnection.HTTP_NOT_FOUND);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
[/code]
We used isList() method to add some tag attributes verification, it's possible to add some
search rules, for checkin specific links on a page
For example add
link.endsWith(".de") for verifiyng only link on German pagesMethod
verifyLinkActive() creates URL object using link, then open connection and cast it to HttpURLConnection.After connect we recive response code of the page and if it equals to
HttpURLConnection.HTTP_NOT_FOUND (404 code)we print out our broken link.
Full statistic about java, ryby, php, python development trends you can find on www.devtrends.net
The output of this test will give you somethis like:
[TestNG] Running: C:\Documents and Settings\User\.IdeaIC12\system\temp-testng-customsuite.xml http://www.gorillalogic.com/forums/flexmonkium - Not Found - 404 =============================================== Custom suite Total tests run: 1, Failures: 0, Skips: 0 ===============================================
Please let me know, how to get the link titles using the code above
ReplyDelete