HTTP::WebTest::Cookbook - Recipes of tests for every day
Not Applicable
This document contains some examples of HTTP::WebTest usage.
Unless otherwise is stated all examples are either runnable programs (see HTTP::WebTest::API) or runnable wtscript files (see perldoc wt).
This wtscript tests couple of static pages on my website:
test_name = First page url = http://martynov.org/ text_require = ( Ilya Martynov's Web Site ) end_test test_name = Mail-CheckUser page url = http://martynov.org/checkuser text_require = ( Mail-CheckUser Download ) regex_require = ( Mail-CheckUser-[\d\.]+\.tar\.gz ) end_test
Same test in form of Perl script:
use HTTP::WebTest; my $webtest = new HTTP::WebTest; $webtest->run_tests( [ { test_name => 'First page', url => 'http://martynov.org/', text_require => [ "Ilya Martynov's Web Site" ] }, { test_name => 'Mail-CheckUser page', url => 'http://martynov.org/checkuser', text_require => [ 'Mail-CheckUser', 'Download' ], regex_require => [ 'Mail-CheckUser-[\d\.]+\.tar\.gz' ] } ]);
This wtscript tests login form on http://fsck.com/rt2/:
test_name = Login page url = http://fsck.com/rt2/ text_require = ( Login Username: Password:) end_test test_name = Submit wrong username & password url = http://fsck.com/rt2/ params = ( user => unknownUser pass => somePassword ) text_require = ( Error Your username or password is incorrect ) end_test test_name = Submit correct username & password url = http://fsck.com/rt2/ params = ( user => guest pass => guest ) regex_require = ( Signed in as.*?guest.*?\. ) end_test
This wtscript tests couple of static pages on my website. It is similar to example in section "Check Static Website" but it uses test parameter click_link to specify link to be followed on next test request instead of hardcoded URI:
click_link
# load HTTP::WebTest::Plugin::Click module which provides test # parameter 'click_link' plugins = ( ::Click ) test_name = First page url = http://martynov.org/ text_require = ( Ilya Martynov's Web Site ) end_test test_name = Mail-CheckUser page click_link = Mail-CheckUser text_require = ( Mail-CheckUser Download ) regex_require = ( Mail-CheckUser-[\d\.]+\.tar\.gz ) end_test
This wtscript tests login form on http://fsck.com/rt2/. It is simular to example in section "Check Login Form" but avoids using hardcoded URL for page where form should be submitted to by using test parameter click_button:
click_button
# load HTTP::WebTest::Plugin::Click module which provides test # parameter 'click_button' plugins = ( ::Click ) test_name = Login page url = http://fsck.com/rt2/ text_require = ( Login Username: Password:) end_test test_name = Submit correct username & password click_button = Login params = ( user => guest pass => guest ) regex_require = ( Signed in as.*?guest.*?\. ) end_test
This Perl script reads test specification from file test.wt and generates Test::Harness compatible output:
test.wt
use HTTP::WebTest; my $webtest = new HTTP::WebTest; $webtest->run_wtscript('test.wt', { default_report => 'no', plugins => [ '::HarnessReport' ] });
It is possible to define new checks without writting new plugin module. Here fragment of wtscript file which checks if a new record have been inserted into a database as result of test request.
# load HTTP::WebTest::Plugin::Hooks module which provides test # parameter 'on_response' plugins = ( ::Hooks ) .... test_name = Add Record # request to this URL with parameter 'name' adds new record url = http://some.server/add-record params = ( name => 'John' ) # define check on_response = { # it is assumed that $Test::dbh is database handler my $has_record = $Test::dbh->selectrow_array( 'SELECT COUNT(*) FROM USERS ' . 'WHERE NAME = ?', undef, 'John' ); # return result of check with a comment [ $has_record > 0 ? 'yes' : 'no', 'Have got John' ]; } end_test
Sometimes it is required to feed the results of a previous test into the next test. For example, Add Record creates a database record, emits HTML containing the new record id, and Delete Record deletes the database record using the record id generated in Add Record.
Add Record
Delete Record
It is possible to use HTTP::WebTest to write such tests. Here incomplete example of wtscript which implements it.
# load HTTP::WebTest::Plugin::Hooks module which provides test # parameter on_response plugins = ( ::Hooks ) .... test_name = Add Record # request to this URL with parameter 'name' adds new record url = http://some.server/add-record params = ( name => 'John' ) # get ID from a page on_response = { # get webtest object my $webtest = shift; # find ID in the returned page ($ID) = $webtest->last_response->content =~ /ID=(\d+)/; # because no checks are defined a reference on empty array # must be returned []; } end_test .... test_name = Delete Record # request to this URL with parameter 'id' deletes record url = http://some.server/delete-record params = ( id => "$ID" ) end_test
Copyright (c) 2001,2002 Ilya Martynov. All rights reserved.
This module is free software. It may be used, redistributed and/or modified under the terms of the Perl Artistic License.
HTTP::WebTest
HTTP::WebTest::API
wt
To install HTTP::WebTest, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTTP::WebTest
CPAN shell
perl -MCPAN -e shell install HTTP::WebTest
For more information on module installation, please visit the detailed CPAN module installation guide.