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


In reply to Input 'foo' is readonly / WWW::Mechanize by talexb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.