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

Howdy monks,

Last week I was advised to look into WWW::Mechanize for automating testing of a CGI website. I started to try to take that good advice today, but ran into a roadblock. I think the block is with forms, but even now that I have the test-case down to something small I can't really see where the error is. The CGI page is:

use CGI; use strict; my $cgi = new CGI; print $cgi->header(), $cgi->start_html(), $cgi->start_form({ -method => 'GET', -action => 'PUNS_addprimers_execute.cgi' }), $cgi->textfield('new_primer_type'), $cgi->submit('submit'), $cgi->end_form(), $cgi->end_html();

Meanwhile I try to automate access to this via:

use WWW::Mechanize; use strict; my $mech = WWW::Mechanize->new(); my $url = 'http://x.x.x.x/cgi-bin/script.cgi; $mech->get($url); if (!$mech->success()) { die "Couldn't get page\n"; } $mech->form_number(1); if (!$mech->success()) { die "Couldn't set form\n"; } $mech->field('new_primer_type', 'dummy1'); if (!$mech->success()) { die "Couldn't set type\n"; } $mech->click('submit');

And this dies with the error message:

Unexpected field value http://x.x.x.x/cgi-bin/script.cgi at (eval 5) l +ine 1

I'm stuck: this seems like a straight-forward test-case. Any ideas what I'm doing wrong?

Replies are listed 'Best First'.
Re: WWW::Mechanize with forms
by jeffa (Bishop) on Sep 22, 2003 at 21:49 UTC
    Hmmm ... i set up this test CGI script:
    use strict; use warnings; use CGI qw(:standard); print header(), start_html(), start_form(), textfield('new_primer_type'), submit('submit'), end_form(), end_html(), ; if (param('submit')) { print hr, p('You submitted ', param('new_primer_type')) ; }
    Over at http://unlocalhost.com/cgi-bin/script.cgi. Then i ran this W::M test script:
    use strict; use warnings; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); my $url = 'http://unlocalhost.com/cgi-bin/script.cgi'; $mech->get($url); die "Couldn't get page\n" unless $mech->success; $mech->form_number(1); die "Couldn't set form\n" unless $mech->success; $mech->field('new_primer_type', 'dummy1'); die "Couldn't set type\n" unless $mech->success; $mech->click('submit'); die "Couldn't click submit\n" unless $mech->success;
    And it never died ... note that even though i made both of your original scripts more 'idiomatic', the results are still the same. The only thing that i changed that might matter is the action destination of the CGI script. The reason why i did so, should be apparent, but as for what is causing your problem, i can't say ...

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Well, that sorta solves it: running the exact test script on my machine generates:

      C:\dev>perl -w testCGI.pl Unexpected field value http://unlocalhost.com/cgi-bin/script.cgi at (e +val 5) line 1

      I'm on Windows XP, Perl 5.6.1, and WWW::Mechanize 0.48. I'll have to test another couple of machines to see where the problem lies. Would you mind terribly keeping that script available for a couple of hours? I'd much appreciate the ability to validate against two servers rather than just one.

        Sure, i'll leave that CGI script for the rest of the day/night.

        I tested with WWW::Mechanize versions 0.55 and the latest, 0.59 - you might need to upgrade.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: WWW::Mechanize with forms (SOLVED: Thanks Jeffa)
by Anonymous Monk on Sep 23, 2003 at 00:42 UTC

    Thanks to jeffa, the problem was solved. It was fixed simply by upgrading versions. I was on win32 and the ppm repositories didn't have anything more recent than 1.48, but the module doesn't have any XS or anything like that, so you can just download the current version from CPAN and overwrite the version in your /perl/site/lib/WWW directory.

    Thanks jeffa!