📄 formscriptingtest.java
字号:
package com.meterware.httpunit.javascript;/******************************************************************************************************************** * $Id: FormScriptingTest.java,v 1.35 2004/12/26 20:33:35 russgold Exp $ * * Copyright (c) 2002-2004, Russell Gold * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and * to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions * of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * *******************************************************************************************************************/import com.meterware.httpunit.*;import com.meterware.pseudoserver.PseudoServlet;import com.meterware.pseudoserver.WebResource;import java.io.IOException;import java.io.File;import java.util.ArrayList;import java.util.List;import junit.textui.TestRunner;import junit.framework.TestSuite;import org.xml.sax.SAXException;/** * * @author <a href="mailto:russgold@acm.org">Russell Gold</a> **/public class FormScriptingTest extends HttpUnitTest { public static void main( String args[] ) { TestRunner.run( suite() ); } public static TestSuite suite() { return new TestSuite( FormScriptingTest.class ); } public FormScriptingTest( String name ) { super( name ); } public void testElementsProperty() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function listElements( form ) {\n" + " elements = form.elements;\n" + " alert( 'form has ' + elements.length + ' elements' );\n" + " alert( 'value is ' + elements['first'].value );\n" + " alert( 'index is ' + elements[1].selectedIndex );\n" + " elements[2].checked=true;\n" + "}" + "</script></head>" + "<body>" + "<form name='the_form'>" + " <input type=text name=first value='Initial Text'>" + " <select name='choices'>" + " <option>red" + " <option selected>blue" + " </select>" + " <input type=checkbox name=ready>" + "</form>" + "<a href='#' onClick='listElements( document.the_form )'>elements</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); final WebForm form = response.getFormWithName( "the_form" ); assertEquals( "Initial state", null, form.getParameterValue( "ready" ) ); response.getLinks()[ 0 ].click(); assertEquals( "Message 1", "form has 3 elements", wc.popNextAlert() ); assertEquals( "Message 2", "value is Initial Text", wc.popNextAlert() ); assertEquals( "Message 3", "index is 1", wc.popNextAlert() ); assertEquals( "Changed state", "on", form.getParameterValue( "ready" ) ); } public void testResetViaScript() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name=spectrum action='DoIt'>" + " <input type=text name=color value=green>" + " <input type=text name=change value=color>" + "</form>" + "<a href='#' onClick='document.spectrum.reset(); return false;'>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "spectrum" ); form.setParameter( "color", "blue" ); response.getLinks()[ 0 ].click(); assertEquals( "Value after reset", "green", form.getParameterValue( "color" ) ); } public void testOnResetEvent() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name=spectrum action='DoIt' onreset='alert( \"Ran the event\" );'>" + " <input type=text name=color value=green>" + " <input type=reset id='clear'>" + "</form>" + "<a href='#' onClick='document.spectrum.reset(); return false;'>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "spectrum" ); form.setParameter( "color", "blue" ); form.getButtonWithID( "clear" ).click(); assertEquals( "Value after reset", "green", form.getParameterValue( "color" ) ); assertEquals( "Alert message", "Ran the event", wc.popNextAlert() ); form.setParameter( "color", "blue" ); response.getLinks()[ 0 ].click(); assertEquals( "Value after reset", "green", form.getParameterValue( "color" ) ); assertNull( "Event ran unexpectedly", wc.getNextAlert() ); } public void testSubmitViaScript() 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=green>" + " <input type=submit name=change value=color>" + " <input type=submit name=keep value=nothing>" + "</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() ); } /** * Verifies bug #959918 */ public void testNumericParameterSetting1() throws Exception { defineResource( "DoIt?id=1234", "You made it!" ); defineResource( "OnCommand.html", "<html><head>" + "<script>" + " function myFunction(value) {" + " document.mainForm.id = value;" + " document.mainForm.submit();" + " }</script>" + "</head>" + "<body>" + "<form name=mainForm action='DoIt'>" + " <a href='javascript:myFunction(1234)'>View Asset</a>" + " <input type='hidden' name='id'>" + "</form>" + "</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() ); } /** * Verifies bug #1087180 */ public void testNumericParameterSetting2() throws Exception { defineResource( "DoIt?id=1234", "You made it!" ); defineResource( "OnCommand.html", "<html><head>" + "<script>" + " function myFunction(value) {" + " document.mainForm.id.value = value;" + " document.mainForm.submit();" + " }</script>" + "</head>" + "<body>" + "<form name=mainForm action='DoIt'>" + " <a href='javascript:myFunction(1234)'>View Asset</a>" + " <input type='hidden' name='id'>" + "</form>" + "</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() ); } /** * Verifies bug #1073810 (Null pointer exception if javascript sets control value to null) */ public void testNullParameterSetting() throws Exception { defineResource( "OnCommand.html", "<html><head>" + "<script>" + " function myFunction(value) {" + " document.mainForm.id.value = null;" + " }</script>" + "</head>" +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -