📄 documentscriptingtest.java
字号:
package com.meterware.httpunit.javascript;/******************************************************************************************************************** * $Id: DocumentScriptingTest.java,v 1.12 2004/10/13 15:11:51 russgold Exp $ * * Copyright (c) 2002, 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 junit.framework.TestSuite;import junit.textui.TestRunner;import com.meterware.httpunit.*;public class DocumentScriptingTest extends HttpUnitTest { public static void main( String args[] ) { TestRunner.run( suite() ); } public static TestSuite suite() { return new TestSuite( DocumentScriptingTest.class ); } public DocumentScriptingTest( String name ) { super( name ); } public void testDocumentTitle() throws Exception { defineResource( "OnCommand.html", "<html><head><title>Amazing!</title></head>" + "<body onLoad='alert(\"Window title is \" + document.title)'></body>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message", "Window title is Amazing!", wc.popNextAlert() ); } public void testDocumentFindForms() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function getFound( object ) {" + " return (object == null) ? \"did not find \" : \"found \";" + " }" + "function viewForms() { " + " alert( \"found \" + document.forms.length + \" form(s)\" );" + " alert( getFound( document.realform ) + \"form 'realform'\" );" + " alert( getFound( document.forms[\"realform\"] ) + \"form 'forms[\'realform\']'\" );" + " alert( getFound( document.noform ) + \"form 'noform'\" ); }" + "</script></head>" + "<body onLoad='viewForms()'>" + "<form name='realform'></form>" + "</body></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message", "found 1 form(s)", wc.popNextAlert() ); assertEquals( "Alert message", "found form 'realform'", wc.popNextAlert() ); assertEquals( "Alert message", "found form 'forms[\'realform\']'", wc.popNextAlert() ); assertEquals( "Alert message", "did not find form 'noform'", wc.popNextAlert() ); assertNull( "Alert should have been removed", wc.getNextAlert() ); } public void testDocumentFindLinks() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function getFound( object ) {" + " return (object == null) ? \"did not find \" : \"found \";" + " }" + "function viewLinks() { " + " alert( \"found \" + document.links.length + \" link(s)\" );" + " alert( getFound( document.reallink ) + \"link 'reallink'\" );" + " alert( getFound( document.links[\"reallink\"] ) + \"link 'links[reallink]'\" );" + " alert( getFound( document.nolink ) + \"link 'nolink'\" );" + "}" + "</script></head>" + "<body onLoad='viewLinks()'>" + "<a href='something' name='reallink'>first</a>" + "<a href='else'>second</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message", "found 2 link(s)", wc.popNextAlert() ); assertEquals( "Alert message", "found link 'reallink'", wc.popNextAlert() ); assertEquals( "Alert message", "found link 'links[reallink]'", wc.popNextAlert() ); assertEquals( "Alert message", "did not find link 'nolink'", wc.popNextAlert() ); assertNull( "Alert should have been removed", wc.getNextAlert() ); } public void testJavaScriptObjectIdentity() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function compareLinks() { " + " if (document.reallink == document.links['reallink']) {" + " alert( 'they are the same' );" + " } else {" + " alert( 'they are different' );" + " }" + "}" + "</script></head>" + "<body onLoad='compareLinks()'>" + "<a href='something' name='reallink'>first</a>" + "<a href='else'>second</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message", "they are the same", wc.popNextAlert() ); } public void testCaseSensitiveNames() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name='item' action='run'></form>" + "<a name='Item' href='sample.html'></a>" + "<a href='#' name='first' onMouseOver='alert( document.item.action );'>1</a>" + "<a href='#' name='second' onMouseOver='alert( document.Item.href );'>2</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinkWithName( "first" ).mouseOver(); assertEquals( "form action", "run", wc.popNextAlert() ); response.getLinkWithName( "second" ).mouseOver(); assertEquals( "link href", getHostPath() + "/sample.html", wc.popNextAlert() ); } public void testLinkMouseOverEvent() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name='realform'><input name='color' value='blue'></form>" + "<a href='#' onMouseOver=\"document.realform.color.value='green';return false;\">green</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "realform" ); WebLink link = response.getLinks()[0]; assertEquals( "initial parameter value", "blue", form.getParameterValue( "color" ) ); link.mouseOver(); assertEquals( "changed parameter value", "green", form.getParameterValue( "color" ) ); } public void testLinkClickEvent() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name='realform'><input name='color' value='blue'></form>" + "<a href='nothing.html' onClick=\"JavaScript:document.realform.color.value='green';return false;\">green</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "realform" ); WebLink link = response.getLinks()[0]; assertEquals( "initial parameter value", "blue", form.getParameterValue( "color" ) ); link.click(); assertEquals( "changed parameter value", "green", form.getParameterValue( "color" ) ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -