Automated Testing with Perl
- 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. - 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.
- 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.
====== 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+?)/);
# 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/, “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.
Related Posts:
Performance Testing with jMeter
Your test suite is not flexible - get agile with FIT
Easy cache for your perl apps!
April 4th, 2007 at 6:36 pm
It’s a good article.
I am conversant with Perl and the LWP module. I did not happen to use the other two modules. As a part of your testing efforts, did you build a complete framework, or relied on adhoc scripting. In either case, if you can share some further information, it will be of good guidance, which I can put to work. It would be great if you could share some sample scripts. In addition to CPAN, can you suggest some other good references for Testing using Perl?
Regards,
Rahul Verma.
December 18th, 2007 at 5:44 am
Hi Rahul,
I found that by using Test::More, I did not need to build a framework myself. I can create a series of test scripts that use Test::More, then roll up the results of those tests in an automated email or on some sort of web page that reports test status. If you’d like to roll up results, you can use the Test-Harness module on cpan.
As for specific script examples, I’ll work on that. I think it’d be a great addition to the blog. Thanks!
February 9th, 2008 at 4:00 pm
The definitive Perl testing book is “Perl Testing: A Developer’s Notebook”
http://www.oreilly.com/catalog/perltestingadn/
February 10th, 2008 at 4:03 am
Just want to call out how cool it is that Andy Lester (PETDANCE) posted a comment on my blog. For those who don’t know, Andy is the author of WWW::Mechanize, HTML::Lint and a ton of other great Perl modules.
See his work at:
http://cpan.uwinnipeg.ca/~petdance
July 21st, 2008 at 10:39 am
It is a simple and neat article for anyone wants to get the power of Perl. How about TAP::Harness? Wont that be useful here ? I feel a great deal is there for a good documentation on building a frame work