📄 formscriptingtest.java
字号:
defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name=spectrum action='DoIt'>" + " <input type=text name=color value=green>" + "</form>" + "<a href='#' onClick='document.spectrum.submit(); return false;'>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[ 0 ].click(); assertEquals( "Result of submit", "You made it!", wc.getCurrentPage().getText() ); } public void testSubmitViaScriptButton() throws Exception { defineResource( "DoIt?color=green", "You made it!" ); defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name=spectrum action='DoIt' onsubmit='return false;'>" + " <input type=text name=color value=green>" + " <input type=button id=submitButton value=submit onClick='this.form.submit();'>" + "</form>" + "<a href='#' onClick='document.spectrum.submit(); return false;'>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getFormWithName( "spectrum" ).getButtons()[0].click(); assertEquals( "Result of submit", "You made it!", wc.getCurrentPage().getText() ); } public void testDisabledScriptButton() throws Exception { defineResource( "DoIt?color=green", "You made it!" ); defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name=spectrum action='DoIt' onsubmit='return false;'>" + " <input type=text name=color value=green>" + " <input type=button id=submitButton disabled value=submit onClick='this.form.submit();'>" + "</form>" + "<a href='#' onClick='document.spectrum.submit(); return false;'>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); try { response.getFormWithName( "spectrum" ).getButtons()[0].click(); fail( "Should not have permitted click of disabled button" ); } catch (IllegalStateException e) {} } public void testUpdateBeforeSubmit() throws Exception { defineResource( "DoIt?color=green", "You made it!" ); defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name=spectrum action='DoIt'>" + " <input type=text name=color value=red>" + " <input type=submit onClick='form.color.value=\"green\";'>" + "</form>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getFormWithName( "spectrum" ).getButtons()[0].click(); assertEquals( "Result of submit", "You made it!", wc.getCurrentPage().getText() ); } public void testSubmitButtonScript() throws Exception { defineResource( "DoIt?color=green", "You made it!" ); defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name=spectrum action='DoIt'>" + " <input type=text name=color value=red>" + " <input type=submit onClick='form.color.value=\"green\";'>" + "</form>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getFormWithName( "spectrum" ).submit(); assertEquals( "Result of submit", "You made it!", wc.getCurrentPage().getText() ); } public void testSetFormTextValue() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body onLoad=\"document.realform.color.value='green'\">" + "<form name='realform'><input name='color' value='blue'></form>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "realform" ); assertEquals( "color parameter value", "green", form.getParameterValue( "color" ) ); } public void testSetFormTextOnChangeEvent() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name='the_form'>" + " <input name='color' value='blue' " + " onChange='alert( \"color is now \" + document.the_form.color.value );'>" + "</form>" + "<a href='#' onClick='document.the_form.color.value=\"green\";'>green</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "the_form" ); assertEquals( "Initial state", "blue", form.getParameterValue( "color" ) ); assertEquals( "Alert message before change", null, wc.getNextAlert() ); form.setParameter( "color", "red" ); assertEquals( "Alert after change", "color is now red", wc.popNextAlert() ); assertEquals( "Changed state", "red", form.getParameterValue( "color" ) ); response.getLinks()[ 0 ].click(); assertEquals( "Final state", "green", form.getParameterValue( "color" ) ); assertEquals( "Alert message after JavaScript change", null, wc.getNextAlert() ); } public void testCheckboxProperties() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function viewCheckbox( checkbox ) { \n" + " alert( 'checkbox ' + checkbox.name + ' default = ' + checkbox.defaultChecked )\n;" + " alert( 'checkbox ' + checkbox.name + ' checked = ' + checkbox.checked )\n;" + " alert( 'checkbox ' + checkbox.name + ' value = ' + checkbox.value )\n;" + "}\n" + "</script></head>" + "<body>" + "<form name='realform'><input type='checkbox' name='ready' value='good'></form>" + "<a href='#' name='clear' onMouseOver='document.realform.ready.checked=false;'>clear</a>" + "<a href='#' name='set' onMouseOver='document.realform.ready.checked=true;'>set</a>" + "<a href='#' name='change' onMouseOver='document.realform.ready.value=\"waiting\";'>change</a>" + "<a href='#' name='report' onMouseOver='viewCheckbox( document.realform.ready );'>report</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "realform" ); response.getLinkWithName( "report" ).mouseOver(); verifyCheckbox( /* default */ wc, false, /* checked */ false, /* value */ "good" ); assertEquals( "initial parameter value", null, form.getParameterValue( "ready" ) ); response.getLinkWithName( "set" ).mouseOver(); assertEquals( "changed parameter value", "good", form.getParameterValue( "ready" ) ); response.getLinkWithName( "clear" ).mouseOver(); assertEquals( "final parameter value", null, form.getParameterValue( "ready" ) ); response.getLinkWithName( "change" ).mouseOver(); assertEquals( "final parameter value", null, form.getParameterValue( "ready" ) ); response.getLinkWithName( "report" ).mouseOver(); verifyCheckbox( /* default */ wc, false, /* checked */ false, /* value */ "waiting" ); form.setParameter( "ready", "waiting" ); } public void testIndexedCheckboxProperties() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function viewCheckbox( checkbox ) { \n" + " alert( 'checkbox ' + checkbox.name + ' default = ' + checkbox.defaultChecked )\n;" + " alert( 'checkbox ' + checkbox.name + ' checked = ' + checkbox.checked )\n;" + " alert( 'checkbox ' + checkbox.name + ' value = ' + checkbox.value )\n;" + "}\n" + "</script></head>" + "<body onload='viewCheckbox( document.realform.ready[0] ); viewCheckbox( document.realform.ready[1] );'>" + "<form name='realform'>" + "<input type='checkbox' name='ready' value='good' checked>" + "<input type='checkbox' name='ready' value='bad'>" + "</form>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getFormWithName( "realform" ); verifyCheckbox( /* default */ wc, true, /* checked */ true, /* value */ "good" ); verifyCheckbox( /* default */ wc, false, /* checked */ false, /* value */ "bad" ); } private void verifyCheckbox( WebClient wc, boolean defaultChecked, boolean checked, String value ) { assertEquals( "Message " + 1 + "-1", "checkbox ready default = " + defaultChecked, wc.popNextAlert() ); assertEquals( "Message " + 1 + "-2", "checkbox ready checked = " + checked, wc.popNextAlert() ); assertEquals( "Message " + 1 + "-3", "checkbox ready value = " + value, wc.popNextAlert() ); } public void testCheckboxOnClickEvent() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name='the_form'>" + " <input type='checkbox' name='color' value='blue' " + " onClick='alert( \"color-blue is now \" + document.the_form.color.checked );'>" + "</form>" + "<a href='#' onClick='document.the_form.color.checked=true;'>blue</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "the_form" ); assertEquals( "Initial state", null, form.getParameterValue( "color" ) ); assertEquals( "Alert message before change", null, wc.getNextAlert() ); form.removeParameter( "color" ); assertEquals( "Alert message w/o change", null, wc.getNextAlert() ); form.setParameter( "color", "blue" ); assertEquals( "Alert after change", "color-blue is now true", wc.popNextAlert() ); form.removeParameter( "color" ); assertEquals( "Alert after change", "color-blue is now false", wc.popNextAlert() ); assertEquals( "Changed state", null, form.getParameterValue( "color" ) ); response.getLinks()[ 0 ].click(); assertEquals( "Final state", "blue", form.getParameterValue( "color" ) ); assertEquals( "Alert message after JavaScript change", null, wc.getNextAlert() ); } public void testSetCheckboxOnClickEvent() throws Exception {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -