As a Test Automation Engineer, if you’re solely dedicated to writing test cases in Selenium you will not be performing to your true potential.
There are many things you could be doing to:
Help the team achieve excellence in your applications
Reduce testing time as much as possible
Help developers gain confidence in their changes knowing in a short time if their commits produced unintentional/unexpected alterations
One of those things will be explained and shared with working code on this Post… Validating all the links on your Website. There are a number of other helpful
Utils we could add to our Test Framework, and I will be discussing them in following posts, so make sure to Subscribe scrolling all the way down to the bottom of the page,
so you don’t miss them!.
The advantage of using your solution, instead of the many available online, such as the recommended W3C Link Checker,
is that you will be having everything in one place, which makes for a single entry point to run your tests, analyze links and other tools you can add, and have a single central
Report.
It will also allow you to navigate to and load a link in your browser with Selenium, which is more than what normal Link Checkers you find online will do (performing only HTTP calls).
What Are Broken Links
Broken links happen when your Web Browser can’t find the URL you’re looking for. In this context, we will one be looking at links within a valid website.
It can happen because:
A webpage was moved without updating its references
The URL structure of a website was changed
Missing/misconfigured properties files in the deployment
And attempting to navigate to them will send you to ugly 404 sites, and reduce the Website quality in Search Engine Optimization, which is
why it’s important to discover broken links before they’re deployed in Production.
Implementing a Simple Link Checker in Your Test Framework
First, let’s be clear on a few things we will need:
We want to be able to run it as part of our Automated tests, and we will use our Framework for that
The report will follow the format: URL that has the broken link -> Broken Link
We want to go through all the possible URLs within the same Host, for example, if your website contains links to other sites, we shouldn’t check their links
Check the links by doing a HEAD HTTP request instead of a GET to improve performance
Make sure we only check an URL once, so we need a List of previously checked URLs
I’ve created a package linkchecker inside the utils package.
Let’s take a look at a class that will be doing most of the heavy work. Remember you can access the entire code at my Github
From the above we have almost everything we need in place. The only thing missing is the implementation of Utilities.getUrlResponse(urlToScan).
Like we mentioned above, it will perform better if the use HEAD HTTP calls to the link instead of calling GET or attempting to navigate, so a nice implementation
of that method looks something like this:
With all that in place, we are ready to start using them in a Test, you could instantiate the LinkChecker class directly in your test, but it’s
probably a better decision to add that in the BasePage instead, where it’s available for others Test Automation Engineers if you’re working with a team.
Instantiation is quite simple:
And now you have a working link checker available as part of your regression tests ! Keep in mind this is one of the simplest versions available, if you’re
feeling adventurous I have a couple improvements in my head that you can implement:
Add a DEPTH variable so you can alter the max level of recursion
Include other HTML tags that can be processed with HTTP requests, for example img
That’s it for today’s post.
Thanks for reading, and happy testing,
Santiago.