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

I am attempting to write a Mechanize test script for a form that contains pairs of duplicate field names. E.g.,
<select id="birth_month" name="birthdate"> <option value=""> -- </option> <option value="01"> Jan </option> ... </select> <select id="birth_day" name="birthdate"> <option value="01"> 01 </option> ... </select>
There doesn't seem to be a straightforward approach to setting both fields at the same time.
$mech = Test::WWW::Mechanize->new(); ... $mech->submit_form( with_fields => { ..., 'birthdate' => ['01',0] }); # or $mech->set_fields({ ..., 'birthdate' => ['01',0] });
Both of these set only a single value.
$mech->field('birthdate', ['01'], 0); $mech->field('birthdate', ['17'], 1);
and the first setting is lost (overwritten by the second). I also tried HTTP::Recorder as a proxy to see what would be submitted, but it just uses the last approach (multiple calls to 'field' without apparent success). Yes, I could alter the form to avoid this, but there's a fair amount of work downstream if I do. I just want to send two values through the form under one CGI parameter, just as the real form does in the browser.

Jeff Boes <>< jeff.boes@gmail.com

Replies are listed 'Best First'.
Re: Duplicate field names and Test::WWW::Mechanize
by roboticus (Chancellor) on Aug 14, 2007 at 14:48 UTC
    Mur:

    I've never used WWW::Mechanize before, but reading the perldoc on it suggests that you might try the select() method, like:

    $mech->select('birth_month', '01'); $mech->select('birth_day', '17');

    ...roboticus

Re: Duplicate field names and Test::WWW::Mechanize
by ww (Archbishop) on Aug 14, 2007 at 19:40 UTC
    Jeff...

    I don't know how much "a fair amount of work downstream" amounts to, nor what timing issues are involved, but having two fields with the same name, in the same form, seems to me not a good idea (Service Mark pending).

    From w3c (Forms in HTML documents):

    name = cdata [CI]
    This attribute names the element so that it may be referred to from style sheets or scripts. Note. This attribute has been included for backwards compatibility. Applications should use the id attribute to identify elements.
    Emphasis in original

    Further, albeit specifically asserted only in reference to anchors, w3c (global structure) notes that id and name "share the same namespace."

    I have seen (sorry, no cite handy) discussions that suggest w3c will go beyond merely deprecating name in the context you're using it. How soon? Who knows, and yes, it'll be some time before browsers catch up, if it actually happens, but you could find yourself under the gun, if you hold off on fixing whatever's needed until there's a (practical) deadline stareing you in the face.

    In fact, I suspect (without testing, since you don't specify the "work downstream") that either
    1. You could change the <select id="birth_day" name="birthdate"> to <c><select id="birth_day" name="birth_day"> without having to make any further changes...
    2. or, you may need only to make minor changes in the script parsing the form as submitted.

    If the form works, as is, in a browser, the former seems likely, because otherwise, I would not expect the form to distinguish between the duplicate names.

Re: Duplicate field names and Test::WWW::Mechanize
by serf (Chaplain) on Aug 15, 2007 at 08:42 UTC

    Hi Mur,

    I had the same kind of problem with needing to tick check boxes with the same name, (and in my case I was talking to a closed source vendor supplied application written in Java so changing the field names was not an option!)

    If you have a look at Ticking multiple checkboxes with WWW::Mechanize / WWW::Mechanize::Shell hopefully that should solve your problem.

    Let us know how you get on please.

    Good luck,

    John