Monday, August 29, 2011

Get all links and click on each one

One of the most tedious tasks, IMO, for a test engineer, is manually verifying links. We can automate most of the process and as long as we have the URL's for where we are expected to land after clicking the link, we can verify this functionality using Selenium and a little bit of JS.

In the below example we first navigate to our desired website and then use Selenium's getEval() function to execute JavaScript that gathers all the links on the page (anchors) and saves them in a comma separated list. This list then gets split and pushed into an array. We then iterate through the list of links in the array clicking on each one and then navigating back to the starting page using go_back.

  1. use strict;
  2. use warnings;
  3. use Time::HiRes qw(sleep);
  4. use Test::WWW::Selenium;
  5. use Test::More "no_plan";
  6.  
  7. my $sel = Test::WWW::Selenium->new( host => "localhost",
  8.                                     port => 4444,
  9.                                     browser => "*iexplore",
  10.                                     browser_url => "http://www.google.com/");
  11.  
  12. $sel->open_ok("/", "true");
  13.  
  14. $sel->set_speed("1000");
  15.  
  16. my $javascript = "var allLinks = this.browserbot.getCurrentWindow().document.getElementsByTagName('a');
  17.                  var separator = ',';
  18.                  var all_links_texts = '';
  19.                  
  20.                  for(var i = 0; i < allLinks.length; i++) {
  21.                        all_links_texts = all_links_texts+separator+allLinks[i].href;
  22.                  }
  23.                all_links_texts;";
  24.  
  25. # Get all of the links in the page and, using a comma to separate each one, add them to the all_links_texts var.
  26. my $link_list = $sel->get_eval($javascript);
  27.  
  28. my @link_array = split /,/ , $link_list;
  29.  
  30. my $count = 0;
  31.  
  32. # Click on each link contained in the array and then go_back
  33. # You can add other logic here like capture and store a screenshot for example
  34. foreach my $link_name (@link_array) {
  35.    
  36.     unless ($link_name =~ /^$/){
  37.        
  38.         $sel->click_ok("css=a[href $= $link_name]");
  39.        
  40.         $sel->wait_for_page_to_load("30000");
  41.        
  42.         print "Clicked Link href: $link_name \n";
  43.        
  44.         $sel->go_back();
  45.        
  46.         $count++;
  47.     }
  48. }
  49. print "Clicked $count URL's";
  50. pass;

This can be easily modified to do much more than just click on the links. And of course nothing beats a good pair of eyes on the intended landing pages for the links clicked. Implementing a similar solution in your organization might ease with the manual testing. Here is how I have done it in the past:
Not everything can be automated, but we can certainly make it much easier to review large amounts of links. The above logic can be easily extended to capture a screen shot and add it to a queue of "to be reviewed" images. These properly tagged [by the software] images are what you use in the final phase of the test; visual verification phase.

With this approach you'll know right away if a link is broken or not (assuming you update the logic above to also include this, again this example can be easily extended to include that functionality). As well you will have the capability of visually verifying the screen shots of the intended link landing pages.


No comments:

Post a Comment

Creative Commons License
VGP-Miami Web and Mobile Automation Blog by Alfred Vega is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.