VERSION

Version 1.42

SYNOPSIS

Test::WWW::Mechanize is a subclass of WWW::Mechanize that incorporates features for web application testing. For example:

    use Test::More tests => 5;
    use Test::WWW::Mechanize;

    my $mech = Test::WWW::Mechanize->new;
    $mech->get_ok( $page );
    $mech->base_is( 'http://petdance.com/', 'Proper <BASE HREF>' );
    $mech->title_is( 'Invoice Status', "Make sure we're on the invoice page" );
    $mech->text_contains( 'Andy Lester', 'My name somewhere' );
    $mech->content_like( qr/(cpan|perl)\.org/, 'Link to perl.org or CPAN' );

This is equivalent to:

use Test::More tests => 5; use WWW::Mechanize;

my $mech = WWW::Mechanize->new; $mech->get( $page ); ok( $mech->success ); is( $mech->base, 'http://petdance.com', 'Proper <BASE HREF>' ); is( $mech->title, 'Invoice Status', "Make sure we're on the invoice page" ); ok( index( $mech->content( format => 'text' ), 'Andy Lester' ) >= 0, 'My name somewhere' ); like( $mech->content, qr/(cpan|perl)\.org/, 'Link to perl.org or CPAN' );

but has nicer diagnostics if they fail.

Default descriptions will be supplied for most methods if you omit them. e.g.

my $mech = Test::WWW::Mechanize->new; $mech->get_ok( 'http://petdance.com/' ); $mech->base_is( 'http://petdance.com/' ); $mech->title_is( 'Invoice Status' ); $mech->content_contains( 'Andy Lester' ); $mech->content_like( qr/(cpan|perl)\.org/ );

results in

ok - Got 'http://petdance.com/' ok ok - Base is 'http://petdance.com/' ok - Title is 'Invoice Status' ok - Text contains 'Andy Lester' ok - Content is like '(?-xism:(cpan|perl)\.org)'

CONSTRUCTOR

Behaves like, and calls, WWW::Mechanize's \*(C`new\*(C' method. Any parms passed in get passed to WWW::Mechanize's constructor.

You can pass in \*(C`autolint => 1\*(C' to make Test::WWW::Mechanize automatically run HTML::Lint after any of the following methods are called. You can also pass in an HTML::Lint object like this:

my $lint = HTML::Lint->new( only_types => HTML::Lint::Error::STRUCTURE ); my $mech = Test::WWW::Mechanize->new( autolint => $lint );

  • get_ok()

  • post_ok()

  • submit_form_ok()

  • follow_link_ok()

  • click_ok()

This means you no longer have to do the following:

my $mech = Test::WWW::Mechanize->new(); $mech->get_ok( $url, 'Fetch the intro page' ); $mech->html_lint_ok( 'Intro page looks OK' );

and can simply do

my $mech = Test::WWW::Mechanize->new( autolint => 1 ); $mech->get_ok( $url, 'Fetch the intro page' );

The \*(C`$mech->get_ok()\*(C' only counts as one test in the test count. Both the main \s-1IO\s0 operation and the linting must pass for the entire test to pass.

You can control autolint on the fly with the \*(C`autolint\*(C' method.

METHODS: HTTP VERBS

A wrapper around WWW::Mechanize's get(), with similar options, except the second argument needs to be a hash reference, not a hash. Like well-behaved \*(C`*_ok()\*(C' functions, it returns true if the test passed, or false if not.

A default description of \*(L"\s-1GET\s0 $url\*(R" is used if none if provided. A wrapper around WWW::Mechanize's head(), with similar options, except the second argument needs to be a hash reference, not a hash. Like well-behaved \*(C`*_ok()\*(C' functions, it returns true if the test passed, or false if not.

A default description of \*(L"\s-1HEAD\s0 $url\*(R" is used if none if provided. A wrapper around WWW::Mechanize's post(), with similar options, except the second argument needs to be a hash reference, not a hash. Like well-behaved \*(C`*_ok()\*(C' functions, it returns true if the test passed, or false if not.

A default description of \*(L"\s-1POST\s0 to $url\*(R" is used if none if provided. A wrapper around WWW::Mechanize's put(), with similar options, except the second argument needs to be a hash reference, not a hash. Like well-behaved \*(C`*_ok()\*(C' functions, it returns true if the test passed, or false if not.

A default description of \*(L"\s-1PUT\s0 to $url\*(R" is used if none if provided. Makes a \*(C`submit_form()\*(C' call and executes tests on the results. The form must be found, and then submitted successfully. Otherwise, this test fails.

%parms is a hashref containing the parms to pass to \*(C`submit_form()\*(C'. Note that the parms to \*(C`submit_form()\*(C' are a hash whereas the parms to this function are a hashref. You have to call this function like:

$mech->submit_form_ok( { form_number => 3, fields => { answer => 42 }, }, 'now we just need the question' );

As with other test functions, $desc is optional. If it is supplied then it will display when running the test harness in verbose mode.

Returns true value if the specified link was found and followed successfully. The HTTP::Response object returned by submit_form() is not available. Makes a \*(C`follow_link()\*(C' call and executes tests on the results. The link must be found, and then followed successfully. Otherwise, this test fails.

%parms is a hashref containing the parms to pass to \*(C`follow_link()\*(C'. Note that the parms to \*(C`follow_link()\*(C' are a hash whereas the parms to this function are a hashref. You have to call this function like:

$mech->follow_link_ok( {n=>3}, 'looking for 3rd link' );

As with other test functions, $desc is optional. If it is supplied then it will display when running the test harness in verbose mode.

Returns a true value if the specified link was found and followed successfully. The HTTP::Response object returned by follow_link() is not available. Clicks the button named by $button. An optional $desc can be given for the test.

METHODS: CONTENT CHECKING

Checks the validity of the \s-1HTML\s0 on the current page. If the page is not \s-1HTML\s0, then it fails. The \s-1URI\s0 is automatically appended to the $desc.

Note that HTML::Lint must be installed for this to work. Otherwise, it will blow up. Tells if the title of the page is the given string.

$mech->title_is( 'Invoice Summary' ); Tells if the title of the page matches the given regex.

$mech->title_like( qr/Invoices for (.+)/ Tells if the title of the page matches the given regex.

$mech->title_unlike( qr/Invoices for (.+)/ Tells if the base of the page is the given string.

$mech->base_is( 'http://example.com/' ); Tells if the base of the page matches the given regex.

$mech->base_like( qr{http://example.com/index.php?PHPSESSID=(.+)}); Tells if the base of the page matches the given regex.

$mech->base_unlike( qr{http://example.com/index.php?PHPSESSID=(.+)}); Tells if the content of the page matches the given string Tells if the content of the page contains $str. Tells if the content of the page lacks $str. Tells if the content of the page matches $regex. Tells if the content of the page does \s-1NOT\s0 match $regex. Tells if the text form of the page's content contains $str.

When your page contains \s-1HTML\s0 which is difficult, unimportant, or unlikely to match over time as designers alter markup, use \*(C`text_contains\*(C' instead of \*(L"content_contains\*(R".

# <b>Hi, <i><a href="some/path">User</a></i>!</b> $mech->content_contains('Hi, User'); # Fails. $mech->text_contains('Hi, User'); # Passes.

Text is determined by calling \*(C`$mech->text()\*(C'. See \*(L"content\*(R" in WWW::Mechanize. Tells if the text of the page lacks $str. Tells if the text form of the page's content matches $regex. Tells if the text format of the page's content does \s-1NOT\s0 match $regex. Tells if the page has a $tag tag with the given content in its text. Tells if the page has a $tag tag with the given content in its text. Returns a list of links that Mech can follow. This is only http and https links. Follow all links on the current page and test for \s-1HTTP\s0 status 200

$mech->page_links_ok('Check all links'); Follow all links on the current page and test their contents for $regex.

$mech->page_links_content_like( qr/foo/, 'Check all links contain "foo"' ); Follow all links on the current page and test their contents do not contain the specified regex.

$mech->page_links_content_unlike(qr/Restricted/, 'Check all links do not contain Restricted'); Follow specified links on the current page and test for \s-1HTTP\s0 status 200. The links may be specified as a reference to an array containing WWW::Mechanize::Link objects, an array of URLs, or a scalar \s-1URL\s0 name.

my @links = $mech->find_all_links( url_regex => qr/cnn\.com$/ ); $mech->links_ok( \@links, 'Check all links for cnn.com' );

my @links = qw( index.html search.html about.html ); $mech->links_ok( \@links, 'Check main links' );

$mech->links_ok( 'index.html', 'Check link to index' ); Follow specified links on the current page and test for \s-1HTTP\s0 status passed. The links may be specified as a reference to an array containing WWW::Mechanize::Link objects, an array of URLs, or a scalar \s-1URL\s0 name.

my @links = $mech->followable_links(); $mech->link_status_is( \@links, 403, 'Check all links are restricted' ); Follow specified links on the current page and test for \s-1HTTP\s0 status passed. The links may be specified as a reference to an array containing WWW::Mechanize::Link objects, an array of URLs, or a scalar \s-1URL\s0 name.

my @links = $mech->followable_links(); $mech->link_status_isnt( \@links, 404, 'Check all links are not 404' ); Follow specified links on the current page and test the resulting content of each against $regex. The links may be specified as a reference to an array containing WWW::Mechanize::Link objects, an array of URLs, or a scalar \s-1URL\s0 name.

my @links = $mech->followable_links(); $mech->link_content_like( \@links, qr/Restricted/, 'Check all links are restricted' ); Follow specified links on the current page and test that the resulting content of each does not match $regex. The links may be specified as a reference to an array containing WWW::Mechanize::Link objects, an array of URLs, or a scalar \s-1URL\s0 name.

my @links = $mech->followable_links(); $mech->link_content_unlike( \@links, qr/Restricted/, 'No restricted links' ); Finds all free-text input fields (text, textarea, and password) in the current form and fills them to their maximum length in hopes of finding application code that can't handle it. Fields with no maximum length and all textarea fields are set to 66000 bytes, which will often be enough to overflow the data's eventual recepticle.

There is no return value.

If there is no current form then nothing is done.

The hashref $options can contain the following keys:

  • ignore hash value is arrayref of field names to not touch, e.g.: $mech->stuff_inputs( { ignore => [qw( specialfield1 specialfield2 )], } );

  • fill hash value is default string to use when stuffing fields. Copies of the string are repeated up to the max length of each field. E.g.: $mech->stuff_inputs( { fill => '@' # stuff all fields with something easy to recognize } );

  • specs hash value is arrayref of hashrefs with which you can pass detailed instructions about how to stuff a given field. E.g.: $mech->stuff_inputs( { specs=>{ # Some fields are datatype-constrained. It's most common to # want the field stuffed with valid data. widget_quantity => { fill=>'9' }, notes => { maxlength=>2000 }, } } ); The specs allowed are fill (use this fill for the field rather than the default) and maxlength (use this as the field's maxlength instead of any maxlength specified in the \s-1HTML\s0). Executes a test to make sure that the current form content has no text input fields that lack the \*(C`maxlength\*(C' attribute, and that each \*(C`maxlength\*(C' value is a positive integer. The test fails if the current form has such a field, and succeeds otherwise.

Returns an array containing all text input fields in the current form that do not specify a maximum input length. Fields for which the concept of input length is irrelevant, and controls that \s-1HTML\s0 does not allow to be capped (e.g. textarea) are ignored.

The inputs in the returned array are descended from HTML::Form::Input.

The return is true if the test succeeded, false otherwise.

METHODS: MISCELLANEOUS

Without an argument, this method returns a true or false value indicating whether autolint is active.

When passed an argument, autolint is turned on or off depending on whether the argument is true or false, and the previous autolint status is returned. As with the autolint option of \*(C`new\*(C', $status can be an HTML::Lint object.

If autolint is currently using an HTML::Lint object you provided, the return is that object, so you can change and exactly restore autolint status:

my $old_status = $mech->autolint( 0 ); ... operations that should not be linted ... $mech->autolint( $old_status ); grep_inputs() returns an array of all the input controls in the current form whose properties match all of the regexes in $properties. The controls returned are all descended from HTML::Form::Input.

If $properties is undef or empty then all inputs will be returned.

If there is no current page, there is no form on the current page, or there are no submit controls in the current form then the return will be an empty array.

# get all text controls whose names begin with "customer" my @customer_text_inputs = $mech->grep_inputs( { type => qr/^(text|textarea)$/, name => qr/^customer/ } ); grep_submits() does the same thing as grep_inputs() except that it only returns controls that are submit controls, ignoring other types of input controls like text and checkboxes. Returns an array of strings, each string the text surrounded by an element with attribute $attr of value $value. You can also pass in a regular expression. If nothing is found the return is an empty list. In scalar context the return is the first string found.

If passed, $html is scraped instead of the current page's content. Finds all elements with the given id attribute and pulls out the text that that element encloses.

In list context, returns a list of all strings found. In scalar context, returns the first one found.

If $html is not provided then the current content is used. Scrapes the current page for given \s-1ID\s0 and tests that it matches the expected value.

TODO

Add HTML::Tidy capabilities.

Other ideas for features are at https://github.com/petdance/test-www-mechanize

AUTHOR

Andy Lester, \*(C`<andy at petdance.com>\*(C'

BUGS

Please report any bugs or feature requests to <https://github.com/petdance/test-www-mechanize>.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Test::WWW::Mechanize

You can also look for information at:

  • Bug tracker https://github.com/petdance/test-www-mechanize <https://github.com/petdance/test-www-mechanize>

  • AnnoCPAN: Annotated \s-1CPAN\s0 documentation http://annocpan.org/dist/Test-WWW-Mechanize <http://annocpan.org/dist/Test-WWW-Mechanize>

  • \s-1CPAN\s0 Ratings http://cpanratings.perl.org/d/Test-WWW-Mechanize <http://cpanratings.perl.org/d/Test-WWW-Mechanize>

  • Search \s-1CPAN\s0 http://search.cpan.org/dist/Test-WWW-Mechanize <http://search.cpan.org/dist/Test-WWW-Mechanize>

ACKNOWLEDGEMENTS

Thanks to Jonathan \*(L"Duke\*(R" Leto, Philip G. Potter, Niko Tyni, Greg Sheard, Michael Schwern, Mark Blackman, Mike O'Regan, Shawn Sorichetti, Chris Dolan, Matt Trout, \s-1MATSUNO\s0 Tokuhiro, and Pete Krawczyk for patches.

COPYRIGHT & LICENSE

Copyright 2004-2012 Andy Lester.

This library is free software; you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.