Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

WWW::Mechanize and logging into application

by simon.proctor (Vicar)
on Apr 06, 2005 at 13:26 UTC ( #445291=perlquestion: print w/replies, xml ) Need Help??
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:
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");
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.

Replies are listed 'Best First'.
Re: WWW::Mechanize and logging into application
by tomhukins (Curate) on Apr 06, 2005 at 16:36 UTC
    If you're using mechanize as part of your testing process, you might find Test::WWW::Mechanize useful.
Re: WWW::Mechanize and logging into application
by cbrandtbuffalo (Deacon) on Apr 06, 2005 at 16:55 UTC
    Other suggestions:
    • Try a bad login to see if the error page is displayed correctly.
    • You may want to consider the Autocheck option since you are checking success each time anyway.
    • Take a look at Test::WWW::Mechanize
Re: WWW::Mechanize and logging into application
by cazz (Pilgrim) on Apr 06, 2005 at 15:08 UTC
    I've been maintaining an automated test suite for a fairly complex web application via mech for a while. mech++.

    I see a few things you might want to look at in your code:

    1. This snippet: cmp_ok($mech->form_name('login'), '==', undef, "Logged in correctly. Page no longer shows logout option.");
      Should probably say "Logged in correctly. Page no longer shows login option."
    2. I'm guessing there is a mechanism for users to log out insead of remembering rm=logout. Instead of hard coding that, why not verify the logout functionality exists as displayed by the user and when the click it, it logs out.
    3. Instead of just checking for $mech->success, perhaps you should check for some content in the page. Just because your CGI returns 200 doesn't mean that all is "ok".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://445291]
Approved by Limbic~Region
[marinersk]: And what fact are you curious about, Discipulus?

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2017-05-30 08:04 GMT
Find Nodes?
    Voting Booth?