Skip to content

Commit 9eaf15e

Browse files
simbabqueoalders
authored andcommitted
allow setting number in select #76
1 parent 8c8aebf commit 9eaf15e

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

Changes

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ Revision history for WWW::Mechanize
44
[ENHANCEMENTS]
55
- WWW::Mechanize no longer taints the responses it receives. This also
66
removes Test::Taint as a prerequisite.
7+
- select() now accepts a number argument to specify which instance of an
8+
element with multiple occurrences to use (GH#189) (Julien Fiegehenn)
79
[DOCUMENTATION]
8-
- Improve FAQ (GH#189) (Julien Fiegehenn)
10+
- Improve FAQ (GH#76) (Julien Fiegehenn)
911

1012
2.19 2024-09-16 15:25:45Z
1113
[DOCUMENTATION]

lib/WWW/Mechanize.pm

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,8 @@ sub field {
20572057

20582058
=head2 $mech->select($name, $new_or_additional_single_value)
20592059
2060+
=head2 $mech->select($name, $new_or_additional_single_value, $number)
2061+
20602062
=head2 $mech->select($name, \%new_single_value_by_number)
20612063
20622064
=head2 $mech->select($name, \@new_list_of_values)
@@ -2069,6 +2071,13 @@ specified.
20692071
# select 'foo'
20702072
$mech->select($name, 'foo');
20712073
2074+
The optional C<$number> parameter is used to distinguish between two fields
2075+
with the same name. The fields are numbered from 1. Note that this only works
2076+
for selecting simple values, not for selecting multiple values as once.
2077+
2078+
# select the second field with the name 'foo'
2079+
$mech->select($name, 'foo', 2);
2080+
20722081
If the field is not C<< <select multiple> >> and the
20732082
C<$value> is an array reference, only the B<first> value will be set. [Note:
20742083
until version 1.05_03 the documentation claimed that only the last value would
@@ -2117,11 +2126,12 @@ false and calls C<< $self->warn() >> with an error message.
21172126
=cut
21182127

21192128
sub select {
2120-
my ( $self, $name, $value ) = @_;
2129+
my ( $self, $name, $value, $number ) = @_;
2130+
$number ||= 1;
21212131

21222132
my $form = $self->current_form();
21232133

2124-
my $input = $form->find_input($name);
2134+
my $input = $form->find_input( $name, undef, $number );
21252135
if ( !$input ) {
21262136
$self->warn(qq{Input "$name" not found});
21272137
return;
@@ -2188,7 +2198,7 @@ sub select {
21882198
return 1;
21892199
}
21902200

2191-
$form->value( $name => $value );
2201+
$form->find_input( $name, undef, $number )->value($value);
21922202
return 1;
21932203
}
21942204

t/select.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
<option value="ccc">ccc</a>
1818
<option value="ddd">ddd</a>
1919
</select>
20+
21+
<select name="exists_twice">
22+
<option value="one">one</option>
23+
<option value="two">two</option>
24+
</select>
25+
26+
<select name="exists_twice">
27+
<option value="three">three</option>
28+
<option value="four">four</option>
29+
</select>
30+
2031
</FORM>
2132
</BODY>
2233
</HTML>

t/select.t

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,22 @@ like warning { $rv = $mech->select( 'missing_list', 1 ) }, qr/not found/,
9999
'warning when field is not found';
100100
is( $rv, undef, 'return undef after failed select' );
101101

102+
# test setting a number
103+
$mech->select( 'exists_twice', 'two', 1 );
104+
$mech->select( 'exists_twice', 'four', 2 );
105+
@return = $form->param('exists_twice');
106+
is_deeply(
107+
\@return, [ 'two', 'four' ],
108+
'select exists twice, set both and values are ' . join( ' ', @return )
109+
);
110+
111+
$mech->select( 'exists_twice', 'one', 1 );
112+
$mech->select( 'exists_twice', 'one', 2 );
113+
@return = $form->param('exists_twice');
114+
is_deeply(
115+
\@return, [ 'one', 'one' ],
116+
'select exists twice, set to double values and they are '
117+
. join( ' ', @return )
118+
);
119+
102120
done_testing;

0 commit comments

Comments
 (0)