Test::WWW::Mechanize::Catalyst - Test::WWW::Mechanize for Catalyst
# We're in a t/*.t test script... # To test a Catalyst application named 'Catty': use Test::WWW::Mechanize::Catalyst 'Catty'; my $mech = Test::WWW::Mechanize::Catalyst->new; $mech->get_ok("http://localhost/"); is($mech->ct, "text/html"); $mech->title_is("Root", "On the root page"); $mech->content_contains("This is the root page", "Correct content"); $mech->follow_link_ok({text => 'Hello'}, "Click on Hello"); # ... and all other Test::WWW::Mechanize methods
Catalyst is an elegant MVC Web Application Framework. Test::WWW::Mechanize is a subclass of WWW::Mechanize that incorporates features for web application testing. The Test::WWW::Mechanize::Catalyst module meshes the two to allow easy testing of Catalyst applications without starting up a web server.
Testing web applications has always been a bit tricky, normally starting a web server for your application and making real HTTP requests to it. This module allows you to test Catalyst web applications but does not start a server or issue HTTP requests. Instead, it passes the HTTP request object directly to Catalyst. Thus you do not need to use a real hostname: "http://localhost/" will do.
This makes testing fast and easy. Test::WWW::Mechanize provides functions for common web testing scenarios. For example:
$mech->get_ok( $page ); $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 module supports cookies automatically.
To use this module you must pass it the name of the application. See the SYNOPSIS above.
Note that Catalyst has a special developing feature: the debug screen. By default this module will treat responses which are the debug screen as failures. If you actually want to test debug screens, please use:
$m->{catalyst_debug} = 1;
Behaves like, and calls, WWW::Mechanize's new method. Any parms passed in get passed to WWW::Mechanize's constructor. Note that we need to pass the name of the Catalyst application to the "use":
new
use Test::WWW::Mechanize::Catalyst 'Catty'; my $mech = Test::WWW::Mechanize::Catalyst->new;
A wrapper around WWW::Mechanize's get(), with similar options, except the second argument needs to be a hash reference, not a hash. Like WWW::Mechanize's get(), it returns an HTTP::Response object.
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 (.+)/
$mech->title_unlike( qr/Invoices for (.+)/
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 NOT match $regex.
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');
Check the current page for specified links 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' );
Check the current page for specified links 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->links(); $mech->link_status_is( \@links, 403, 'Check all links are restricted' );
my @links = $mech->links(); $mech->link_status_isnt( \@links, 404, 'Check all links are not 404' );
Check the current page for specified links and test the 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->links(); $mech->link_content_like( \@links, qr/Restricted/, 'Check all links are restricted' );
Check the current page for specified links and test the 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.
Makes a follow_link() call and executes tests on the results. The link must be found, and then followed successfully. Otherwise, this test fails.
follow_link()
%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:
$agent->follow_like_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.
$comment
Returns true value if the specified link was found and followed successfully. The HTTP::Response object returned by follow_link() is not available.
Related modules which may be of interest: Catalyst, Test::WWW::Mechanize, WWW::Mechanize.
Leon Brocard, <acme@astray.com>
<acme@astray.com>
Copyright (C) 2005, Leon Brocard
This module is free software; you can redistribute it or modify it under the same terms as Perl itself.
To install Test::WWW::Mechanize::Catalyst, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::WWW::Mechanize::Catalyst
CPAN shell
perl -MCPAN -e shell install Test::WWW::Mechanize::Catalyst
For more information on module installation, please visit the detailed CPAN module installation guide.