Consider I am having hyperlinks on a webpage. How can I find which links are working and whichare not?
Kindly suggest me how should I track this using Selenium IDE or Java code.
I don't think that it can easily be done via Selenium IDE or even WebDriver Java code.
You can use i.e. Xenu's Link Sleuth free application to detect broken links.
Another option is using Apache JMeter, it's HTML Link Parser allows to implement something like site (or even web) spider in just few clicks
If you don't want spider and just need to test links on a single page your situation can be also handled as it described in Using Regular Expressions in JMeter guide.
If you want to check all the links on a page using selenium if so the first you need to click and you need to record first. And you can check with assertLocation. It was broken or not.But I want to give you some advice if you have many tests if so please use webdriver. Because IDE have many limitation.
"assertLocation" Command This Command will check and compare targeted URL with absolute URL of the current selected page and will return error message if both URLs not match. If "assertLocation" Command fails then selenium will stop execution of sript. In above example, 1st "assertLocation" Command will return error message so that next command will be not executed.
The approach I took in was to use Selenium Webdriver to collect all of the links on a given page. I then used a simple http request to get the response and check it. So it looks something like this, this code may not work exactly since I pulled it out of my own tests and tried to customize it.
Test:
public void linkChecker test()
{
List<String> incorrectLinks = new ArrayList<String>();
List<WebElement> agentLinks = driver.findElements(By.cssSelector("section
div[class='line'] ul li a")); //use your own selector here.
//You will need to make sure this references your driver object as part of the test.
logger.info(agentLinks.size() + " agent links to check.");
for(WebElement link : agentLinks)
{
String url = link.getAttribute("href");
//logger.info("Checking: " + url );
int responseCode = sendGetToReturnResponseCode(url,"");
if(responseCode == 404)
incorrectLinks.add(url);
}
}
HTTPResponse call;
public static int sendGetToReturnResponseCode(String server, String query){
int responseCode = 0;
try {
// Connect to the server
URL u = new URL(server + query);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "USER_AGENT");
responseCode = connection.getResponseCode();
}catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseCode;
}
The advantage of this approach in my opinion is that I can do the link check with out needing to interact with the browser and actually navigate to the page so it is quite fast.