Menu

Httpunit submit form with dropdown

Help
kennethmch
2005-03-18
2013-04-26
  • kennethmch

    kennethmch - 2005-03-18

    I have a form like below.
    <form name="testform">
    <select name="testselect" onChange()="js">
    <option value="1">one</option>
    <option value="2">two</option>
    </select>

    which the javascript will invoke submit();

    In my httpunit, when I do
    form.setParameter("testselect","2");
    it gives me a illegalParameterValueException.
    com.meterware.httpunit.IllegalParameterValueException: May not set parameter 'testselect' to '2'. Value must be one of: {  }

    Anyone know why is that? I am pretty sure I am at the right page and form. Also, when I call form.getOptions(), it returns me a empty String[].

    Hope someone can give me a hint.
    thanks for your time.

    kenneth

     
    • JFB

      JFB - 2005-04-05

      It happens because of the javascript.  You can get around this by using the following code . . .

          protected WebRequest modifyParameterArray(WebRequest form,String name, String value,int arrayIndex){
              
              String[] currentValues = form.getParameterValues( name );
              java.util.Vector values = new java.util.Vector();
              
              if (arrayIndex != -1){
      //         change all values to the one passed in
              }
              else{
      //        change only the 1 value identified by the arrayIndex to the value passed in
              currentValues[arrayIndex] = value;
              
              for (int x = 0; x < currentValues.length; x++){
              values.add (currentValues[x]);
              
              }
              }
      //        save value back to form
              form.setParameter( name, (String[])values.toArray( new String[0] ) ); 
              return form; 
              }

      ... bunch of code here . . .

                  WebForm testForm = response.getFormWithName("testform");
                  request = testForm.getRequest();
                 
                  request = modifyParameterArray(request,"search_type","address",0);

      ... more code ...

      The modifyParameterArray can be called as many times for as many values as is required.

      Hopefully this will bypass this problem . . .

      BigFred

       

Log in to post a comment.

MongoDB Logo MongoDB