http://www.perlmonks.org?node_id=664303

talexb has asked for the wisdom of the Perl Monks concerning the following question:

I am using the most excellent Test::WWW::Mechanize::CGIApp to test my web application, and I appear to have run into a snag.

I want to set some values in a form, but since the form values are stored in a hash, the values that I set may be superceded by the default values. So my plan was to get all of the form's key/value pairs and copy everything over to a new list, except for the keys that I want to change.

I did this by getting the form from Mech, then extracting the key/value pairs from the form ..

my $form = $mech->form_number(1); my @keyValuePairs = $form->form();
.. then I looped through the key/value pairs, transferring over everything except the fields I'm going to be monkeying with ..
my @newKeyValuePairs; my %testFields = ('p_customer' => 1, 'p_name' => 1, 'new_users' => 1); while (@keyValuePairs) { my ($key, $value) = (shift @keyValuePairs, shift @keyValuePairs); push (@newKeyValuePairs, $key, $value) unless (exists($testFields{$key})); }
.. and finally I used Mech's set_fields method to set the form's fields.
$mech->set_fields( @newKeyValuePairs, p_customer => 'Internal', p_name => $thisProject, new_users => join ("\n", @{$newProjects{$thisProject}}), );

This produced the following errors:

Input 'rm' is readonly at /usr/lib/perl5/vendor_perl/5.8.8/WWW/Mechani +ze.pm line 1233 Input 'member' is readonly at /usr/lib/perl5/vendor_perl/5.8.8/WWW/Mec +hanize.pm line 1233 Input 'p_id' is readonly at /usr/lib/perl5/vendor_perl/5.8.8/WWW/Mecha +nize.pm line 1233
Is there a better way to solve this problem? I haven't tried just setting the field values -- I assume that because it's a hash, it will work sometimes and other times will not.

Update: Aha -- of course, the readonly variables are the variables that are 'hidden' in the original page. I don't feel like I'm that much closer to a solution, however.

Update 2: I have tried calling set_field without all of the cleverness shown above, and it seems to work properly .. so perhaps this effort was in vain. I certainly am learning a lot about using Test::WWW::Mechanize::CGIApp, in any case, and that's definitely a Good Thing. Thanks to all the monks who read this node.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re: Input 'foo' is readonly / WWW::Mechanize
by almut (Canon) on Jan 25, 2008 at 16:31 UTC

    Maybe those are hidden fields, which according to HTML::Form (used under the hood) start out readonly:

    $bool = $input->readonly
    $input->readonly( $bool )
    This method is used to get/set the value of the readonly attribute. You are allowed to modify the value of readonly inputs, but setting the value will generate some noise when warnings are enabled. Hidden fields always start out readonly.

    Update (in reply to your update):  have you tried turning off the readonly attribute to see if it helps?  (IIRC, $mech->forms returns HTML::Form objects, whose inputs method returns input objects, upon which you can then call the readonly method...)

      Thanks, but I didn't want to go that route -- if I'm getting warnings from HTML::Form not to do stuff, I don't really want to force the issue. And I seem to have resolved the issue anyway (see Update 2, above). Thanks!

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds