Mechanize(3pm)
π NAME π
okay so this is the Mechanize(3pm) page from User Contributed Perl Documentation, and the star of the show is:
Test::WWW::Mechanize β it's a Testing-specific subclass of WWW::Mechanize. bestie. it's literally built for testing web apps. π
β¨ VERSION β¨
Version 1.60 π«Ά
πΈ SYNOPSIS πΈ
Test::WWW::Mechanize is a subclass of WWW::Mechanize that incorporates features for web application testing. like, 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' );
and that whole thing is equivalent to writing this by hand:
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 β and this is the iconic part β it has nicer diagnostics if they fail. π no more squinting at unhelpful failure output.
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)'
look at those lil auto-descriptions doing the absolute most. π
π§Έ CONSTRUCTOR π§Έ
new( %args )
Behaves like β and literally calls β WWW::Mechanize's "new" method. Any parms you pass in get passed straight through 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. 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 );
The same is also possible with "autotidy => 1" to use HTML::Tidy5.
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 "$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. no skipping out early, bestie. π
You can control autolint and autotidy on the fly with the "autolint" and "autotidy" methods. π
π₯ METHODS: HTTP VERBS π₯
$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.
NOTE π Due to compatibility reasons it is not possible to pass additionalLWP_optionsbeyond form data via this method (such asContentorContent-Type). It is recommend that you useWWW::Mechanize'spost()directly for instances where more granular control of the post is needed. fr fr, go direct if you need the fancy stuff.
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->delete_ok( $url, [ \%LWP_options ,] $desc )
A wrapper around WWW::Mechanize's delete(), 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 "DELETE 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. π
$mech->follow_link_ok( \%parms [, $desc] )
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, $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. π
$mech->click_ok( $button[, $desc] )
$mech->click_ok( \@button-and-coordinates [, $desc ] )
Clicks the button named by $button. An optional $desc can be given for the test.
$mech->click_ok( 'continue', 'Clicking the "Continue" button' );
Alternatively the first argument can be an arrayref with three elements: The name of the button and the X and Y coordinates of the button.
$mech->click_ok( [ 'continue', 12, 47 ], 'Clicking the "Continue" button' );
coordinates?? we're clicking buttons by pixel location now. iconic. β¨
π METHODS: HEADER CHECKING π
$mech->header_exists_ok( $header [, $desc ] )
Assures that a given response header exists. The actual value of the response header is not checked, only that the header exists. presence check only, bestie.
$mech->lacks_header_ok( $header [, $desc ] )
Assures that a given response header does NOT exist. π
$mech->header_is( $header, $value [, $desc ] )
Assures that a given response header exists and has the given value.
$mech->header_like( $header, $value [, $desc ] )
Assures that a given response header exists and has the given value.
π
$mech->html_lint_ok( [$desc] )
Checks whether the HTML on the current page is actually valid, using the HTML::Lint module. If the page isn't HTML at all, it fails, periodt. And the URI gets automatically appended to your $desc, cute.
okay bestie, listen: HTML::Lint has to be installed for this to work. If it's not? It will blow up. No cap. π
π
$mech->html_tidy_ok( [$desc] )
Same vibe check but with the HTML::Tidy module β validates the HTML on the current page. Not HTML? It fails. The URI gets automatically appended to $desc again, love that for us.
and again: HTML::tidy must be installed for this to work. Otherwise it will blow up. π
π
$mech->content_for_tidy()
This one gets called by html_tidy_ok() to grab the content that HTML::Tidy5 should validate. By default it's literally just content(), but subclasses can override it to tweak the content before validation. So much power. π
but bestie β this method should not change any state in the Mech object. Specifically, it should not actually modify any of the actual content. Keep your hands off the content, fr fr. π βοΈ
π
$mech->title_is( $str [, $desc ] )
Tells you if the title of the page is the given string. Simple. Iconic.
$mech->title_is( 'Invoice Summary' );
π
$mech->title_like( $regex [, $desc ] )
Tells you if the title of the page matches the given regex.
$mech->title_like( qr/Invoices for (.+)/ );
π
$mech->title_unlike( $regex [, $desc ] )
Tells you if the title of the page matches the given regex.
$mech->title_unlike( qr/Invoices for (.+)/ );
π
$mech->base_is( $str [, $desc ] )
Tells you if the base of the page is the given string.
$mech->base_is( 'http://example.com/' );
π
$mech->base_like( $regex [, $desc ] )
Tells you 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 you 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 you if the content of the page matches the given string.
π
$mech->content_contains( $str [, $desc ] )
Tells you if the content of the page contains $str.
π
$mech->content_lacks( $str [, $desc ] )
Tells you if the content of the page lacks $str.
π
$mech->content_like( $regex [, $desc ] )
Tells you if the content of the page matches $regex.
π
$mech->content_unlike( $regex [, $desc ] )
Tells you if the content of the page does NOT match $regex.
π
$mech->text_contains( $str [, $desc ] )
Tells you if the text form of the page's content contains $str.
When your page has HTML that's difficult, unimportant, or just straight up unlikely to still match over time because designers keep altering the markup, use text_contains instead of content_contains. Trust. π«Ά
# <b>Hi, <i><a href="some/path">User</a></i>!</b>
$mech->content_contains('Hi, User'); # Fails.
$mech->text_contains('Hi, User'); # Passes.
The text is determined by calling $mech->text(). See content in WWW::Mechanize. β¨
π
$mech->text_lacks( $str [, $desc ] )
Tells you if the text of the page lacks $str.
π
$mech->text_like( $regex [, $desc ] )
Tells you if the text form of the page's content matches $regex.
π
$mech->text_unlike( $regex [, $desc ] )
Tells you if the text format of the page's content does NOT match $regex.
π
$mech->has_tag( $tag, $text [, $desc ] )
Tells you if the page has a $tag tag with the given content in its text.
π
$mech->has_tag_like( $tag, $regex [, $desc ] )
Tells you if the page has a $tag tag with the given content in its text.
π
$mech->page_links_ok( [ $desc ] )
Follows all the links on the current page and tests for HTTP status 200. Go little links go π¦
$mech->page_links_ok('Check all links');
π
$mech->page_links_content_like( $regex [, $desc ] )
Follows all the links on the current page and tests their contents for $regex.
$mech->page_links_content_like( qr/foo/,
'Check all links contain "foo"' );
π
$mech->page_links_content_unlike( $regex [, $desc ] )
Follows all the links on the current page and tests that their contents do not contain the specified regex.
$mech->page_links_content_unlike(qr/Restricted/,
'Check all links do not contain Restricted');
π
$mech->links_ok( $links [, $desc ] )
Follows the specified links on the current page and tests for HTTP status 200. You can hand over the links as a reference to an array holding WWW::Mechanize::Link objects, an array of URLs, or just a single scalar URL name. Flexible queen. π
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' );
π
$mech->link_status_is( $links, $status [, $desc ] )
Follows the specified links on the current page and tests for the HTTP status you passed in. Again, links can be 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' );
π
$mech->link_status_isnt( $links, $status [, $desc ] )
Follows the specified links on the current page and tests for the HTTP status you passed in. Links can be 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' );
π
$mech->link_content_like( $links, $regex [, $desc ] )
Follows the specified links on the current page and tests the resulting content of each one against $regex. Links can be 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' );
π
$mech->link_content_unlike( $links, $regex [, $desc ] )
Follows the specified links on the current page and tests that the resulting content of each one does not match $regex. Links can be 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' );
π METHODS: SCRAPING π
π
$mech->scrape_text_by_attr( $attr, $attr_value [, $html ] )
π
$mech->scrape_text_by_attr( $attr, $attr_regex [, $html ] )
Returns a list of strings, and each string is the text surrounded by an element with attribute $attr of value $value. You can also pass in a regular expression instead. If nothing's found, you get back an empty list. In scalar context, the return is just the first string it found. First come, first served. π
If $html is passed in, that gets scraped instead of the current page's content.
π
$mech->scrape_text_by_id( $id [, $html ] )
Finds all the elements with the given ID attribute and pulls out the text that element encloses. β¨
In list context, returns a list of all the strings found. In scalar context, returns the first one found.
If $html isn't provided, then the current content gets used.
π
$mech->scraped_id_is( $id, $expected [, $msg] )
Scrapes the current page for the given ID and tests that it matches the expected value.
π
$mech->scraped_id_like( $id, $expected_regex [, $msg] )
Scrapes the current page for the given id and tests that it matches the expected regex.
π
$mech->id_exists( $id )
Returns TRUE/FALSE depending on whether the given ID exists in the given HTML, or β if none is provided β then in the current page.
The Mech object caches the IDs so it doesn't bother reparsing every single time it's asked about an ID. Efficient. We love a low-effort queen. π
π
$agent->id_exists_ok( $id [, $msg] )
Verifies there's an HTML element with ID $id in the page.
π
$agent->ids_exist_ok( \@ids [, $msg] )
Verifies an HTML element exists with each ID in \@ids.
π
$agent->lacks_id_ok( $id [, $msg] )
Verifies there is NOT an HTML element with ID $id in the page.
π
$agent->lacks_ids_ok( \@ids [, $msg] )
Verifies there are no HTML elements with any of the ids given in \@ids.
π
$mech->button_exists( $button )
Returns a boolean saying whether a submit button with the name $button exists. Heads up bestie, this one does not do a test. For that you want button_exists_ok or lacks_button_ok.
π
$mech->button_exists_ok( $button [, $msg] )
Asserts that the button exists on the page. π
π
$mech->lacks_button_ok( $button [, $msg] )
Asserts that no button named $button exists on the page.
π METHODS: MISCELLANEOUS π
$mech->autolint( [$status] )
okay so if you call it with no argument at all, it just hands you back a true-or-false vibe check on whether autolint is currently active. that's it. that's the whole thing. π«Ά
But when you pass it an argument, autolint gets switched on or off depending on whether that argument is true or false β and you get the previous autolint status back as a lil souvenir. And just like with the autolint option of new, $status can totally be an HTML::Lint object. π
And here's the slay part: if autolint is currently using an HTML::Lint object you provided, the return value is that exact object. So you can change autolint and then restore it exactly:
my $old_status = $mech->autolint( 0 );
... operations that should not be linted ...
$mech->autolint( $old_status );
bestie that's literally the save-and-restore of your dreams, no cap. πΎβ¨
$mech->autotidy( [$status] )
same energy, different glow-up. With no argument, you get a true or false value saying whether autotidy is active.
Pass it an argument and autotidy flips on or off depending on whether the argument is true or false, and the previous autotidy status comes back to you. As with the autotidy option of new, $status can be an HTML::Tidy5 object. π§Ό
If autotidy is currently using an HTML::Tidy5 object you provided, the return is that object, so you can change and exactly restore autotidy status:
my $old_status = $mech->autotidy( 0 );
... operations that should not be tidied ...
$mech->autotidy( $old_status );
tidy up, then put it back exactly how it was. main character behaviour fr fr. π
$mech->grep_inputs( \%properties )
Returns a list of all the input controls in the current form whose properties match all of the regexes in $properties. And every control it hands back descends from HTML::Form::Input, just so you know its lineage. πΈ
If $properties is undef or empty then all inputs get returned. generous queen.
If there's no current page, or no form on the current page, or no submit controls in the current form β then you get an empty list back. it's giving nothing, and that's on purpose. π
# Get all text controls whose names begin with "customer".
my @customer_text_inputs =
$mech->grep_inputs( {
type => qr/^(text|textarea)$/,
name => qr/^customer/
}
);
look at that regex flex. π
$mech->grep_submits( \%properties )
grep_submits() does the same thing as grep_inputs() β same matching, same energy β except it only returns controls that are submit controls, ignoring other types of input controls like text and checkboxes. we're being picky and we're thriving. π
$mech->stuff_inputs( [\%options] )
okay this one is unhinged and I love her. π It finds all the 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. yes, it's chaos on purpose.
Fields with no maximum length, plus all textarea fields, get set to 66000 bytes, which will often be enough to overflow the data's eventual receptacle. π₯
There is no return value. It just does the thing.
If there is no current form then nothing is done. respectful. π«Ά
The hashref $options can contain the following keys:
ignoreβ hash value is an arrayref of field names to not touch, e.g.:
$mech->stuff_inputs( {
ignore => [qw( specialfield1 specialfield2 )],
} );
fillβ hash value is the 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 an 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).
heads up bestie: this method is designed to push limits. that's the whole point. don't be surprised when things get weird. π
$mech->followable_links()
Returns a list of links that Mech can follow. And that's only http and https links. if it's not those two, it's not coming with us. periodt. π
$mech->lacks_uncapped_inputs( [$comment] )
Executes a test to make sure the current form content has no text input fields that lack the "maxlength" attribute, and that each "maxlength" value is a positive integer. The test fails if the current form has such a field, and succeeds otherwise.
It checks that all text input fields in the current form specify a maximum input length. Fields for which the concept of input length is irrelevant, and controls that HTML does not allow to be capped (e.g. textarea) are ignored. so it's fair, it's just not playing games. π
The return is true if the test succeeded, false otherwise.
$mech->check_all_images_ok( [%criterium ], [$comment] )
Executes a test to make sure all images in the page can be downloaded. It does this by running "HEAD" requests on them. The current page content stays the same β no navigation, no drama, just a lil peek. π
The test fails if any image cannot be found, but reports all of the ones that were not found. thorough queen. π
For a definition of all images, see "images" in WWW::Mechanize.
The optional %criterium argument can be passed in before the $comment and will be used to define which images should be considered. This is useful to filter out specific paths. π¦
$mech->check_all_images_ok( url_regex => qr{^/}, 'All absolute images should exist');
$mech->check_all_images_ok( url_regex => qr{\.(?:gif|jpg)$}, 'All gif and jpg images should exist');
$mech->check_all_images_ok(
url_regex => qr{^((?!\Qhttps://googleads.g.doubleclick.net/\E).)*$},
'All images should exist, but Ignore the ones from Doubleclick'
);
look at that negative lookahead. she ate. π₯
For a full list of possible arguments see "find_all_images" in WWW::Mechanize.
The return is true if the test succeeded, false otherwise.
π TODO π
Other ideas for features are at https://github.com/petdance/test-www-mechanize
the wishlist is open, the vibes are immaculate. β¨
π«Ά AUTHOR π«Ά
Andy Lester, "<andy at petdance.com>"
π BUGS π
Please report any bugs or feature requests to https://github.com/petdance/test-www-mechanize.
ngl if you found something broken, they genuinely wanna know. π«‘
π 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
- CPAN Ratings
http://cpanratings.perl.org/d/Test-WWW-Mechanize
- Search CPAN
http://search.cpan.org/dist/Test-WWW-Mechanize
π ACKNOWLEDGEMENTS π
Thanks to Julien Fiegehenn, @marderh, Eric A. Zarko, @moznion, Robert Stone, @tynovsky, Jerry Gay, Jonathan "Duke" Leto, 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.
y'all are the real ones, every single one. π
π COPYRIGHT & LICENSE π
Copyright 2004-2022 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.
and that's the whole page bestie, we made it to the end π this one shipped as perl v5.36.0, dated 2022-12-16, in the Mechanize(3pm) manpage. go forth and test your web things, you absolute icon. πβ¨π¦
π¦ where this page comes from
- debian bookworm: libtest-www-mechanize-perl 1.60-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtest-www-mechanize-perl 1.60-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtest-www-mechanize-perl 1.60-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-www-mechanize-perl 1.60-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-www-mechanize-perl 1.60-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms