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.


Wednesday, August 10, 2011

Tester by association

Are you often referred to by other teams in your organization as a "tester"? How do you feel about companies using this term in actual job titles? For example QA Tester, Software Tester, and my all time favorite Automation Tester.

That last one really is the icing on the cake. I thought the "tester" in any automation initiative was a Personal Computer or a server? Since when do we need an Automation Tester? Is this person testing the automation tests? I must admit, I am baffled by this one.

Playing devil's advocate, I posted a similar question on a few user groups and lists. The general consensus i received was that the word "tester" as a title is really not a good fit. Unless of course the sole function of this person is to test. In which case it is fitting. But other than that, "testing" is only one aspect of QA as well as just one function (out of many) that a QA / Test team member must perform.

This is not the case in most situations, however, and all personnel in the Quality Assurance department get labeled "tester" by association. Just the fact that they are a part of the QA department makes them a "tester". To me this is just one more proof that we are not doing a good enough job at getting the word out on what a person in our profession actually does.

How do you feel about being called a "tester" by association?

Sunday, August 7, 2011

Procedural or Object Oriented? Which one should a Test Engineer learn first?

I just read a discussion thread on LinkedIn where this very same topic was being bounced around. The sentiment of the commentators, until I arrived that is <evil grin>, was that it would be easier for a Test Engineer to make a transition from Object Oriented programming (i.e. C++) to procedural programming (Ruby, Python, Perl). I challenge that thought and believe that learning a procedural language before indulging in the OO world would be far more beneficial.

Further I'd argue that a person that learns a procedural language before diving into OO would have a much easier time learning OO principles since they would already be familiar with at least one part of the equation (i.e. procedures).

Procedural languages are actually a very good fit for automation. Since tests are usually a set of procedures performed with test data that is usually external (if you want to achieve any kind of repetition in your tests that is), tests can be designed with ease.

And further, further more :) you can design your tests using an object oriented approach in Ruby, Python or Perl see below links:

Perl: http://perldoc.perl.org/perltoot.html
Ruby: http://www.ruby-lang.org/en/
Python: http://docs.python.org/tutorial/

That's not to say that you cannot do any of these things or learn the same principles in C++, but the question is, why would you? All the tools you'd be using already support the same Object Oriented principles that in the future you'll be able to apply when diving into C++ if you so desire.
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.