Support for arbitrary attributes
Brought to you by:
russgold
We use a lot of proprietary attributes in JavaScript via setAttribute() getAttribute(). This patch adds support for arbitrary attributes in HttpUnit. It also prevents errors when referring the event object and calling the onchange() function/event explicitly.
No test case is provided. This may be a bit blunt, but should at least serve as a starting point.
Patch for arbitrary attributes
Logged In: YES
user_id=1220573
Originator: NO
Thank you for the patch. Given 709 existing test cases with a test case even for one-liners it looks like a bit of testing would do what you are trying to do really good. Do you have a small javascript example that uses the setAttribute and getAttribute functionality?
I'm looking forward to your contribution!
Logged In: YES
user_id=692656
Originator: YES
A minimal snippet:
<input type="text" id="foo" name="foo" myattr="bar" />
...
var field = document.getElementById("foo");
var attributeValue = field.getAttribute("myattr");
alert("The attribute value is " + attributeValue);
field.setAttribute("myattr", "new_attribute_value");
Let me know if I can be of further help.
Test case
Logged In: YES
user_id=692656
Originator: YES
File Added: httpunit_getsetAttribute_test.patch
Logged In: YES
user_id=692656
Originator: YES
I realized I forgot to test removeAttribute in the test case.
Logged In: YES
user_id=1220573
Originator: NO
The patch is now in the subversion repository I'm going to comment on the mailinglist. Please note that the small onChange and event changes are in fact separate issues (see test case below) both are not covered fully by the changes made yet - please check and file new Bug reports if necessary.
/**
* test for Patch proposal 1653410
* Date: 2008-01-08 15:49
* @author Mattias Jiderhamn (mattias78)
*/
public void testSetAttribute() throws Exception {
/*
*
A minimal snippet:
<input type="text" id="foo" name="foo" myattr="bar" />
...
var field = document.getElementById("foo");
var attributeValue = field.getAttribute("myattr");
alert("The attribute value is " + attributeValue);
field.setAttribute("myattr", "new_attribute_value");
*/
// will only work with Dom based scripting engine before patch
// needs addCustomAttribute for old scriptin engine
if (HttpUnitOptions.DEFAULT_SCRIPT_ENGINE_FACTORY.equals(HttpUnitOptions.ORIGINAL_SCRIPTING_ENGINE_FACTORY)) {
HttpUnitOptions.addCustomAttribute("myattr");
}
defineResource( "start.html",
"<html><head>\n"+
"<script language='JavaScript'>\n"+
"function testAttributes() {\n"+
"var field = document.getElementById(\"foo\");"+
"var attributeValue = field.getAttribute(\"myattr\");"+
"alert('The attribute value is ' + attributeValue);\n" +
"field.setAttribute(\"myattr\", \"newValue\");\n"+
"alert('The attribute value is changed to ' + field.getAttribute('myattr'));\n" +
"}\n"+
"</script>\n" +
"</head>\n" +
"<body id='body_id' onLoad='testAttributes();'>"+
"<form name='the_form'><input type=\"text\" id=\"foo\" name=\"foo\" myattr=\"bar\" /></form>"+
"</body></html");
WebConversation wc = new WebConversation();
wc.getResponse( getHostPath() + "/start.html" );
assertEquals( "The attribute value is bar", wc.popNextAlert() );
assertEquals( "The attribute value is changed to newValue", wc.popNextAlert() );
// } // if
}
/**
* test for onChange part of Patch proposal 1653410
* calling on change from javascript
* used to throw com.meterware.httpunit.ScriptException: Event 'callonChange();' failed: org.mozilla.javascript.EcmaError: TypeError: Cannot find function onChange. (httpunit#6)
* after patch
* @throws Exception
*/
public void testCallOnChange() throws Exception {
defineResource( "start.html",
"<html><head>\n"+
"<script language='JavaScript'>\n"+
"function onChangeHandler() {\n"+
"alert('onChange has been called');\n" +
"}\n"+
"function callonChange() {\n"+
"alert('calling onChange');\n" +
"// fire onChangeHandler directly\n"+
"onChangeHandler();\n"+
"var field = document.getElementById(\"foo\");\n"+
"// fire onChangeHandler indirectly via event\n"+
"field.onchange();\n"+
"}\n"+
"</script>\n" +
"</head>\n" +
"<body id='body_id' onLoad='callonChange();'>"+
"<form name='the_form'><input type=\"text\" onchange='onChangeHandler' id=\"foo\" name=\"foo\" /></form>"+
"</body></html");
WebConversation wc = new WebConversation();
wc.getResponse( getHostPath() + "/start.html" );
String firstAlert=wc.popNextAlert();
assertEquals("calling onChange",firstAlert);
String secondAlert=wc.popNextAlert();
assertEquals("2nd","onChange has been called",secondAlert);
String thirdAlert=wc.popNextAlert();
// TODO make this work
// assertEquals("3rd","onChange has been called",thirdAlert);
}
/**
* test for window event part of Patch proposal 1653410
* @throws Exception
*/
public void testWindowEvent() throws Exception {
defineWebPage( "OnCommand",
"<html><head>\n"+
"<script language='JavaScript'>\n"+
"function buttonclick() {\n"+
"alert('hi');\n"+
"var event=window.event;\n" +
"}\n"+
"</script>\n" +
"</head><body onload='buttonclick'>\n" +
"<form id='someform'><input type='button' id='button1' onClick='buttonclick'></input></form>\n"+
"</body></html");
WebConversation wc = new WebConversation();
WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" );
Button button1 = (Button) response.getElementWithID( "button1" );
button1.click();
// TODO make this work
// assertEquals( "Alert message 1", "hi", wc.popNextAlert() );
}