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

I am writing some Web tests and am using WWW::Mechanize to emulate a browser. All went well until I encountered a rather bizarre form.

$webtest->submit_form( form_name => 'lookup', fields => { title => $some_title, }); is($webtest->title, 'Film Lookup Results', 'The Film Lookup title +should be correct'); like($webtest->content, qr/Usual Suspects, The/, '... and have som +e valid content');

As it turns out, the search works by redirecting the user to the results page, rather than simply returning the results. Thus, my title and content tests fail as the webtest is not automatically following the redirect. I tried using the WWW::Mechanize::redirect_ok() method, but I can't figure it out from the docs. They don't make it clear that a request object is a necessary argument. I also was wondering if I need to just grab the URI from the headers and just go there directly, but I've not tried that yet. Any suggestions?

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re: Redirecting Forms with WWW::Mechanize
by petdance (Parson) on Sep 30, 2003 at 20:17 UTC
    Can you post a larger code sample? Redirects work fine for me, but you'd expect them to. :-)

    xoxo,
    Andy

      To make a long story very short, it turns out that there was more than one button on the form. I didn't realize this. The redirect works perfectly. I merely had to select the correct button.

      $webtest->submit_form( form_name => 'lookup', button => 'do_search', fields => { title_name => 'Suspects', });

      Everyone be sure to /msg Ovid RTFM :)

      Cheers,
      Ovid

      New address of my CGI Course.

        That's why I have the mech-dump program included with Mech. Run it against the page you're interested in and you'll have a handy summary of what's going on in the page.

        xoxo,
        Andy

      Here's the basics of how the web testing object is set up. If WWW::Mechanize should automatically follow a redirect request, then perhaps there's something wrong I am doing in the setup?

      package Foo::Webtest; use strict; use warnings; use URI; use Hook::LexWrap; use WWW::Mechanize; use Class::MethodMaker ( new => 'new', object => [ 'WWW::Mechanize' => { slot => 'browser', comp_mthds => [qw/ add_header agent_alias back base click content ct current_form field find_all_links find_link follow_link form_name form_number forms get is_html links quiet redirect_ok reload request response set_fields status submit submit_form success tick title untick uri /], } ]); my $start_time; my @times; my %URI; if ($ENV{TRACK_NAVIGATION}) { foreach my $method (qw/back click follow_link get reload submit subm +it_form/) { wrap $method, pre => sub {$start_time = time}, post => sub { my $end_time = time; my $time = $end_time - $start_time; push @times => $time; push @{$URI{$_[0]->base_uri}} => $time; } } }

      The above code is my setup for the Mechanize object. I use Class::MethodMaker to ensure that I have a "hasa" relationship to Mechanize and all methods listed are delegated to the Mechanize object. Then, Hook::LexWrap is called to allow me to (loosely) time the fetch methods.

      Later, I can do this:

      my $webtest = Foo::Webtest->new; $webtest->get($page_to_test); # and then use the code in my root post

      (Note that this was pared down fairly quickly). I am wondering if my weird setup is causing the problem. All I know right now is that I receive a redirect, but the $webtest->content still points to the page the original form is on. I was hoping it was simply a quick question on how to get the form to automatically follow, but if it's not, I can spend some time paring this down to a minimal test case.

      Cheers,
      Ovid

      New address of my CGI Course.