📄 formscriptingtest.java
字号:
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>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "the_form" ); form.toggleCheckbox( "color" ); assertEquals( "Alert after change", "color-blue is now true", wc.popNextAlert() ); form.setCheckbox( "color", false ); assertEquals( "Alert after change", "color-blue is now false", wc.popNextAlert() ); } public void testIndexedRadioProperties() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function viewRadio( radio ) { \n" + " alert( 'radio ' + radio.name + ' default = ' + radio.defaultChecked )\n;" + " alert( 'radio ' + radio.name + ' checked = ' + radio.checked )\n;" + " alert( 'radio ' + radio.name + ' value = ' + radio.value )\n;" + "}\n" + "</script></head>" + "<body onload='viewRadio( document.realform.ready[0] ); viewRadio( document.realform.ready[1] );'>" + "<form name='realform'>" + "<input type='radio' name='ready' value='good' checked>" + "<input type='radio' name='ready' value='bad'>" + "</form>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getFormWithName( "realform" ); verifyRadio( /* default */ wc, true, /* checked */ true, /* value */ "good" ); verifyRadio( /* default */ wc, false, /* checked */ false, /* value */ "bad" ); } private void verifyRadio( WebClient wc, boolean defaultChecked, boolean checked, String value ) { assertEquals( "Message " + 1 + "-1", "radio ready default = " + defaultChecked, wc.popNextAlert() ); assertEquals( "Message " + 1 + "-2", "radio ready checked = " + checked, wc.popNextAlert() ); assertEquals( "Message " + 1 + "-3", "radio ready value = " + value, wc.popNextAlert() ); } public void testRadioOnClickEvent() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name='the_form'>" + " <input type='radio' name='color' value='blue' " + " onClick='alert( \"color is now blue\" );'>" + " <input type='radio' name='color' value='red' checked" + " onClick='alert( \"color is now red\" );'>" + "</form>" + "<a href='#' onClick='document.the_form.color[1].checked=false; document.the_form.color[0].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", "red", form.getParameterValue( "color" ) ); assertEquals( "Alert message before change", null, wc.getNextAlert() ); form.setParameter( "color", "red" ); assertEquals( "Alert message w/o change", null, wc.getNextAlert() ); form.setParameter( "color", "blue" ); assertEquals( "Alert after change", "color is now blue", wc.popNextAlert() ); 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", "blue", form.getParameterValue( "color" ) ); assertEquals( "Alert message after JavaScript change", null, wc.getNextAlert() ); } public void testFormActionProperty() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "Default", "<form method=GET name='the_form' action = 'ask'>" + "<Input type=text name=age>" + "<Input type=submit value=Go>" + "</form>" + "<a href='#' name='doTell' onClick='document.the_form.action=\"tell\";'>tell</a>" + "<a href='#' name='doShow' onClick='alert( document.the_form.action );'>show</a>" ); WebResponse page = wc.getResponse( getHostPath() + "/Default.html" ); page.getLinkWithName( "doShow" ).click(); assertEquals( "Current action", "ask", wc.popNextAlert() ); page.getLinkWithName( "doTell" ).click(); WebRequest request = page.getForms()[0].getRequest(); request.setParameter( "age", "23" ); assertEquals( getHostPath() + "/tell?age=23", request.getURL().toExternalForm() ); } public void testFormTargetProperty() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "Default", "<form method=GET name='the_form' action = 'ask'>" + "<Input type=text name=age>" + "<Input type=submit value=Go>" + "</form>" + "<a href='#' name='doTell' onClick='document.the_form.target=\"_blank\";'>tell</a>" + "<a href='#' name='doShow' onClick='alert( document.the_form.target );'>show</a>" ); WebResponse page = wc.getResponse( getHostPath() + "/Default.html" ); page.getLinkWithName( "doShow" ).click(); assertEquals( "Initial target", "_top", wc.popNextAlert() ); page.getLinkWithName( "doTell" ).click(); page.getLinkWithName( "doShow" ).click(); assertEquals( "Current target", "_blank", wc.popNextAlert() ); WebRequest request = page.getForms()[0].getRequest(); assertEquals( "_blank", request.getTarget() ); } public void testFormValidationOnSubmit() throws Exception { defineResource( "doIt?color=pink", "You got it!", "text/plain" ); defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function verifyForm() { " + " if (document.realform.color.value == 'pink') {" + " return true;" + " } else {" + " alert( 'wrong color' );" + " return false;" + " }" + "}" + "</script></head>" + "<body>" + "<form name='realform' action='doIt' onSubmit='return verifyForm();'>" + " <input name='color' value='blue'>" + "</form>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "realform" ); form.submit(); assertEquals( "Alert message", "wrong color", wc.popNextAlert() ); assertSame( "Current response", response, wc.getCurrentPage() ); form.setParameter( "color", "pink" ); WebResponse newResponse = form.submit(); assertEquals( "Result of submit", "You got it!", newResponse.getText() ); } public void testFormSelectReadableProperties() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function viewSelect( choices ) { \n" + " alert( 'select has ' + choices.options.length + ' options' )\n;" + " alert( 'select still has ' + choices.length + ' options' )\n;" + " alert( 'select option ' + choices.options[0].index + ' is ' + choices.options[0].text )\n;" + " alert( 'select 2nd option value is ' + choices.options[1].value )\n;" + " if (choices.options[0].selected) alert( 'red selected' );\n" + " if (choices.options[1].selected) alert( 'blue selected' );\n" + " if (choices[1].selected) alert( 'blue selected again' );\n" + "}\n" + "</script></head>" + "<body onLoad='viewSelect( document.the_form.choices )'>" + "<form name='the_form'>" + " <select name='choices'>" + " <option value='1'>red" + " <option value='3' selected>blue" + " </select>" + "</form>" + "<a href='#' onMouseOver=\"alert( 'selected #' + document.the_form.choices.selectedIndex );\">which</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "1st message", "select has 2 options", wc.popNextAlert() ); assertEquals( "2nd message", "select still has 2 options", wc.popNextAlert() ); assertEquals( "3rd message", "select option 0 is red", wc.popNextAlert() ); assertEquals( "4th message", "select 2nd option value is 3", wc.popNextAlert() ); assertEquals( "5th message", "blue selected", wc.popNextAlert() ); assertEquals( "6th message", "blue selected again", wc.popNextAlert() ); response.getLinks()[0].mouseOver(); assertEquals( "before change message", "selected #1", wc.popNextAlert() ); response.getFormWithName( "the_form" ).setParameter( "choices", "1" ); response.getLinks()[0].mouseOver(); assertEquals( "after change message", "selected #0", wc.popNextAlert() ); } public void testFormSelectDefaults() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function viewSelect( form ) { \n" + " alert( 'first default index= ' + form.first.selectedIndex )\n;" + " alert( 'second default index= ' + form.second.selectedIndex )\n;" + " alert( 'third default index= ' + form.third.selectedIndex )\n;" + " alert( 'fourth default index= ' + form.fourth.selectedIndex )\n;" + "}\n" + "</script></head>" + "<body onLoad='viewSelect( document.the_form )'>" + "<form name='the_form'>" + " <select name='first'><option value='1'>red<option value='3'>blue</select>" + " <select name='second' multiple><option value='1'>red<option value='3'>blue</select>" + " <select name='third' size=2><option value='1'>red<option value='3'>blue</select>" + " <select name='fourth' multiple size=1><option value='1'>red<option value='3'>blue</select>" + "</form>" + "</body></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "1st message", "first default index= 0", wc.popNextAlert() ); assertEquals( "2nd message", "second default index= -1", wc.popNextAlert() ); assertEquals( "3rd message", "third default index= -1", wc.popNextAlert() ); assertEquals( "4th message", "fourth default index= 0", wc.popNextAlert() ); } public void testFileSubmitProperties() throws Exception { File file = new File( "temp.html" ); defineResource( "OnCommand.html", "<html><head></head>" + "<body'>" + "<form name='the_form'>" + " <input type='file' name='file'>" + "</form>" + "<a href='#' onMouseOver=\"alert( 'file selected is [' + document.the_form.file.value + ']' );\">which</a>" +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -