Automated Testing with Perl

If you are a software tester or Quality Assurance Engineer testing any sort of product with a web interface and you aren’t using Perl to automate your testing, you are missing out. Perl is a simple, fast, powerful scripting language that has a ton of free modules available to ease your job as a tester.
I use it everyday. Working for a top 50 web company, I’m responsible for testing a website with several hundred pages and several hundred partner sites with their own layouts, color schemes, logos, links, etc. Doing all this testing manually is simply not an option. A simple change such as updating a logo on all sites can take several hours to test manually by hand. Using an automated script, I can perform this amount of testing in about one minute.
Let me tell you about my Perl toolkit, what packages I use, how I use them, and what they can offer you.
  1. LWP – LWP is Perl’s interface to the World Wide Web. It provides the tools you need, exposed through a variety of simple through complex interfaces, to test web sites.
    LWP::Simple allows the user to grab the content of any webpage with one line of code.

     

    $content = get('http://www.distinctquality.com/blog')

    Will take the HTML returned from this blog and put it into the $content variable.

    LWP::UserAgent allows you more detailed access to the LWP library. I use it to set UserAgents and modify HTTP headers before retrieving a web page. By changing the user agent, I can tell a website that my request is coming from a Blackberry device, a Firefox browser, an Internet Explorer 7 browser, a Treo, or a Motorola RAZR. If your product needs to serve different content based on the browser type, LWP::UserAgent is going to become your best friend.
  2. WWW::Mechanize – WWW::Mechanize allows me, through a few simple lines of code to check all the links on the page, check image properties, check attributes of objects, and populate forms for testing form submissions. I can build a quick script to grab every link on a page, fetch the URL, and verify the response is a success code. If I point this type of script at a thorough site map, I can quickly test the majority of my site’s pages in the blink of an eye. For adaptive regression testing, this is a invaluable tool.
  3. Test::More – Test::More will be the foundation of your test scripts. It allows you to quickly build a test “plan” for each script with the number of test cases and each test being evaluated as pass/fail. Its simple, but it supports test suites, allowing you to run a series of test scripts with a nice report at the end showing pass fail statistics and a list of which tests failed. Test::More allows you to run your tests in a simple manner by evaluating any statement to see if it is true or false.

    ($string) = ($content =~ /\<title\>(.*)\<\/title\>/);
    ok($string =~ /.+/, “Check for title”);

    This test will pass if $string contains anything between the title tags. This would allow you to quickly test for non-null titles on all of your pages.

Test Automation with Perl is a snap. If you haven’t used it yet, I encourage you to pick up a book on the subject or take a class at your local community college. Learning the basic syntax is a breeze and you’ll be automating large amounts of tests in no time! If you have used perl for test automation and know of other perl modules that are a great help in automating tests, please leave a comment to share with the rest of the readers!

====== Addendum 2/11/2008 ======

Kirk Brown over at About.com wrote a quick review about this blog post on http://perl.about.com. He mentioned it needed more good examples, so I’ve put together a *slightly* more advanced example using LWP::Simple and Test::More together to do some really basic web testing.


Here is the script:

------------------------------
#!/usr/bin/perl

use Test::More;
use LWP::Simple;
use strict;

plan tests => 4; # Tell Test::More that I'm planning 4 tests

# The URL I plan to test
my $url = 'http://www.msn.com';

# Uses LWP::Simple to 'get' the page content from the url
my $content = get($url); # gets HTML source via LWP::Simple

# LWP::Simple returns undefined if the request fails,
# lets test for success
ok(defined($content), "Get worked on $url");

# Lets check for the year being correct on the page by
# running a regex on the new $content variable
my ($msn_date) = ($content =~ /id="dl"\>.*?, .*? \d+, (\d+?)\<\/a\>/);

# Then we'll take the output of the regex from the page and
# make sure the year is right. If the test fails here, we may
# want input as to why it failed so we'll add a diag line if
# the test fails
ok($msn_date == 2008, "Date shows 2008")
        || diag "Date test failed, value was $msn_date";

# Lets make sure the Privacy link is there, legal usually gets
# upset if it is missing
ok($content =~ /\MSN Privacy\<\/a\>/, "Privacy Policy link exists");

# Then we'll run a quick test that we'll know the
# media folks will require
ok($content =~ /Britney Spears/, "Page contains Britney Spears story");

——————————
Here is the output:
——————————

> perl msntest.t
1..4
ok 1 – Get worked on http://www.msn.com
ok 2 – Date shows 2008
ok 3 – Page contains link to MSN Privacy Policy
not ok 4 – Page contains requisite Britney Spears story
# Failed test ‘Page contains requisite Britney Spears story’
# at msntest.t line 29.
# Looks like you failed 1 test of 4.

——————————

As you can see, its a simple script, but I hope it will give you some hints as to how you may want to go about using Perl to test for conditions in your own web pages.

你可能感兴趣的:(perl)