cL83
cL83

Reputation: 489

Selenium IDE: Verify broken link 404 not found?

Any one have any idea whether Selenium IDE able to verify any broken link in page??

Upvotes: 0

Views: 3138

Answers (4)

Nandhis
Nandhis

Reputation: 65

You can use 404 status code to verify using REST web services. Use gson and apache jars to get it.

public class Testing{

public static void main(String[] args) {
    try{
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.navigate().to("https://www.amazon.in/");
        List<WebElement> links = driver.findElements(By.tagName("a"));
        System.out.println("Number of links : " + links.size());
        for(WebElement e : links) {
            String linkText = e.getAttribute("href");
            System.out.println("Link -->>" +linkText);
            if(linkText!=null && !linkText.isEmpty()) {
                HttpPost post = new HttpPost(linkText);
                HttpClient client = HttpClientBuilder.create().build();
                HttpResponse res = client.execute(post);
                String s = res.getStatusLine().toString();
                if(s.contains("404")) {
                    System.out.println("Navigated");
                    //your code to handle logic 
                } else {
                    //your code to handle logic with other response code
                }
            }
        }
    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    }
}

}

Upvotes: 0

Bryce
Bryce

Reputation: 8742

In short, you can't (yet). See a more full writeup of the situation at: How do I ask Selenium IDE to check a HTTP Status Code (e.g. 2XX, 404, 500) Basically the selenium maintainers decided this function did not belong in selenium because error codes are read by machines not humans.

Upvotes: 1

datka
datka

Reputation: 880

This sounds like a job for a simple spider script, for example using the wget on Linux

--spider

   When invoked with this option, Wget will behave as a Web spider,
   which means that it will not download the pages, just check that
   they are there.  For example, you can use Wget to check your book‐
   marks:  

           wget --spider --force-html -i bookmarks.html

   This feature needs much more work for Wget to get close to the
   functionality of real web spiders.

Upvotes: 0

Silver Quettier
Silver Quettier

Reputation: 2050

Short answer: yes, but probably not the best idea.

Selenium might not be the best program to do this - especially if you use only Selenium IDE. Multiple programs exist with the sole purpose of testing broken links (do a little search), hell, even the W3C has a webapp for that.

That said, you could take the output from Selenium IDE formatted to your favourite language, program a Selenium test case with more advanced Unit Testing classes, include a while loop which would check for the existence of more links, and visit them / check headers, if you really want to do this through Selenium.

Upvotes: 1

Related Questions