📄 formscriptingtest.java
字号:
"</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[0].mouseOver(); assertEquals( "1st message", "file selected is []", wc.popNextAlert() ); WebForm form = response.getFormWithName( "the_form" ); form.setParameter( "file", new UploadFileSpec[] { new UploadFileSpec( file ) } ); response.getLinks()[0].mouseOver(); assertEquals( "2nd message", "file selected is [" + file.getAbsolutePath() + "]", wc.popNextAlert() ); } public void testFormSelectOnChangeEvent() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function selectOptionNum( the_select, index ) { \n" + " for (var i = 0; i < the_select.length; i++) {\n" + " the_select.options[i].selected = (i == index);\n" + " }\n" + "}\n" + "</script></head>" + "<body>" + "<form name='the_form'>" + " <select name='choices' onChange='alert( \"Selected index is \" + document.the_form.choices.selectedIndex );'>" + " <option>red" + " <option selected>blue" + " </select>" + "</form>" + "<a href='#' onClick='selectOptionNum( document.the_form.choices, 0 )'>reset</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); final WebForm form = response.getFormWithName( "the_form" ); assertEquals( "Initial state", "blue", form.getParameterValue( "choices" ) ); assertEquals( "Alert message before change", null, wc.getNextAlert() ); form.setParameter( "choices", "red" ); assertEquals( "Alert after change", "Selected index is 0", wc.popNextAlert() ); form.setParameter( "choices", "blue" ); assertEquals( "Alert after change", "Selected index is 1", wc.popNextAlert() ); assertEquals( "Initial state", "blue", form.getParameterValue( "choices" ) ); response.getLinks()[ 0 ].click(); assertEquals( "Final state", "red", form.getParameterValue( "choices" ) ); assertEquals( "Alert message after JavaScript change", null, wc.getNextAlert() ); } public void testFormSelectWriteableProperties() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name='the_form'>" + " <select name='choices'>" + " <option value='1'>red" + " <option value='3'>blue" + " <option value='5'>green" + " <option value='7'>azure" + " </select>" + "</form>" + "<a href='#' onclick='alert( \"Selected index is \" + document.the_form.choices.selectedIndex );'>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "the_form" ); assertEquals( "initial selection", "1", form.getParameterValue( "choices" ) ); response.getLinks()[0].click(); assertEquals( "Notification", "Selected index is 0", wc.popNextAlert() ); } public void testFormSelectDefaultProperties() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function selectOptionNum( the_select, index ) { \n" + " for (var i = 0; i < the_select.length; i++) {\n" + " if (i == index) the_select.options[i].selected = true;\n" + " }\n" + "}\n" + "</script></head>" + "<body>" + "<form name='the_form'>" + " <select name='choices'>" + " <option value='1'>red" + " <option value='3' selected>blue" + " <option value='5'>green" + " <option value='7'>azure" + " </select>" + "</form>" + "<a href='#' onClick='selectOptionNum( document.the_form.choices, 2 )'>green</a>" + "<a href='#' onClick='selectOptionNum( document.the_form.choices, 0 )'>red</a>" + "<a href='#' onClick='document.the_form.choices.options[0].value=\"9\"'>red</a>" + "<a href='#' onClick='document.the_form.choices.options[0].text=\"orange\"'>orange</a>" + "<a href='#' onClick='document.the_form.choices.selectedIndex=3'>azure</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "the_form" ); assertEquals( "initial selection", "3", form.getParameterValue( "choices" ) ); response.getLinks()[0].click(); assertEquals( "2nd selection", "5", form.getParameterValue( "choices" ) ); response.getLinks()[1].click(); assertEquals( "3rd selection", "1", form.getParameterValue( "choices" ) ); response.getLinks()[2].click(); assertEquals( "4th selection", "9", form.getParameterValue( "choices" ) ); assertMatchingSet( "Displayed options", new String[] { "red", "blue", "green", "azure" }, form.getOptions( "choices" ) ); response.getLinks()[3].click(); assertMatchingSet( "Modified options", new String[] { "orange", "blue", "green", "azure" }, form.getOptions( "choices" ) ); response.getLinks()[4].click(); assertEquals( "5th selection", "7", form.getParameterValue( "choices" ) ); } public void testFormSelectOverwriteOptions() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function rewriteSelect( the_select ) { \n" + " the_select.options[0] = new Option( 'apache', 'a' );\n" + " the_select.options[1] = new Option( 'comanche', 'c' );\n" + " the_select.options[2] = new Option( 'sioux', 'x' );\n" + " the_select.options[3] = new Option( 'iriquois', 'q' );\n" + "}\n" + "</script></head>" + "<body>" + "<form name='the_form'>" + " <select name='choices'>" + " <option value='1'>red" + " <option value='2'>yellow" + " <option value='3' selected>blue" + " <option value='5'>green" + " </select>" + "</form>" + "<a href='#' onMouseOver='document.the_form.choices.options.length=3;'>shorter</a>" + "<a href='#' onMouseOver='document.the_form.choices.options[1]=null;'>weed</a>" + "<a href='#' onMouseOver='rewriteSelect( document.the_form.choices );'>replace</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "the_form" ); assertMatchingSet( "initial values", new String[] { "1", "2", "3", "5" }, form.getOptionValues( "choices" ) ); assertMatchingSet( "initial text", new String[] { "red", "yellow", "blue", "green" }, form.getOptions( "choices" ) ); response.getLinks()[0].mouseOver(); assertMatchingSet( "modified values", new String[] { "1", "2", "3" }, form.getOptionValues( "choices" ) ); assertMatchingSet( "modified text", new String[] { "red", "yellow", "blue" }, form.getOptions( "choices" ) ); response.getLinks()[1].mouseOver(); assertMatchingSet( "weeded values", new String[] { "1", "3" }, form.getOptionValues( "choices" ) ); assertMatchingSet( "weeded text", new String[] { "red", "blue" }, form.getOptions( "choices" ) ); response.getLinks()[2].mouseOver(); assertMatchingSet( "replaced values", new String[] { "a", "c", "x", "q" }, form.getOptionValues( "choices" ) ); assertMatchingSet( "replaced text", new String[] { "apache", "comanche", "sioux", "iriquois" }, form.getOptions( "choices" ) ); } public void testAccessAcrossFrames() throws Exception { defineResource( "First.html", "<html><head><script language='JavaScript'>" + "function accessOtherFrame() {" + " top.frame2.document.testform.param1.value = 'new1';" + " window.alert('value: ' + top.frame2.document.testform.param1.value);" + "}" + "</script><body onload='accessOtherFrame();'>" + "</body></html>" ); defineWebPage( "Second", "<form method=post name=testform action='http://trinity/dummy'>" + " <input type=hidden name='param1' value='old1'></form>" ); defineResource( "Frames.html", "<html><head><title>Initial</title></head>" + "<frameset cols=\"20%,80%\">" + " <frame src='First.html' name='frame1'>" + " <frame src='Second.html' name='frame2'>" + "</frameset></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/Frames.html" ); assertEquals( "Alert message", "value: new1", wc.popNextAlert() ); } public void testSetFromEmbeddedScript() throws Exception { defineWebPage( "OnCommand", "<form name=\"testform\">" + "<input type=text name=\"testfield\" value=\"old\">" + "</form>" + "<script language=\"JavaScript\">" + " document.testform.testfield.value=\"new\"" + "</script>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Form parameter value", "new", response.getForms()[0].getParameterValue( "testfield") ); } public void testSubmitFromJavaScriptLink() throws Exception { defineResource( "test2.txt?Submit=Submit", "You made it!", "text/plain" ); defineWebPage( "OnCommand", "<form name='myform' action='test2.txt'>" + " <input type='submit' id='btn' name='Submit' value='Submit'/>" + " <a href='javascript:document.myform.btn.click();'>Link</a>" + "</form>" ); WebConversation wc = new WebConversation(); WebResponse wr = wc.getResponse( getHostPath() + "/OnCommand.html" ); wr.getLinkWith( "Link" ).click(); } public void testSubmitOnLoad() throws Exception { defineResource( "test2.txt?Submit=Submit", "You made it!", "text/plain" ); defineResource( "OnCommand.html", "<html><body onload='document.myform.btn.click();'>" + "<form name='myform' action='test2.txt'>" + " <input type='submit' id='btn' name='Submit' value='Submit'/>" + "</form></body></html>" ); WebConversation wc = new WebConversation(); WebResponse wr = wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "current page URL", getHostPath() + "/test2.txt?Su
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -