HTTP-WebTest
view release on metacpan or search on metacpan
lib/HTTP/WebTest/Cookbook.pod view on Meta::CPAN
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 file tests the login form at http://fsck.com/rt2/. It is similar
to the example in section L<Check Login Form> but avoids using a hardcoded
URL for the page the form should be submitted to by using the test parameter
C<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
=head1 ADVANCED
=head2 Test::Harness Compatible Output
This Perl script reads a test specification from file C<test.wt> and
generates L<Test::Harness|Test::Harness> compatible output:
use Test::More qw(no_plan);
use HTTP::WebTest;
my $webtest = new HTTP::WebTest;
$webtest->run_wtscript('test.wt',
{
default_report => 'no',
plugins => [ '::HarnessReport' ]
});
This script uses reporting plugin
L<HTTP::WebTest::Plugin::HarnessReport|HTTP::WebTest::Plugin::HarnessReport>
which internally uses L<Test::Builder|Test::Builder> module to
generate L<Test::Harness|Test::Harness> compatible output. It should
be compatible with other testing libraries built using
L<Test::Builder|Test::Builder> (like L<Test::More|Test::More> or
L<Test::Differences|Test::Differences>) so you can freely intermix
them in one test script.
=head2 User-Defined Tests
It is possible to define new tests without writing new plugin
module. This is a fragment of a wtscript file that checks if a new record
has been inserted into a database as a result of the Add Record test.
# load HTTP::WebTest::Plugin::Hooks module which provides test
# parameters 'on_start', 'on_finish' and 'on_response'
plugins = ( ::Hooks )
on_start = {
# initialize a database handle used later in the tests
require DBI;
$dbh = DBI->connect('dbi:mysql:test', 'login', 'password');
}
on_finish = {
# disconnect from the database
$dbh->disconnect;
}
....
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 = {
my $has_record = $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
=head2 Dynamic Tests
Sometimes you want to feed the results of a previous test into
the next test. In this example, C<Add Record> creates a database record,
emits HTML containing the new record ID, and C<Delete Record> deletes
the database record using the record ID from C<Add Record>.
# 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' )
( run in 0.560 second using v1.01-cache-2.11-cpan-39bf76dae61 )