simon.proctor has asked for the wisdom of the Perl Monks concerning the following question:
I am shoehorning a test suite into a web application. There is already a paper based test process (that the user works through) and I am beginning by automating that.
To that end, I figured I would use WWW::Mechanize and came up with the code below (inside the readmore). The app uses an internal redirect via CGI::Application to load the login form.
This is via a combination of calls to cgi_prerun and prerun_mode.
I figured the best way to test this was to check for the presence of the login form (requiring I supress warnings).
This looks the best way to do it but as this is my first Mechanize script I thought I'd throw it open for feedback.
Code follows:
Comments and suggestions welcome. Especially given that
I am going to have to build on the above code for each test
case as it requires you to be logged in first and to expect
certain forms (and not others) to be present.
To that end, I figured I would use WWW::Mechanize and came up with the code below (inside the readmore). The app uses an internal redirect via CGI::Application to load the login form.
This is via a combination of calls to cgi_prerun and prerun_mode.
I figured the best way to test this was to check for the presence of the login form (requiring I supress warnings).
This looks the best way to do it but as this is my first Mechanize script I thought I'd throw it open for feedback.
Code follows:
use Test::More qw(no_plan); use strict; use warnings 'all'; use lib qw(/mypath/); use Lib::Constants; use WWW::Mechanize; use constant USERNAME => 'user'; use constant PASSWORD => 'pass'; # ==================================================================== +====== # Test logging in and out. We suppress warnings as we expect to NOT se +e # the login form when logged in. # ==================================================================== +====== my $mech = WWW::Mechanize->new( quiet => 1 ); my $base = ''; if(Constants::APPSTATUS < 0) { $base = Constants::DEVURL; } else { $base = Constants::UATURL; } $mech->get( $base . 'index.cgi' ); ok($mech->success(), "Home page loaded successfully"); # We are not logged in, we should have a login option. cmp_ok($mech->form_name('login'), '!=', undef, "Login form loaded suce +ssfully"); # Login $mech->form_number('1'); $mech->field('USERNAME', USERNAME); $mech->field('PASSWORD', PASSWORD); $mech->submit(); ok($mech->success(), "Login details submitted successfully"); # We should now be able to load the home page without # seeing the login form $mech->get( $base . 'index.cgi?rm=home' ); ok($mech->success(), "Home page loaded successfully after sending in l +og in details"); # If we have a logout option then we logged in ok! cmp_ok($mech->form_name('login'), '==', undef, "Logged in correctly. P +age no longer shows logout option."); # Now logout - this redirects to the home via a meta refresh. $mech->get( $base . 'index.cgi?rm=logout' ); ok($mech->success(), "Logout call made successfully"); # Try and view the home page again $mech->get( $base . 'index.cgi?rm=home' ); ok($mech->success(), "Home page reloaded successfully"); cmp_ok($mech->form_name('login'), '!=', undef, "Login form sucessfully + shown again");
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: WWW::Mechanize and logging into application
by tomhukins (Curate) on Apr 06, 2005 at 16:36 UTC | |
Re: WWW::Mechanize and logging into application
by cbrandtbuffalo (Deacon) on Apr 06, 2005 at 16:55 UTC | |
Re: WWW::Mechanize and logging into application
by cazz (Pilgrim) on Apr 06, 2005 at 15:08 UTC |
Back to
Seekers of Perl Wisdom