Test::WWW::Mechanize - Testing-specific WWW::Mechanize subclass


Test-WWW-Mechanize documentation  | view source Contained in the Test-WWW-Mechanize distribution.

Index


NAME

Top

Test::WWW::Mechanize - Testing-specific WWW::Mechanize subclass

VERSION

Top

Version 1.32

SYNOPSIS

Top

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

Top

new( %args )

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

You can pass in autolint => 1 to make Test::WWW::Mechanize automatically run HTML::Lint after any of the following methods are called.

* get_ok()
* post_ok()
* submit_form_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 $mech->get_ok() only counts as one test in the test count. Both the main IO operation and the linting must pass for the entire test to pass.

METHODS: HTTP VERBS

Top

$mech->get_ok($url, [ \%LWP_options ,] $desc)

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 *_ok() functions, it returns true if the test passed, or false if not.

A default description of "GET $url" is used if none if provided.

$mech->head_ok($url, [ \%LWP_options ,] $desc)

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 *_ok() functions, it returns true if the test passed, or false if not.

A default description of "HEAD $url" is used if none if provided.

$mech->post_ok( $url, [ \%LWP_options ,] $desc )

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 *_ok() functions, it returns true if the test passed, or false if not.

A default description of "POST to $url" is used if none if provided.

$mech->put_ok( $url, [ \%LWP_options ,] $desc )

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 *_ok() functions, it returns true if the test passed, or false if not.

A default description of "PUT to $url" is used if none if provided.

$mech->submit_form_ok( \%parms [, $desc] )

Makes a submit_form() 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 submit_form(). Note that the parms to submit_form() 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.

click_ok( $button[, $desc] )

Clicks the button named by $button. An optional $desc can be given for the test.

METHODS: CONTENT CHECKING

Top

$mech->html_lint_ok( [$desc] )

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

Note that HTML::Lint must be installed for this to work. Otherwise, it will blow up.

$mech->title_is( $str [, $desc ] )

Tells if the title of the page is the given string.

    $mech->title_is( 'Invoice Summary' );

$mech->title_like( $regex [, $desc ] )

Tells if the title of the page matches the given regex.

    $mech->title_like( qr/Invoices for (.+)/

$mech->title_unlike( $regex [, $desc ] )

Tells if the title of the page matches the given regex.

    $mech->title_unlike( qr/Invoices for (.+)/

$mech->base_is( $str [, $desc ] )

Tells if the base of the page is the given string.

    $mech->base_is( 'http://example.com/' );

$mech->base_like( $regex [, $desc ] )

Tells if the base of the page matches the given regex.

    $mech->base_like( qr{http://example.com/index.php?PHPSESSID=(.+)});

$mech->base_unlike( $regex [, $desc ] )

Tells if the base of the page matches the given regex.

    $mech->base_unlike( qr{http://example.com/index.php?PHPSESSID=(.+)});

$mech->content_is( $str [, $desc ] )

Tells if the content of the page matches the given string

$mech->content_contains( $str [, $desc ] )

Tells if the content of the page contains $str.

$mech->content_lacks( $str [, $desc ] )

Tells if the content of the page lacks $str.

$mech->content_like( $regex [, $desc ] )

Tells if the content of the page matches $regex.

$mech->content_unlike( $regex [, $desc ] )

Tells if the content of the page does NOT match $regex.

$mech->text_contains( $str [, $desc ] )

Tells if the text form of the page's content contains $str.

When your page contains HTML which is difficult, unimportant, or unlikely to match over time as designers alter markup, use text_contains instead of content_contains.

 # <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 $mech->content(format => 'text'). See content in WWW::Mechanize.

$mech->text_like( $regex [, $desc ] )

Tells if the text form of the page's content matches $regex.

$mech->text_unlike( $regex [, $desc ] )

Tells if the text format of the page's content does NOT match $regex.

$mech->has_tag( $tag, $text [, $desc ] )

Tells if the page has a $tag tag with the given content in its text.

$mech->has_tag_like( $tag, $regex [, $desc ] )

Tells if the page has a $tag tag with the given content in its text.

$mech->stuff_inputs( [\%options] )

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 HTML).

TODO

Top

Add HTML::Tidy capabilities.

Add a broken image check.

AUTHOR

Top

Andy Lester, <andy at petdance.com>

BUGS

Top

Please report any bugs or feature requests to <http://code.google.com/p/www-mechanize/issues/list>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

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

    perldoc Test::WWW::Mechanize

You can also look for information at:

* Google Code bug tracker

http://code.google.com/p/www-mechanize/issues/list

Please do not use the old queues for WWW::Mechanize and Test::WWW::Mechanize at http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-WWW-Mechanize

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Test-WWW-Mechanize

* CPAN Ratings

http://cpanratings.perl.org/d/Test-WWW-Mechanize

* Search CPAN

http://search.cpan.org/dist/Test-WWW-Mechanize

ACKNOWLEDGEMENTS

Top

Thanks to Philip G. Potter, Niko Tyni, Greg Sheard, Michael Schwern, Mark Blackman, Mike O'Regan, Shawn Sorichetti, Chris Dolan, Matt Trout, MATSUNO Tokuhiro, and Pete Krawczyk for patches.

COPYRIGHT & LICENSE

Top


Test-WWW-Mechanize documentation  | view source Contained in the Test-WWW-Mechanize distribution.