设为首页 收藏本站
查看: 704|回复: 0

[经验分享] Automated Testing with Perl

[复制链接]

尚未签到

发表于 2017-5-17 11:43:00 | 显示全部楼层 |阅读模式
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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-378500-1-1.html 上篇帖子: Perl的语法-1 下篇帖子: Perl中的特殊变量
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表