public class Animals extends TestCase {
WebConversation wc;
WebRequest req;
WebForm form;
private WebResponse resp;
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
public static TestSuite suite() {
return new TestSuite( Animals.class );
}
public Animals( String s) {
super( s );
}
public void testLogin() throws Exception{
wc = new WebConversation();
req = new GetMethodWebRequest( "http://myhost/~alex/animals.h
tml" );
resp = wc.getResponse( req );
form=resp.getForms()[0];
form.setParameter("pet", "Bird");
System.out.println("pA value is: " + form.getParameterValue("pet"));
}
}
produces this error:
There was 1 error:
1) testLogin(Animals)com.meterware.httpunit.IllegalParameterValueException: May not set parameter 'pet' to 'Bird'. Value must be one of: { }
at com.meterware.httpunit.SelectionFormControl$Options.reportNoMatches(FormControl.java:1184)
at com.meterware.httpunit.SelectionFormControl$SingleSelectOptions.claimUniqueValues(FormControl.java:1358)
at com.meterware.httpunit.SelectionFormControl$Options.claimUniqueValues(FormControl.java:1176)
at com.meterware.httpunit.SelectionFormControl.claimUniqueValue(FormControl.java:1057)
at com.meterware.httpunit.FormParameter.setValues(FormParameter.java:90)
at com.meterware.httpunit.WebForm.setParameter(WebForm.java:612)
at com.meterware.httpunit.WebForm.setParameter(WebForm.java:601)
at Animals.testLogin(Animals.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at Animals.main(Animals.java:17)
FAILURES!!!
Tests run: 1, Failures: 0, Errors: 1
on this file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-\
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<form action="my/pets" method="post">
<select name="pet" size="1">
<option>Dog</option>
<option>Cat</option>
<option>Bird</option>
<option>Fish</option>
</select>
</form>
</html>
once I remove this line from the above file:
"<html xmlns="http://www.w3.org/1999/xhtml">"
everything seems to work, I get this output:
pA value is: Bird
Time: 1.191
OK (1 test)
I looked at the Httpunit code, but could not isolate
the problem.
Any help is GREATLY appreciated,
aplotits
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I believe I have uncovered the root of the problem. Likely you are using the nekohtml library and HttpUnit defaults to switching the case of the tags to "upper". However, when it goes to look for the options, it searches for tags matching the name "option" which will not work since internally it is storing all the tags as "OPTION". A quick and dirty solution is to add this line to your setup() method:
HTMLParserFactory.setPreserveTagCase(true);
of course, all your tags must be lowercase. I am proposing a patch that should fix the problem permenantly.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
My code below:
import com.meterware.httpunit.*;
import com.meterware.servletunit.*;
import java.util.*;
import junit.framework.*;
public class Animals extends TestCase {
WebConversation wc;
WebRequest req;
WebForm form;
private WebResponse resp;
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
public static TestSuite suite() {
return new TestSuite( Animals.class );
}
public Animals( String s) {
super( s );
}
public void testLogin() throws Exception{
wc = new WebConversation();
req = new GetMethodWebRequest( "http://myhost/~alex/animals.h
tml" );
resp = wc.getResponse( req );
form=resp.getForms()[0];
form.setParameter("pet", "Bird");
System.out.println("pA value is: " + form.getParameterValue("pet"));
}
}
produces this error:
There was 1 error:
1) testLogin(Animals)com.meterware.httpunit.IllegalParameterValueException: May not set parameter 'pet' to 'Bird'. Value must be one of: { }
at com.meterware.httpunit.SelectionFormControl$Options.reportNoMatches(FormControl.java:1184)
at com.meterware.httpunit.SelectionFormControl$SingleSelectOptions.claimUniqueValues(FormControl.java:1358)
at com.meterware.httpunit.SelectionFormControl$Options.claimUniqueValues(FormControl.java:1176)
at com.meterware.httpunit.SelectionFormControl.claimUniqueValue(FormControl.java:1057)
at com.meterware.httpunit.FormParameter.setValues(FormParameter.java:90)
at com.meterware.httpunit.WebForm.setParameter(WebForm.java:612)
at com.meterware.httpunit.WebForm.setParameter(WebForm.java:601)
at Animals.testLogin(Animals.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at Animals.main(Animals.java:17)
FAILURES!!!
Tests run: 1, Failures: 0, Errors: 1
on this file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-\
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<form action="my/pets" method="post">
<select name="pet" size="1">
<option>Dog</option>
<option>Cat</option>
<option>Bird</option>
<option>Fish</option>
</select>
</form>
</html>
once I remove this line from the above file:
"<html xmlns="http://www.w3.org/1999/xhtml">"
everything seems to work, I get this output:
pA value is: Bird
Time: 1.191
OK (1 test)
I looked at the Httpunit code, but could not isolate
the problem.
Any help is GREATLY appreciated,
aplotits
I believe I have uncovered the root of the problem. Likely you are using the nekohtml library and HttpUnit defaults to switching the case of the tags to "upper". However, when it goes to look for the options, it searches for tags matching the name "option" which will not work since internally it is storing all the tags as "OPTION". A quick and dirty solution is to add this line to your setup() method:
HTMLParserFactory.setPreserveTagCase(true);
of course, all your tags must be lowercase. I am proposing a patch that should fix the problem permenantly.