Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Test-WWW-Mechanize CPAN distribution.

Report information
The Basics
Id: 12137
Status: resolved
Priority: 0/
Queue: Test-WWW-Mechanize

People
Owner: Nobody in particular
Requestors: mark [...] summersault.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 1.04
Fixed in: (no value)



Subject: wish: submit_form_ok()
Andy, What do you think about adding submit_form_ok(), like get_ok()? I could possibly code it up if acceptance was expected. Mark
Date: Tue, 5 Apr 2005 00:02:27 -0500
From: Andy Lester <andy [...] petdance.com>
To: Guest via RT <bug-Test-WWW-Mechanize [...] rt.cpan.org>
CC: undisclosed-recipients: ;
Subject: Re: [cpan #12137] wish: submit_form_ok()
RT-Send-Cc:
On Mon, Apr 04, 2005 at 04:30:45PM -0400, Guest via RT (bug-Test-WWW-Mechanize@rt.cpan.org) wrote: Show quoted text
> > I could possibly code it up if acceptance was expected.
Yes please. I'd like to eradicate all occurrences of $mech->submit_form( ..... ); ok( $mech->success(), "form OK" ); in my code. -- Andy Lester => andy@petdance.com => www.petdance.com => AIM:petdance
Subject: PATCH: submit_form_ok()
Attached is a patch with code, docs, tests and 'Changes' file entry for the submit_form_ok() method. Mark
Wed Jun 7 14:36:30 EDT 2006 mark@summersault.com * Add submit_form_ok() method. diff -rN -u old-Test-WWW-Mechanize-1.08/Changes new-Test-WWW-Mechanize-1.08/Changes --- old-Test-WWW-Mechanize-1.08/Changes 2006-06-07 14:36:59.000000000 -0400 +++ new-Test-WWW-Mechanize-1.08/Changes 2006-06-07 14:36:59.000000000 -0400 @@ -1,5 +1,8 @@ Revision history for Test-WWW-Mechanize + [ENHANCEMENTS] + * Added submit_form_ok() method. Thanks to Mark Stosberg. + 1.08 [FIXES] * has_tag_like()'s regex was reversed, so would match when diff -rN -u old-Test-WWW-Mechanize-1.08/Mechanize.pm new-Test-WWW-Mechanize-1.08/Mechanize.pm --- old-Test-WWW-Mechanize-1.08/Mechanize.pm 2006-06-07 14:36:59.000000000 -0400 +++ new-Test-WWW-Mechanize-1.08/Mechanize.pm 2006-06-07 14:36:59.000000000 -0400 @@ -665,6 +665,54 @@ return $ok; } +=head2 submit_form_ok( \%parms [, $comment] ) + +Makes a C<submit_form()> call and executes tests on the results. +The form must be found, and then submitted successfully. Otherwise, +this test fails. + +I<%parms> is a hashref containing the parms to pass to C<submit_form()>. +Note that the parms to C<submit_form()> are a hash whereas the parms to +this function are a hashref. You have to call this function like: + + $agent->submit_form_ok( {n=>3}, "looking for 3rd link" ); + +As with other test functions, C<$comment> is optional. If it is supplied +then it will display when running the test harness in verbose mode. + +Returns true value if the specified link was found and followed +successfully. The L<HTTP::Response> object returned by submit_form() +is not available. + +=cut + +sub submit_form_ok { + my $self = shift; + my $parms = shift || {}; + my $comment = shift; + + # return from submit_form() is an HTTP::Response or undef + my $response = $self->submit_form( %$parms ); + + my $ok; + my $error; + if ( !$response ) { + $error = "No matching form found"; + } else { + if ( !$response->is_success ) { + $error = $response->as_string; + } else { + $ok = 1; + } + } + + $Test->ok( $ok, $comment ); + $Test->diag( $error ) if $error; + + return $ok; +} + + =head2 $agent->stuff_inputs( [\%options] ) XXX Delete this when it winds up in Test::WWW::Mechanize diff -rN -u old-Test-WWW-Mechanize-1.08/t/submit_form_ok.t new-Test-WWW-Mechanize-1.08/t/submit_form_ok.t --- old-Test-WWW-Mechanize-1.08/t/submit_form_ok.t 1969-12-31 19:00:00.000000000 -0500 +++ new-Test-WWW-Mechanize-1.08/t/submit_form_ok.t 2006-06-07 14:36:59.000000000 -0400 @@ -0,0 +1,55 @@ +#!perl -T + +use strict; +use warnings; +use Test::More 'no_plan'; +use Test::Builder::Tester; +use URI::file; + +use constant PORT => 13432; + +BEGIN { + use_ok( 'Test::WWW::Mechanize' ); +} + + +my $server=TWMServer->new(PORT); +my $pid=$server->background; +ok($pid,'HTTP Server started') or die "Can't start the server"; + +sub cleanup { kill(9,$pid) }; +$SIG{__DIE__}=\&cleanup; + +SUBMIT_GOOD_FORM: { + my $mech = Test::WWW::Mechanize->new(); + isa_ok( $mech,'Test::WWW::Mechanize' ); + + $mech->get('http://localhost:'.PORT.'/form.html'); + $mech->submit_form_ok( {form_number =>1}, "Submit First Form" ); +} + +cleanup(); + +{ + package TWMServer; + use base 'HTTP::Server::Simple::CGI'; + + sub handle_request { + my $self=shift; + my $cgi=shift; + + my $file=(split('/',$cgi->path_info))[-1]||'index.html'; + $file=~s/\s+//g; + + if(-r "t/html/$file") { + if(my $response=do { local (@ARGV, $/) = "t/html/$file"; <> }) { + print "HTTP/1.0 200 OK\r\n"; + print "Content-Type: text/html\r\nContent-Length: ", + length($response), "\r\n\r\n", $response; + return; + } + } + + print "HTTP/1.0 404 Not Found\r\n\r\n"; + } +}
Subject: PATCH: better diagnostics for submit_form_ok() and follow_link_ok()
From: mark [...] summersault.com
When switching a submit_form() to submit_form_ok(), it's really easy to forgot to change the hash of inputs to a hashref. This patch gives a really clear error in that case. The same was applied to follow_link_ok() as well. Mark
--- old-miami/perllib/Test/WWW/Mechanize.pm 2006-06-12 09:54:37.000000000 -0400 +++ new-miami/perllib/Test/WWW/Mechanize.pm 2006-06-12 09:54:37.000000000 -0400 @@ -40,6 +40,7 @@ use WWW::Mechanize; use Test::LongString 0.07; use Test::Builder; +use Carp; use Carp::Assert::More; use base 'WWW::Mechanize'; @@ -644,6 +645,10 @@ my $parms = shift || {}; my $comment = shift; + if (not (ref $parms eq 'HASH')) { + croak "FATAL: parameters must be given as a hashref"; + } + # return from follow_link() is an HTTP::Response or undef my $response = $self->follow_link( %$parms ); @@ -691,6 +696,10 @@ my $parms = shift || {}; my $comment = shift; + if (not (ref $parms eq 'HASH')) { + croak "FATAL: parameters must be given as a hashref"; + } + # return from submit_form() is an HTTP::Response or undef my $response = $self->submit_form( %$parms ); @@ -773,8 +782,6 @@ sub stuff_inputs { my $self = shift; - use Carp; - my $options = shift || {}; assert_isa( $options, 'HASH' );
Subject: Ping: submit_form_ok() patch ready.
From: mark [...] summersault.com
On Tue Apr 05 01:09:22 2005, andy@petdance.com wrote: Show quoted text
> On Mon, Apr 04, 2005 at 04:30:45PM -0400, Guest via RT (bug-Test-WWW- > Mechanize@rt.cpan.org) wrote:
> > > > I could possibly code it up if acceptance was expected.
> > Yes please. I'd like to eradicate all occurrences of > > $mech->submit_form( ..... ); > ok( $mech->success(), "form OK" ); > > in my code.
Ping. I saw the 1.12 release go by, with didn't include the patches in this ticket despite the interest expressed here. If you want to give me direct SCM access, I'm happy to self-service. Mark
Done.