Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tick() can handle checkboxes that have no value #331

Merged
merged 3 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Revision history for WWW::Mechanize

{{$NEXT}}
[FIXED]
- tick() can now handle checkboxes without a value (GH#331) (Jordan M Adler
and Julien Fiegehenn)

2.10 2022-07-04 21:06:13Z

Expand Down
21 changes: 18 additions & 3 deletions lib/WWW/Mechanize.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2069,9 +2069,17 @@ sub set_visible {
=head2 $mech->tick( $name, $value [, $set] )

"Ticks" the first checkbox that has both the name and value associated
with it on the current form. Dies if there is no named check box for
that value. Passing in a false value as the third optional argument
will cause the checkbox to be unticked.
with it on the current form. If there is no value to the input, just
pass an empty string as the value. Dies if there is no named checkbox
for the value given, if a value is given. Passing in a false value
as the third optional argument will cause the checkbox to be unticked.
The third value does not need to be set if you wish to merely tick the
box.

$mech->tick('extra', 'cheese');
$mech->tick('extra', 'mushrooms');

$mech->tick('no_value', ''); # <input type="checkbox" name="no_value">

=cut

Expand All @@ -2084,6 +2092,13 @@ sub tick {
# loop though all the inputs
my $index = 1;
while ( my $input = $self->current_form->find_input( $name, 'checkbox', $index ) ) {
# Sometimes the HTML is malformed and there is no value for the check
# box, so we just return if the value passed is an empty string
# (and the form input is found)
if ($value eq '') {
$input->value($set ? $value : undef);
return;
}
# Can't guarantee that the first element will be undef and the second
# element will be the right name
foreach my $val ($input->possible_values()) {
Expand Down
5 changes: 4 additions & 1 deletion t/tick.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

<input type="checkbox" name="foo" value="hello" /> Hello<br />
<input type="checkbox" name="foo" value="bye" /> Bye<br />
<input type="checkbox" name="foo" value="arse" /> Arse<br />
<input type="checkbox" name="foo" value="parse" /> Parse<br />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a change I would have made, but I'll approve 😆

<input type="checkbox" name="foo" value="wibble" /> Wibble<br />
<input type="checkbox" name="foo" value="foo" /> Foo<br />
<label>
<input type="checkbox" name="no_value" /> I have no value
</label>

<input type="Submit" name="submit" value="Submit" label="Submit" />
</form>
Expand Down
6 changes: 4 additions & 2 deletions t/tick.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ $mech->tick('foo','hello');
$mech->tick('foo','bye');
$mech->untick('foo','hello');

$mech->tick('no_value', '');

my $form = $mech->form_number(1);
isa_ok( $form, 'HTML::Form' );

my $reqstring = $form->click->as_string;

my $wanted = <<'EOT';
POST http://localhost/
Content-Length: 21
Content-Length: 31
Content-Type: application/x-www-form-urlencoded
foo=bye&submit=Submit
foo=bye&no_value=&submit=Submit
EOT

is( $reqstring, $wanted, 'Proper posting' );
Expand Down