edwardt_tril has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to test website that does the following:
1. user login with their username and password
2. user can navigate to different tab, click on the tab then select the page they go.
3. under each tab, user can select certain items throught eh drop down menu, press enter
then it will give data back from database.
4. the site fromntend if all php
Iam trying to mimic this activity and have the perl test
script to go through these link, clicking, and verify the returned data.
but I am having trouble to connect the the website through
the login using Test::WWW::Mechanize. example website: http://sourceforge.net/projects/phpwebsite/
there is a login required.
here is my code
#!perl use strict; use Test::More tests => 3; use Test::WWW::Mechanize; my $agent = Test::WWW::Mechanize->new; $agent->get_ok( 'http://192.168.44.132/reporter' ); $agent->field( 'Username:', 'admin' ); $agent->field( 'Password:', 'password' ); $agent->submit_form( form_name => 'f' ); $agent->follow_link_ok( { text => 'Home', n => '1' } ); $agent->field( 'query', 'test' ); $agent->submit_form( form_name => 'f' ); $agent->follow_link_ok( { text => 'Next >>', n => '1' } );

Error msg:
00-SiteAgentStatus....ok 1/3No such field 'Username:' at C:/Perl/site/lib/WWW/M echanize.pm line 1052
how to do this? is there a better way to test >>php<< site?
thanks I used the view source and found out the this:
<script language="javascript"> function CreateI18n() { this.MUST_BE_NUMERIC="Input must be numeric and greater than ze +ro"; this.CANT_EXCLUDE="You cannot exclude a wildcard"; this.FROM="From"; this.TO="To"; this.ADVANCED_OPTIONS="Advanced Settings"; this.QUICK_OPTIONS="Basic Settings"; this.THIS_WILL_PERMANENTLY_DELETE1="This will permanently delet +e the selected entries"; this.DELETE_ALL_ENTRIES="Delete all entries in current view.\n* +** Warning! This action cannot be undone.***"; this.YOU_MUST_ENTER_USER_NAME_TO_CHANGE_PASSWORD="You must ente +r your user name and password in order to change your password"; this.ENTER_FILTER_NAME="Please enter a filter name into the fil +ter name field"; this.TO_DATE_MUST_BE_MORE_RECENT="The to date must be more rece +nt than the from date"; this.YOU_ENTERED_AN_INVALID_DATE_SEPARATOR="You entered an inva +lid date separator. It must be a single character and cannot be a spa +ce, ?, % or $."; this.YOU_MUST_ENTER_AN_ALERT_CONDITION="You must enter an alert + condition"; return this; } var I18n = CreateI18n(); </script> <html> <head> <title>Reporter - Login</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link href="../Resources/styles.css" rel="stylesheet" type="text/css"> </head> <body onload="document.PasswordForm.user_name_tmp.focus();" class="co +lor_link" leftmargin="0" marginheight="0" marginwidth="0" topmargin=" +0"> <!-- Menu Header --> <!-- Menu Header --> <script type="text/javascript" src="../Resources/sha1.js"></script> <script type="text/javascript" > function change_password() { hidden = document.getElementById('change_pwd'); hidden.value='on'; user_name_tmp = document.getElementById('user_name_tmp'); if (user_name_tmp.value == '') { alert('' + I18n.YOU_MUST_ENTER_USER_NAME_TO_CHANGE_PASSWORD); user_name_tmp.focus(); } else { document.PasswordForm.submit(); } } function login() { document.PasswordForm.submit(); } function sha1_password() { password_tmp_widget = document.getElementById('password_tmp_widget +'); password_tmp = document.getElementById('password_tmp'); var foo = "7cbc76df106c18e2363c552a71b6e455"; var total = hex_sha1(password_tmp_widget.value)+foo; var x = hex_sha1(total); //if (x != password_tmp.value) { password_tmp.value= x; //} } </script> <BR> <BR> <BR> <BR> <table width=600 align=center border="0" cellspacing="0" cellpadding +="0"> <tr> <td> <table border="0" align=center cellspacing="0" cellpadding="0"> <form method="post" name="PasswordForm" action="Login_veri +fy.php"> <tr width=100% height=60 valign=center > <table width=100% border=0 background="../images/loginT +itle.jpg"> <tr > <td height=60 class="HeaderPassword" colspan=2 ali +gn=center background="../images/loginTitle.jpg"> Login </td> <td height=60 > <img src="../images/loginlogoBlue.gif" alt="Login + logo" valign=top align="right" border="0"> </td> </tr> </table> </tr> <tr > <td > <table background="../images/gradient.png" width +=600 height=260 cellspacing="0" cellpadding="0" align="right"> <tr height=50 > <td align=left> </td> </tr> <tr height=70> <td> <table border=0 cellpadding=0 cellspacing +=0> <tr> <td></td> <td colspan=2> <span class=Menue>Please enter your user + name and password to login to Reporting:</span> </td> </tr> <tr> <td>&nbsp;</td> </tr> <tr> <td width=20%></td> <td width=20%> <table height=120 border=0 cellspacin +g=0 align=center cellpadding=5> <tr> <td align=left > <span class="Menue">User Name:</sp +an> </td> </tr> <tr> <td align=left > <span class="Menue">Password:</spa +n> </td> </tr> <tr> <td >&nbsp; <input type="hidden" name="destinat +ion" value="../dashboard/homepage.php"> <input type="hidden" name="referer" + value="../dashboard/homepage.php"> <input type="hidden" name="change_p +wd" id="change_pwd"> </td> </tr> </table> </td> <td width=40%> <table width=100% height=120 border +="0" cellspacing="0" cellpadding="5" align="left"> <tr> <td align=right> <input type="text" name="user_ +name_tmp" id="user_name_tmp" style="width: 225px"> </td> </tr> <tr> <input type="hidden" name="passwo +rd_tmp" id="password_tmp"> <td align=right> <input type="password" onChange +="sha1_password();" onMousemove="sha1_password();" id="password_tmp_w +idget" name="password_tmp_widget" style="width: 225px"> </td> </tr> <tr> <td align=right> <input type="submit" class="UCD" on +click="login();" value='&nbsp;Login&nbsp;'</input> </td> </tr> </table> </td> <td width=20%></td> </tr> </table> </tr> <tr height=50><td></td></tr> <tr> <td width=600 height=5 colspan=3 bgcolor="FF +CC00"></td> </tr> </table> </td> </tr> </form> </table> <p>&nbsp;</p> </td> </tr> </table> </body> </html>

Replies are listed 'Best First'.
Re: testing website with login
by jonadab (Parson) on Dec 04, 2005 at 19:07 UTC
    No such field 'Username'

    Look at the HTML source of the login page (not the raw PHP, but the resulting HTML, e.g., in your browser use the View Page Source feature) and see what that form field is called in its name attribute. Apparently it is not called 'Username'. (Either that or the site isn't feeding the same content to Mechanize that it would give to a regular browser, but that's unlikely if it's a PHP site, IMO, especially if you wrote the PHP, because then you'd know if it was sniffing.)

Re: testing website with login
by planetscape (Chancellor) on Dec 05, 2005 at 06:25 UTC

    As jonadab says, it is essential to check the HTML source of the site you are accessing to make certain you have the correct field names, and to insure you're not trying to put 20 chars into a field with a limit of 10, for example...

    If you still have trouble after checking the source, I would try using a module such as HTTP::Recorder or WWW::Mechanize::Shell to record a successful manual form submission. The output of HTTP::Recorder, for instance, can be "dropped" right into your WWW::Mechanize scripts.

    Another important tool for finding out what is really happening behind the scenes between server and browser is a protocol analyzer such as Ethereal.

    Don't forget that Super Search is your friend here on PM. Many questions such as yours have been asked recently...

    HTH,

    planetscape
      Thank you so much. WWW::Mechanize::Shell was just the tool I needed. And thanks to whoever pointed me at the SuperSearch tool here. It helped me search through a tremendous backlog of wisdom and experience in my quest. In fact, it got me here.

      I was running out of developed perl options I had yet found when I started to read about python's twill-shell. Installed it even (at the cost of a trusted library) and had played around with it. Was imagining using it for the tests I couldn't figure out with Test::More, when I found this post.

      I'm still checking out ethereal. Need to spend more time with its docs to get a clue, I guess. But it certainly looks promising.

      Even wrote an enthusiastic email about this module to my coding buddies.

      Thanks again.

      -- Hugh