Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

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

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

People
Owner: andy [...] petdance.com
Requestors: Peter [...] PSDT.com
Cc:
AdminCc:

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



Date: Mon, 12 Jan 2004 10:33:25 -0800
From: Peter Scott <Peter [...] PSDT.com>
Subject: New feature for Mech
To: andy [...] petdance.com
Hi there. I've added a new feature to Mechanize that I find extremely useful and therefore I hope you'll roll it in. Excerpt from documentation: 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 to see what the field names are; you can just say $mech->set_visible( $username, $password ) ; Patch: --- lib/WWW/Mechanize.pm.orig 2004-01-11 19:42:10.000000000 -0800 +++ lib/WWW/Mechanize.pm 2004-01-11 19:42:21.000000000 -0800 @@ -6,13 +6,13 @@ =head1 VERSION -Version 0.71_02 +Version 0.71_03 $Header: /cvsroot/www-mechanize/www-mechanize/lib/WWW/Mechanize.pm,v 1.101 2003/12/22 20:51:44 petdance Exp $ =cut -our $VERSION = "0.71_02"; +our $VERSION = "0.71_03"; =head1 SYNOPSIS @@ -69,6 +69,7 @@ $mech->form_name( $name ); $mech->field( $name, $value ); $mech->set_fields( %field_values ); + $mech->set_visible( @criteria ); $mech->click( $button ); L<WWW::Mechanize> is a proper subclass of L<LWP::UserAgent> and @@ -572,6 +573,70 @@ } # while } # set_fields() + +=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 to see what the +field names are; you can just say + + $mech->set_visible( $username, $password ) ; + +and the first and second fields will be set accordingly. The method +is called set_I<visible> because it acts only on visible fields; hidden +form inputs are not considered. The order of the fields is the order in +which they appear in the HTML source which is nearly always the order anyone +viewing the page would think they are in, but some creative work with tables +could change that; caveat user. + +Each element in C<@criteria> is either a field value or a field +specifier. A field value is a scalar. A field specifier allows you +to specify the I<type> of input field you want to set and is denoted +with an arrayref containing two elements. So you could specify the +first radio button with + + $mech->set_visible( [ radio => "KCRW" ] ) ; + +Field values and specifiers can be intermixed, hence + + $mech->set_visible( "fred", "secret", [ option => "Checking" ] ) ; + +would set the first two fields to ``fred'' and ``secret'', and the I<next> +C<OPTION> menu field to ``Checking''. + +The possible field specifier types are: "text", "password", "hidden", +"textarea", "file", "image", "submit", "radio", "checkbox" and "option". + +=cut + +sub set_visible { + my $self = shift; + + my $form = $self->current_form; + my @inputs = $form->inputs; + + while (my $value = shift) { + if ( ref $value eq 'ARRAY' ) { + my ( $type, $value ) = @$value; + while ( my $input = shift @inputs ) { + next if $input->type eq 'hidden'; + if ( $input->type eq $type ) { + $input->value( $value ); + last; + } + } # while + } else { + while ( my $input = shift @inputs ) { + next if $input->type eq 'hidden'; + $input->value( $value ); + last; + } # while + } + } # while + +} # set_visible() + =head2 $mech->tick( $name, $value [, $set] ) 'Ticks' the first checkbox that has both the name and value assoicated --- t/field.t.orig 2004-01-11 19:28:50.000000000 -0800 +++ t/field.t 2004-01-11 19:59:41.000000000 -0800 @@ -2,7 +2,7 @@ use warnings; use strict; -use Test::More tests => 4; +use Test::More tests => 8; use URI::file; BEGIN { @@ -20,3 +20,13 @@ $mech->field("dingo","Modified!"); my $form = $mech->current_form(); is( $form->value( "dingo" ), "Modified!" ); + +$mech->set_visible("bingo", "bango"); +$form = $mech->current_form(); +is( $form->value( "dingo" ), "bingo" ); +is( $form->value( "bongo" ), "bango" ); + +$mech->set_visible( [ radio => "wongo!" ], "boingo" ); +$form = $mech->current_form(); +is( $form->value( "wango" ), "wongo!" ); +is( $form->find_input( "dingo", undef, 2 )->value, "boingo" ); --- t/field.html.orig 2003-06-13 08:46:42.000000000 -0700 +++ t/field.html 2004-01-11 19:59:30.000000000 -0800 @@ -7,6 +7,7 @@ <INPUT TYPE="text" NAME="dingo" VALUE="dingo1"> <INPUT TYPE="text" NAME="bongo" VALUE="bongo!"> <INPUT TYPE="radio" NAME="wango" VALUE="wango!"> +<INPUT TYPE="radio" NAME="wango" VALUE="wongo!"> <INPUT TYPE="text" NAME="dingo" VALUE="dingo2"> </FORM> </BODY> -- Peter Scott Pacific Systems Design Technologies http://www.perldebugged.com/ *** New! *** http://www.perlmedic.com/
Date: Mon, 12 Jan 2004 20:53:02 -0600
From: Andy Lester <andy [...] petdance.com>
To: Peter Scott via RT <bug-WWW-Mechanize [...] rt.cpan.org>
CC: "AdminCc of cpan Ticket #4919": ;
Subject: Re: [cpan #4919] New feature for Mech
RT-Send-Cc:
On Mon, Jan 12, 2004 at 02:38:40PM -0500, Peter Scott via RT (bug-WWW-Mechanize@rt.cpan.org) wrote: Show quoted text
> $mech->set_visible( $username, $password ) ;
That's brilliantly lazy! I'm not sure I like the name "set_visible", but I definitely will put it in. I have some form overhauling to do, and this will be part of it. Thanks. VERY cool idea. xoa -- Andy Lester => andy@petdance.com => www.petdance.com => AIM:petdance