📄 scriptingtest.java
字号:
package com.meterware.httpunit.javascript;/******************************************************************************************************************** * $Id: ScriptingTest.java,v 1.69 2005/09/05 23:41:39 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 junit.framework.TestSuite;import junit.textui.TestRunner;import java.util.ArrayList;/** * * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a> **/public class ScriptingTest extends HttpUnitTest { public static void main( String args[] ) { TestRunner.run( suite() ); } public static TestSuite suite() { return new TestSuite( ScriptingTest.class ); } public ScriptingTest( String name ) { super( name ); } public void testJavaScriptURLWithValue() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<a href='JavaScript:\"You made it!\"'>go</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[0].click(); assertEquals( "New page", "You made it!", wc.getCurrentPage().getText() ); assertEquals( "New URL", "javascript:\"You made it!\"", wc.getCurrentPage().getURL().toExternalForm() ); } public void testJavaScriptURLWithNoValue() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<a href=\"javascript:alert( 'Hi there!' )\">go</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebResponse myPage = response.getLinks()[0].click(); assertEquals( "Alert message", "Hi there!", wc.popNextAlert() ); assertEquals( "Current page URL", getHostPath() + "/OnCommand.html", wc.getCurrentPage().getURL().toExternalForm() ); assertEquals( "Returned page URL", getHostPath() + "/OnCommand.html", myPage.getURL().toExternalForm() ); } public void testInitialJavaScriptURL() throws Exception { WebConversation wc = new WebConversation(); GetMethodWebRequest request = new GetMethodWebRequest( "javascript:alert( 'Hi there!' )" ); assertEquals( "Javascript URL", "javascript:alert( 'Hi there!' )", request.getURL().toExternalForm() ); wc.getResponse( request ); assertEquals( "Alert message", "Hi there!", wc.popNextAlert() ); } public void testJavaScriptURLWithVariables() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<a href='javascript:\"Our winner is... \" + document.the_form.winner.value'>go</a>" + "<form name='the_form'>" + " <input name=winner type=text value='George of the Jungle'>" + "</form></body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[0].click(); assertEquals( "New page", "Our winner is... George of the Jungle", wc.getCurrentPage().getText() ); } public void testJavaScriptURLWithQuestionMark() throws Exception { defineResource( "/appname/HandleAction/report?type=C", "You made it!" ); defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<a href=\"javascript:redirect('/appname/HandleAction/report?type=C')\">go</a>" + "<script language='JavaScript'>" + " function redirect( url ) { window.location=url; }" + "</script>" + "</form></body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[0].click(); assertEquals( "New page", "You made it!", wc.getCurrentPage().getText() ); } public void testJavaScriptURLWithIncludedFunction() throws Exception { defineResource( "saycheese.js", "function sayCheese() { alert( \"Cheese!\" ); }" ); defineResource( "OnCommand.html", "<html><head><script language='JavaScript' src='saycheese.js'>" + "</script></head>" + "<body>" + "<a href=\"javascript:sayCheese()\">go</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinkWith( "go" ).click(); assertEquals( "Alert message", "Cheese!", wc.popNextAlert() ); } public void testJavaScriptURLInNewWindow() throws Exception { defineWebPage( "OnCommand", "<input type='button' id='nowindow' onClick='alert(\"hi\")'></input>\n" + "<input type='button' id='withwindow' onClick=\"window.open('javascript:alert(\\'hi\\')','_self')\"></input>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); Button button1 = (Button) response.getElementWithID( "nowindow" ); Button button2 = (Button) response.getElementWithID( "withwindow" ); button1.click(); assertEquals( "Alert message 1", "hi", wc.popNextAlert() ); button2.click(); assertEquals( "Alert message 2", "hi", wc.popNextAlert() ); } public void testSingleCommandOnLoad() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body onLoad='alert(\"Ouch!\")'></body>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertNotNull( "No alert detected", wc.getNextAlert() ); assertEquals( "Alert message", "Ouch!", wc.popNextAlert() ); assertNull( "Alert should have been removed", wc.getNextAlert() ); } public void testOnLoadErrorBypass() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body onLoad='noSuchFunction()'>" + "<img src=sample.jpg>" + "</body>" ); WebConversation wc = new WebConversation(); HttpUnitOptions.setExceptionsThrownOnScriptError( false ); HttpUnitOptions.clearScriptErrorMessages(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Number of images on page", 1, response.getImages().length ); assertEquals( "Number of script failures logged", 1, HttpUnitOptions.getScriptErrorMessages().length ); } public void testConfirmationDialog() throws Exception { defineWebPage( "OnCommand", "<a href='NextPage' id='go' onClick='return confirm( \"go on?\" );'>" ); defineResource( "NextPage", "Got the next page!" ); WebConversation wc = new WebConversation(); WebResponse wr = wc.getResponse( getHostPath() + "/OnCommand.html" ); wc.setDialogResponder( new DialogAdapter() { public boolean getConfirmation( String confirmationPrompt ) { assertEquals( "Confirmation prompt", "go on?", confirmationPrompt ); return false; } } ); wr.getLinkWithID( "go" ).click(); assertEquals( "Current page", wr, wc.getCurrentPage() ); wc.setDialogResponder( new DialogAdapter() ); wr.getLinkWithID( "go" ).click(); assertEquals( "Page after confirmation", "Got the next page!", wc.getCurrentPage().getText() ); } public void testPromptDialog() throws Exception { defineWebPage( "OnCommand", "<a href='NextPage' id='go' onClick='return \"yes\" == prompt( \"go on?\", \"no\" );'>" ); defineResource( "NextPage", "Got the next page!" ); WebConversation wc = new WebConversation(); WebResponse wr = wc.getResponse( getHostPath() + "/OnCommand.html" ); wr.getLinkWithID( "go" ).click(); assertEquals( "Current page", wr, wc.getCurrentPage() ); wc.setDialogResponder( new DialogAdapter() { public String getUserResponse( String prompt, String defaultResponse ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -