0

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.

1

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.

  • Jmeter works really well in this respect. Personally, i prefer to use Jmeter to just record an entire user session and then playback instead of random link checking. – djangofan Jan 24 '15 at 4:33
1

I've has success using LinkChecker to find all broken (404) links.

0

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.

0

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.

  • Hi Dan, I tried to implement you code but I am facing few compilation errors such as java.lang.Error: Unresolved compilation problems: agentDirectory cannot be resolved logger cannot be resolved HttpURLConnectUtil cannot be resolved Please suggest me which java file should I import. – Rajesh Rathod Jan 23 '15 at 9:40
  • Sorry I copied this out of and existing test. This part here agentDirectory.generateAgentSiteMapLinkElements(); is a method that returns the List<WebElement> collection. The HttpURLConnectUtil is a custom class. Let me see if I can tweak this a bit to get it a little more usable. – Dan Snell Jan 23 '15 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.