Search code examples
javatestingseleniumhttp-status-code-404url-validation

Check for valid links if a dead URL redirects to a costum error page


I need to test(Selenium) if the links in a given page is valid or not. I found a good post about it here

http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/

But the problem is, what if a error page redirects to a custom error page? Then i would get a 200 or a 302 instead of a 404. How should I go about checking the validity of URLs for webpages that redirect their 404s.


Solution

  • You should utilize an assertion of an element on the page with a known specific test. Use a specific reusable function for this.

    Then when you hit a page call the function as a check. If you find the specific element present then click the browser back button after recording the URL. If not you can continue your test as desired. There is another post in regards to recursively finding all links and testing them. How to browse a whole website using selenium?

        if (checkError()) //calls specific check for the error on the custom error page
        {
          //Log URL
          string badURL = driver.Url();
          //Save somewhere in a list for output later...
    
          //navigate to previous page
          driver.navigate().Back();
        }