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

Hi

I want to write a test script for web page that has select option box. The values of this option box are not static. They change depending on search criteria from previous page. How do I find out how many items are in the select option? and how do I say to select item # 1, or so?

Looking at documentation for TWMC, TWM, WM, TM and saw there's mech->select(). Trying this, does not select the first item or a specific item.

Is there a way to select a specific item from drop-down box whose values are different ? (i.e. they are not static values and are not created by javascript either.)

Thanks in advance! --nikie

Replies are listed 'Best First'.
Re: choosing option value in test script.
by Corion (Patriarch) on Apr 30, 2009 at 18:22 UTC

    Look again, in the documentation of WWW::Mechanize. There is ->find_all_inputs(...), which returns HTML::Form::Input elements that you can query in turn.

      ahh that's the one I didn't see. Thanks Corion for that!
        Corion, update: updated select to option (as thats the type ) I tried the find_all_inputs() but am getting this error. (I am not sure if select option is called select? or option? did try both and still got this error. Any help would be appreciated!!
        my @teacher_input = $mech->find_all_inputs( { type => 'option', name => 'teacher', } ); Reference found where even-sized list expected at /usr/local/share/per +l/5.10.0/WWW/Mechanize.pm line 973. at /usr/local/share/perl/5.10.0/WWW/Mechanize.pm line 973
        I was using the example in the WM doc, and used type instead of type_regex.
        # get all text or textarea controls called "customer" my @customer_text_inputs = $mech->find_all_inputs( { type_regex => qr/^(text|textarea)$/, name => 'customer', } );


        Thanks for help once again in advance!
Re: choosing option value in test script.
by kennethk (Abbot) on Apr 30, 2009 at 18:26 UTC
    From the WWW::Mechanize documentation:
    $mech->select($name, \@values)

    Given the name of a select field, set its value to the value specified. If the field is not <select multiple> and the $value is an array, only the first value will be set. [Note: the documentation previously claimed that only the last value would be set, but this was incorrect.] Passing $value as a hash with an n key selects an item by number (e.g. {n => 3} or { {n = [2,4]} >>). The numbering starts at 1. This applies to the current form.

    Returns 1 on successfully setting the value. On failure, returns undef and calls $self>warn() with an error message.

    Without knowing what the options are, you can just loop from 1 until the select fails.

      I did try that, and unfortunately, instead of selecting/finding, what it does it submits to database, with the value of select. (which is not what I want though. )

      Thanks Kenneth for help!