Day 12 of the 30 Day Writing Challenge:
Hello, I’m Truett Kueck and you’re not. Today I am going to expand on what I wrote about last week. It is amazingly common to forget to add urls to links. This is completely understandable. You are developing the page in sprints, and sometimes the page you need to link to does not exist yet. Multiple sprints later when that page exists it slips your mind and then BAM! a site is live with scores of non-functioning. It is also possible, and very common, that the actual requirements for links are unclear or completely non-existent.
To solve this problem I have developed a simple Ruby application that parses a page for any links and then generates a Selenium script that clicks through the links on the page. You will still need to make a few modifications, but you are 95% of the way there. It uses the mechanize gem, another of many web automation gems, so we will need to install that first.
Mechanize is actually completely capable of performing the functionality of the Selenium script, but it has one major disadvantage. The Selenium script can be run against Saucelab’s OnDemandService which gives you access to most every major OS/Browser combination. That might not matter to you as a developer, but Compatibility Testing is the bread and butter of QA. Now onto the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
The user is initially prompted to enter the url they would like to run against. This does require the url to be entered in http://www.url.com format, but you can add in error checking fairly easily. They are then asked what they would like to name the script. Then magic happens and we end up with a nearly ready-made Selenium script. Now we need to update the script to actually check that the links are working. The outputted code for a given link should look like:
1 2 3 4 5 6 7 8 9 10
The angry caps locked, “PUT PAGE TITLE HERE,” is our target. As I mentioned in the previous article: Selenium is capable of checking for things on the page other than the title. If the link goes to content that is hidden, you can check for specific text. Whichever you use replace that text with what you are looking for, and you are done. Now go fourth and fix broken links.