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
[Corion]: Discipulus: Well, in many cases it doesn't make sense to build an interface and complicated program just to enter 20 rows into a database ;) But yes, automating data imports should pay off in the long run
[LanX]: Choroba: this happened before I joined, was still in uni, but my boss was summoned to the CEO of the second biggest German bank at that time and could only say " I told them its not ready" ;)
[LanX]: memories....I missed my connection while chatting
[Discipulus]: in this case Corion we are speaking about software licensing: evry year or two we must rescan the whole ced to produce an excel report, while at every activation / disactivation we update a black box DB: i said that i a week i can produce the perl to..
[Discipulus]: rend out the xls IF i have access to the DB
[choroba]: LanX I miss working in a bank sometimes...
[Corion]: Discipulus: Ooof. Especially yearly things are things I like to automate instead of trying to remember how I did things last year...
[Corion]: And the second rule that I've learned is, that there is no one-off job, so writing a program for it pays off almost immediately. The third rule is to give all my programs numbers and have them reproduce that number in the name of their output files. :)
[Discipulus]: the true part is that also specification change between years.. but well our job is cheap but dont abuse of us.. ;=)

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2017-03-29 12:07 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (350 votes). Check out past polls.