When setting a parameter in a form, the 'onchange'
event causes
a 'java.lang.ArrayIndexOutOfBoundsException'. It seems
that when the 'onchange' event function is called,
HttpUnit does not initialize the listbox object correctly.
Attached is the html file (test.html) which can be used
to test the problem.
Here is the code for my function test
/* BEGIN */
WebConversation wc = Util.getWebConversation();
WebResponse resp = wc.getResponse
(test.html);
// get the registration form
WebForm form = resp.getForms()[1];
// fill form fields
form.setParameter("industryID", "3");
/* END */
When selecting an option value in a drop down list,
an 'onchange' event is performed which calls a
javascript function. This javascript function modifies the
values of a different listbox using an Array object for
the 'value' and 'text' fields of the options object.
A number of Arrays are used for the data insertion into
the listbox. Here's an example:
mapping["1"] = new Array(
new JobRole(0, "Select Job Role"),
new JobRole(236, "Management")
);
The function which is called through the 'onchange'
event takes the current listbox control as the input
argument. Then modifies
the 'document.userProfileForm.jobRoleID.options' object.
/* BEGIN JAVASCRIPT FUNCTION */
function loadOnChangeIndustry(selectl) {
// get the map for the selection
roleArray = mapping[select.options
[select.selectedIndex].value];
// reassign the new
document.userProfileForm.jobRoleID.options.length =
roleArray.length;
// add the new values to the listbox from the array
for (var i = 0; i < roleArray.length; i++) {
/* EXCEPTION IS THROWN WHEN THESE FUNCTIONS
ARE EXECUTED */
document.userProfileForm.jobRoleID.options[i].value =
roleArray[i].id;
document.userProfileForm.jobRoleID.options[i].text =
roleArray[i].text;
}
}
HTML file with javascript problem
Logged In: YES
user_id=978434
This Problem is within the rhino lib and not httpunit.
I already reported this to mozilla:
https://bugzilla.mozilla.org/show_bug.cgi?id=280671
Workaround / Fix from me:
Class = org.mozilla.javascript.ScriptRuntime
Method =public static Object getObjectElem(Scriptable obj,
Object elem, Context cx)
***********************************************
Fix code snip:
public static Object getObjectElem(Scriptable obj, Object elem,
Context cx)
{
if (obj instanceof XMLObject) {
XMLObject xmlObject = (XMLObject)obj;
return xmlObject.ecmaGet(cx, elem);
}
Object result;
String s = toStringIdOrIndex(cx, elem);
if (s == null) {
int index = lastIndexResult(cx);
// ToDo Fix von uns, da sonst ein
ArrayIndexOutOfBoundsException kommt.
if (index==-1)
index=0;
result = ScriptableObject.getProperty(obj, index);
} else {
result = ScriptableObject.getProperty(obj, s);
}
if (result == Scriptable.NOT_FOUND) {
result = Undefined.instance;
}
return result;
}
***********************************************
The Main Problem is tat the Return Value from an not
initialized dropdownbox is "-1" and not zero. Browsers dont
care, but Java dont know about the ArrayIndex[-1] :) This
have to cause an ArrayIndexOutOfBounds.
Logged In: YES
user_id=37920
Originator: NO
The change is now in the subversion repository and will be in the next build
Logged In: YES
user_id=37920
Originator: NO
This actually IS a bug in HttpUnit