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.
-
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.
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.
分享到:
相关推荐
In addition, more and more software projects are embracing continuous integration and including an automated testing phase, as release cycles are shortening and thorough manual testing of ...
Tellurium Automated Testing Framework2Tellurium Automated Testing Framework2
Tellurium Automated Testing Framework¶Tellurium Automated Testing Framework¶
Tellurium Automated Testing Framework3Tellurium Automated Testing Framework3
Efficiently automate test cases for Andriod applications using Robotium Automation testing on mobile devices has been around for a number of years, although it has really taken off with the advent of...
自动化测试在SAP解决方案管理器与集成测试自动化工具中的应用 一、自动化测试的重要性与SAP的解决方案 在当今快速发展的IT行业中,自动化测试已成为确保软件质量和效率的关键环节。传统手动测试方法面临着时间紧迫...
This is a step-by-step, ... It's assumed that you have some experience in Android development, as well be familiar with the Android test framework, as Robotium is a wrapper to Android test framework.
A GOOD BOOK ABOUT AUTOMATED TESTING
application note about the nature of jitter and jitter testing. This was due to the fact that we identified this topic as the one that all engineers that in the one or other way have responsibility ...
本书《Automated Software Testing: Introduction, Management, and Performance》详细介绍了自动化测试的基础概念、实施策略以及如何评估其效果。 #### 知识点二:自动化测试的重要性 自动化测试在现代软件开发...
Selenium WebDriver is a popular automated testing tool for web applications. Python is one of the top programming languages and when used with Selenium it can automate and test web applications. Using...
《Automated testing using Unix Shell Scripting》一文由Prasad Patwa和Aniruddha Patwardhan共同撰写,旨在介绍在Unix环境下使用Shell脚本进行自动化测试的优势、方法及注意事项。 ### Unix Shell脚本自动化测试...
At the end of the book, you will use an automated technique called fuzzing to be able to identify flaws in a web application. Finally, you will understand the web application vulnerabilities and the ...
“Web Penetration Testing with Kali Linux Third Edition” 资源转载自网络,如有侵权,请联系上传者或csdn删除 。 Table of Contents Introduction to Penetration Testing and Web Applications Setting Up Your ...