[MARKSTOS - Tue Oct 19 21:28:30 2004]:
Show quoted text> Anybody what to sign up to provide some?
Sure! The enclosed patch also clarifies documentation ("the current
form" instead of "a form" where needed) and adds the possibility to
click_button() on an HTML::Form::SubmitInput object.
--- www-mechanize.CVS/lib/WWW/Mechanize.pm Thu Oct 21 13:35:40 2004
+++ www-mechanize/lib/WWW/Mechanize.pm Thu Oct 21 13:40:21 2004
@@ -530,11 +530,11 @@
=head2 $mech->set_fields( $name => $value ... )
-This method sets multiple fields of a form. It takes a list of field
-name and value pairs. If there is more than one field with the same
-name, the first one found is set. If you want to select which of the
-duplicate field to set, use a value which is an anonymous array which
-has the field value and its number as the 2 elements.
+This method sets multiple fields of the current form. It takes a list
+of field name and value pairs. If there is more than one field with
+the same name, the first one found is set. If you want to select which
+of the duplicate field to set, use a value which is an anonymous array
+which has the field value and its number as the 2 elements.
# set the second foo field
$mech->set_fields( $name => [ 'foo', 2 ] ) ;
@@ -564,11 +564,11 @@
=head2 $mech->set_visible( @criteria )
-This method sets fields of a form without having to know their
-names. So if you have a login screen that wants a username and
-password, you do not have to fetch the form and inspect the source
-(or use the F<mech-dump> utility, installed with WWW::Mechanize)
-to see what the field names are; you can just say
+This method sets fields of the current form without having to know
+their names. So if you have a login screen that wants a username and
+password, you do not have to fetch the form and inspect the source (or
+use the F<mech-dump> utility, installed with WWW::Mechanize) to see
+what the field names are; you can just say
$mech->set_visible( $username, $password ) ;
@@ -705,9 +705,9 @@
=head2 $mech->click( $button [, $x, $y] )
-Has the effect of clicking a button on a form. The first argument
-is the name of the button to be clicked. The second and third
-arguments (optional) allow you to specify the (x,y) coordinates
+Has the effect of clicking a button on the current form. The first
+argument is the name of the button to be clicked. The second and
+third arguments (optional) allow you to specify the (x,y) coordinates
of the click.
If there is only one button on the form, C<< $mech->click() >> with
@@ -724,27 +724,35 @@
return $self->request( $request );
}
-=head2 $mech->click_button( ... )
+=head2 $mech->click_button( ... )
-Has the effect of clicking a button on a form by specifying its name,
-value, or index. Its arguments are a list of key/value pairs. Only
-one of name, number, or value must be specified.
-
-TODO: This function has no tests.
+Has the effect of clicking a button on the current form by specifying
+its name, value, or index. Its arguments are a list of key/value
+pairs. Only one of name, number, input or value must be specified in
+the keys.
=over 4
=item * name => name
-Clicks the button named I<name>.
+Clicks the button named I<name> in the current form.
=item * number => n
-Clicks the I<n>th button in the form.
+Clicks the I<n>th button in the current form. Numbering starts at 1.
=item * value => value
-Clicks the button with the value I<value>.
+Clicks the button with the value I<value> in the current form.
+
+=item * input => $inputobject
+
+Clicks on the button referenced by $inputobject, an instance of
+L<HTML::Form::SubmitInput> obtained e.g. from
+
+ $mech->current_form()->find_input(undef, "submit")
+
+$inputobject must belong to the current form.
=item * x => x
=item * y => y
@@ -761,8 +769,8 @@
my %args = @_;
for ( keys %args ) {
- if ( !/^(number|name|value|x|y)$/ ) {
- $self->warn( qq{Unknown click_button_form parameter "$_"} );
+ if ( !/^(number|name|value|input|x|y)$/ ) {
+ $self->warn( qq{Unknown click_button parameter "$_"} );
}
}
@@ -774,6 +782,8 @@
} elsif ( $args{number} ) {
my $input = $form->find_input( undef, 'submit', $args{number} );
$request = $input->click( $form, $args{x}, $args{y} );
+ } elsif ( $args{input} ) {
+ $request = $args{input}->click( $form, $args{x}, $args{y} );
} elsif ( $args{value} ) {
my $i = 1;
while ( my $input = $form->find_input(undef, 'submit', $i) ) {
diff -Nr -U3 www-mechanize.CVS/nohup.out www-mechanize/nohup.out
diff -Nr -U3 www-mechanize.CVS/t/local/click_button.t www-mechanize/t/local/click_button.t
--- www-mechanize.CVS/t/local/click_button.t Thu Jan 1 01:00:00 1970
+++ www-mechanize/t/local/click_button.t Thu Oct 21 13:37:01 2004
@@ -0,0 +1,51 @@
+use warnings;
+use strict;
+use lib 't/local';
+use LocalServer;
+use Test::More tests => 18;
+
+BEGIN { delete @ENV{ qw( http_proxy HTTP_PROXY ) }; }
+BEGIN {
+ use_ok( 'WWW::Mechanize' );
+}
+
+my $mech = WWW::Mechanize->new();
+isa_ok( $mech, 'WWW::Mechanize', 'Created the object' );
+
+my $server = LocalServer->spawn();
+isa_ok( $server, 'LocalServer' );
+
+my $response = $mech->get( $server->url );
+isa_ok( $response, 'HTTP::Response', 'Got back a response' );
+ok( $response->is_success, 'Got URL' ) or die "Can't even fetch local url";
+ok( $mech->is_html, "Local page is HTML" );
+
+my @forms = $mech->forms;
+is( scalar @forms, 1, "Only one form" );
+
+sub ok_click_success {
+ my ($mech, $message) = @_;
+ like($mech->uri(), qr/formsubmit/, $message);
+ like($mech->uri(), qr/submit=Go/, "Correct button was pressed");
+ like($mech->uri(), qr/cat_foo/, "Parameters got transmitted OK");
+}
+
+$mech->click_button(number => 1);
+ok_click_success($mech, "Clicking on button by number");
+$mech->back();
+
+ok(! eval { $mech->click_button(number => 2); 1 },
+ "Button number out of range");
+
+$mech->click_button(name => "submit");
+ok_click_success($mech, "Clicking on button by name");
+$mech->back();
+
+ok(! eval { $mech->click_button(name => "bogus"); 1 },
+ "Button name unknown");
+
+my ($input) = $forms[0]->find_input(undef, "submit");
+$mech->click_button(input => $input);
+ok_click_success($mech, "Clicking on button by object reference");
+$mech->back();
+