NAME

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

VERSION

Version 1.14

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->content_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, "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.

CONSTRUCTOR

new

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

METHODS

$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.

$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->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->followable_links()

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 HTTP 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 HTTP 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 URL 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 HTTP 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 URL 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 HTTP 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 URL 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 URL 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 URL name.

    my @links = $mech->followable_links();
    $mech->link_content_unlike( \@links, qr/Restricted/,
      'No restricted links' );

Makes a follow_link() 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 follow_link(). Note that the parms to follow_link() 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, $comment 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 follow_link() is not available.

$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

Add HTML::Lint and HTML::Tidy capabilities.

AUTHOR

Andy Lester, <andy at petdance.com>

BUGS

Please report any bugs or feature requests to bug-test-www-mechanize at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-WWW-Mechanize. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

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

    perldoc Test::WWW::Mechanize

You can also look for information at:

ACKNOWLEDGEMENTS

Thanks to Michael Schwern, Mark Blackman, Mike O'Regan, Shawn Sorichetti, Chris Dolan, Matt Trout, MATSUNO Tokuhiro, and Pete Krawczyk for patches.

COPYRIGHT & LICENSE

Copyright 2004-2007 Andy Lester, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.