Skip to content

Commit

Permalink
Return form number in list context. Fixes #71
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyromanov authored and oalders committed Oct 25, 2017
1 parent bccb3e4 commit 688f837
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/WWW/Mechanize.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,8 @@ selected.
If it is found, the form is returned as an L<HTML::Form> object and set internally
for later use with Mech's form methods such as C<L</field()>> and C<L</click()>>.
When called in a list context, the number of the found form is also returned as
a second value.
Emits a warning and returns undef if no form is found.
Expand All @@ -1364,10 +1366,12 @@ sub form_number {
my $forms = $self->forms;
if ( $forms->[$form-1] ) {
$self->{current_form} = $forms->[$form-1];
return $self->{current_form};
return wantarray
? ($self->{current_form}, $form)
: $self->{current_form};
}

return;
return wantarray ? () : undef;
}

=head2 $mech->form_name( $name )
Expand Down
6 changes: 5 additions & 1 deletion t/local/form.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use warnings;
use strict;
use Test::More tests => 19;
use Test::More tests => 21;

use lib 't/local';
use LocalServer;
Expand All @@ -21,6 +21,10 @@ $mech->get($server->url);
ok( $mech->success, 'Got a page' ) or die;
is( $mech->uri, $server->url, 'Got page' );

my ($form, $number) = $mech->form_number(1);
isa_ok( $form, 'HTML::Form', 'Can select the first form in list context call');
is( $number, 1, 'Form number is correct' );

my $form_number_1 = $mech->form_number(1);
isa_ok( $form_number_1, 'HTML::Form', 'Can select the first form');
is( $mech->current_form(), $mech->{forms}->[0], 'Set the form attribute' );
Expand Down

0 comments on commit 688f837

Please sign in to comment.