Test::WWW::Mechanize::Maypole - Test::WWW::Mechanize for Maypole
use Test::WWW::Mechanize::Maypole 'BeerDB'; # or load a test database instead of the one configured in BeerDB.pm: # # use Test::WWW::Mechanize::Maypole 'BeerDB', 'dbi:SQLite:test-beerdb.db'; # use Test::WWW::Mechanize::Maypole 'BeerDB', 'dbi:mysql:beer_d_b', 'dhoworth', 'password'; $ENV{MAYPOLE_TEMPLATES} = 'path/to/templates'; my $mech = Test::WWW::Mechanize::Maypole->new; # # basic tests: # $mech->get_ok( "http://localhost/beerdb/" ); is( $mech->ct, "text/html" ); $mech->content_contains( 'This is the frontpage' ); # # logging in and storing cookies: # $mech->get_ok("http://localhost/beerdb/customer/buybeer"); $mech->content_contains( 'Login to BeerDB', 'got login page' ); # specify which form we're interested in $mech->form_number(1); # the 1st form # fill in credentials $mech->field( 'username' => 'landlord' ); $mech->field( 'password' => 'handpump' ); # get a HTTP::Response back my $response = $mech->click_button( name => 'submit' ); like( $response->content, qr/Shop for beer/, 'got customer/buybeer page' ); # check our cookies give access to other pages $mech->get_ok( "http://localhost/beerdb/customer/edit" ); $mech->content_contains( 'Update your details', "got customer account edit page"); # ... see Test::WWW::Mechanize for many more test methods
By inheriting from Test::WWW::Mechanize, this module provides two key benefits over using Maypole::CLI in test scripts. First, it inherits a plethora of methods for testing web content. Second, cookies are handled transparently, allowing you to test applications that use cookie-based sessions and authentication.
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 Maypole web applications but does not start a server or issue HTTP requests. Instead, it passes the HTTP request parameters directly to Maypole. 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( "David Baird", "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.
Additionally, you can pass an alternate set of database connection parameters, and these will override the settings configured in your application. Useful for connecting to a test database without having to alter your production code. This won't work if your application calls setup() inside a BEGIN block.
setup()
BEGIN
Inherited from Test::WWW::Mechanize, which passes any parameters through to WWW::Mechanize::new().
Note that the name of the Maypole application should be passed to the use statement:
use
use Test::WWW::Mechanize::Maypole 'BeerDB'; my $mech = Test::WWW::Mechanize::Maypole->new;
Set $ENV{MAYPOLE_TEMPLATES} to the path where the templates for the application can be found. Defaults to '.'.
$ENV{MAYPOLE_TEMPLATES}
'.'
Please see the documentation for Test::WWW::Mechanize.
These methods are exported into the application's namespace, and override methods that would otherwise be inherited from Maypole or the Maypole frontend.
You will not normally need to use these methods in your test scripts.
If you need to replace these methods with custom versions, let me know, and I'll make exporting more flexible.
Just some random notes, feel free to send me any favourite usages and I'll include them here.
sub new_mech { my ( $url ) = @_; my $mech = Test::WWW::Mechanize::Maypole->new; $mech->get_ok( $url, "got something for $url" ) if $url; return $mech; } sub new_logged_in_mech { my ( $protected_url ) = @_; my $mech = new_mech; # request something that will get redirected to the login page $mech->get("http://localhost/index.html"); # specify which form we're interested in $mech->form_number(1); my $user = 'testuser'; my $pass = 'testpass'; # fill in credentials $mech->field( username => $user ); $mech->field( password => $pass ); $mech->click; $mech->get_ok( $protected_url, "got something for $url" ) if $protected_url; return $mech; }
David Baird, <cpan@riverside-cms.co.uk>
<cpan@riverside-cms.co.uk>
Please report any bugs or feature requests to bug-test-www-mechanize-maypole@rt.cpan.org, or through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-test-www-mechanize-maypole@rt.cpan.org
Test::WWW::Mechanize, WWW::Mechanize.
Pieced together from bits of from Test::WWW::Mechanize::Catalyst, by Leon Brocard, Maypole::CLI, by Simon Cozens, Catalyst::Request, by Sebastian Riedel and Marcus Ramberg, and Catalyst::Engine::HTTP::Base, by Sebastian Riedel and Christian Hansen.
Copyright 2004 David Baird, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Test::WWW::Mechanize::Maypole, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::WWW::Mechanize::Maypole
CPAN shell
perl -MCPAN -e shell install Test::WWW::Mechanize::Maypole
For more information on module installation, please visit the detailed CPAN module installation guide.