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.

In reply to WWW::Mechanize and logging into application by simon.proctor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.