Thread: [Httpunit-commit] CVS: httpunit/test/com/meterware/httpunit/javascript FormScriptingTest.java,1.5,1.
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-08-23 19:33:16
|
Update of /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/javascript In directory usw-pr-cvs1:/tmp/cvs-serv15716/test/com/meterware/httpunit/javascript Modified Files: FormScriptingTest.java ScriptingTest.java Log Message: Added support for javascript: URLs Index: FormScriptingTest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/javascript/FormScriptingTest.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- FormScriptingTest.java 21 Aug 2002 03:45:01 -0000 1.5 +++ FormScriptingTest.java 23 Aug 2002 19:33:13 -0000 1.6 @@ -24,6 +24,7 @@ import com.meterware.httpunit.WebForm; import com.meterware.httpunit.HttpUnitTest; import com.meterware.httpunit.WebRequest; +import com.meterware.httpunit.WebClient; import org.xml.sax.SAXException; @@ -79,9 +80,9 @@ assertEquals( "Initial state", null, form.getParameterValue( "ready" ) ); response.getLinks()[ 0 ].click(); - assertEquals( "Message 1", "form has 3 elements", response.popNextAlert() ); - assertEquals( "Message 2", "value is Initial Text", response.popNextAlert() ); - assertEquals( "Message 3", "index is 1", response.popNextAlert() ); + 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" ) ); } @@ -166,14 +167,14 @@ WebForm form = response.getFormWithName( "the_form" ); assertEquals( "Initial state", "blue", form.getParameterValue( "color" ) ); - assertEquals( "Alert message before change", null, response.getNextAlert() ); + assertEquals( "Alert message before change", null, wc.getNextAlert() ); form.setParameter( "color", "red" ); - assertEquals( "Alert after change", "color is now red", response.popNextAlert() ); + assertEquals( "Alert after change", "color is now red", wc.popNextAlert() ); assertEquals( "Changed state", "red", form.getParameterValue( "color" ) ); response.getLinks()[ 0 ].click(); assertEquals( "Final state", "green", form.getParameterValue( "color" ) ); - assertEquals( "Alert message after JavaScript change", null, response.getNextAlert() ); + assertEquals( "Alert message after JavaScript change", null, wc.getNextAlert() ); } @@ -196,7 +197,7 @@ WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "realform" ); response.getLinkWithName( "report" ).mouseOver(); - verifyCheckbox( response, /* default */ false, /* checked */ false, /* value */ "good" ); + verifyCheckbox( /* default */ wc, false, /* checked */ false, /* value */ "good" ); assertEquals( "initial parameter value", null, form.getParameterValue( "ready" ) ); response.getLinkWithName( "set" ).mouseOver(); @@ -206,7 +207,7 @@ response.getLinkWithName( "change" ).mouseOver(); assertEquals( "final parameter value", null, form.getParameterValue( "ready" ) ); response.getLinkWithName( "report" ).mouseOver(); - verifyCheckbox( response, /* default */ false, /* checked */ false, /* value */ "waiting" ); + verifyCheckbox( /* default */ wc, false, /* checked */ false, /* value */ "waiting" ); form.setParameter( "ready", "waiting" ); } @@ -228,15 +229,15 @@ WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "realform" ); - verifyCheckbox( response, /* default */ true, /* checked */ true, /* value */ "good" ); - verifyCheckbox( response, /* default */ false, /* checked */ false, /* value */ "bad" ); + verifyCheckbox( /* default */ wc, true, /* checked */ true, /* value */ "good" ); + verifyCheckbox( /* default */ wc, false, /* checked */ false, /* value */ "bad" ); } - private void verifyCheckbox( WebResponse response, boolean defaultChecked, boolean checked, String value ) throws SAXException { - assertEquals( "Message " + 1 + "-1", "checkbox ready default = " + defaultChecked, response.popNextAlert() ); - assertEquals( "Message " + 1 + "-2", "checkbox ready checked = " + checked, response.popNextAlert() ); - assertEquals( "Message " + 1 + "-3", "checkbox ready value = " + value, response.popNextAlert() ); + private void verifyCheckbox( WebClient wc, boolean defaultChecked, boolean checked, String value ) throws SAXException { + assertEquals( "Message " + 1 + "-1", "checkbox ready default = " + defaultChecked, wc.popNextAlert() ); + assertEquals( "Message " + 1 + "-2", "checkbox ready checked = " + checked, wc.popNextAlert() ); + assertEquals( "Message " + 1 + "-3", "checkbox ready value = " + value, wc.popNextAlert() ); } @@ -254,18 +255,18 @@ WebForm form = response.getFormWithName( "the_form" ); assertEquals( "Initial state", null, form.getParameterValue( "color" ) ); - assertEquals( "Alert message before change", null, response.getNextAlert() ); + assertEquals( "Alert message before change", null, wc.getNextAlert() ); form.removeParameter( "color" ); - assertEquals( "Alert message w/o change", null, response.getNextAlert() ); + assertEquals( "Alert message w/o change", null, wc.getNextAlert() ); form.setParameter( "color", "blue" ); - assertEquals( "Alert after change", "color-blue is now true", response.popNextAlert() ); + assertEquals( "Alert after change", "color-blue is now true", wc.popNextAlert() ); form.removeParameter( "color" ); - assertEquals( "Alert after change", "color-blue is now false", response.popNextAlert() ); + assertEquals( "Alert after change", "color-blue is now false", wc.popNextAlert() ); assertEquals( "Changed state", null, form.getParameterValue( "color" ) ); response.getLinks()[ 0 ].click(); assertEquals( "Final state", "blue", form.getParameterValue( "color" ) ); - assertEquals( "Alert message after JavaScript change", null, response.getNextAlert() ); + assertEquals( "Alert message after JavaScript change", null, wc.getNextAlert() ); } @@ -286,15 +287,15 @@ WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "realform" ); - verifyRadio( response, /* default */ true, /* checked */ true, /* value */ "good" ); - verifyRadio( response, /* default */ false, /* checked */ false, /* value */ "bad" ); + verifyRadio( /* default */ wc, true, /* checked */ true, /* value */ "good" ); + verifyRadio( /* default */ wc, false, /* checked */ false, /* value */ "bad" ); } - private void verifyRadio( WebResponse response, boolean defaultChecked, boolean checked, String value ) throws SAXException { - assertEquals( "Message " + 1 + "-1", "radio ready default = " + defaultChecked, response.popNextAlert() ); - assertEquals( "Message " + 1 + "-2", "radio ready checked = " + checked, response.popNextAlert() ); - assertEquals( "Message " + 1 + "-3", "radio ready value = " + value, response.popNextAlert() ); + private void verifyRadio( WebClient wc, boolean defaultChecked, boolean checked, String value ) throws SAXException { + 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() ); } @@ -314,18 +315,18 @@ WebForm form = response.getFormWithName( "the_form" ); assertEquals( "Initial state", "red", form.getParameterValue( "color" ) ); - assertEquals( "Alert message before change", null, response.getNextAlert() ); + assertEquals( "Alert message before change", null, wc.getNextAlert() ); form.setParameter( "color", "red" ); - assertEquals( "Alert message w/o change", null, response.getNextAlert() ); + assertEquals( "Alert message w/o change", null, wc.getNextAlert() ); form.setParameter( "color", "blue" ); - assertEquals( "Alert after change", "color is now blue", response.popNextAlert() ); + assertEquals( "Alert after change", "color is now blue", wc.popNextAlert() ); form.setParameter( "color", "red" ); - assertEquals( "Alert after change", "color is now red", response.popNextAlert() ); + 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, response.getNextAlert() ); + assertEquals( "Alert message after JavaScript change", null, wc.getNextAlert() ); } @@ -339,7 +340,7 @@ "<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", page.popNextAlert() ); + assertEquals( "Current action", "ask", wc.popNextAlert() ); page.getLinkWithName( "doTell" ).click(); WebRequest request = page.getForms()[0].getRequest(); @@ -369,7 +370,7 @@ WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "realform" ); form.submit(); - assertEquals( "Alert message", "wrong color", response.popNextAlert() ); + assertEquals( "Alert message", "wrong color", wc.popNextAlert() ); assertSame( "Current response", response, wc.getCurrentPage() ); form.setParameter( "color", "pink" ); WebResponse newResponse = form.submit(); @@ -399,17 +400,17 @@ "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); - assertEquals( "1st message", "select has 2 options", response.popNextAlert() ); - assertEquals( "2nd message", "select still has 2 options", response.popNextAlert() ); - assertEquals( "3rd message", "select option 0 is red", response.popNextAlert() ); - assertEquals( "4th message", "select 2nd option value is 3", response.popNextAlert() ); - assertEquals( "5th message", "blue selected", response.popNextAlert() ); + 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() ); response.getLinks()[0].mouseOver(); - assertEquals( "before change message", "selected #1", response.popNextAlert() ); + 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", response.popNextAlert() ); + assertEquals( "after change message", "selected #0", wc.popNextAlert() ); } @@ -435,16 +436,16 @@ final WebForm form = response.getFormWithName( "the_form" ); assertEquals( "Initial state", "blue", form.getParameterValue( "choices" ) ); - assertEquals( "Alert message before change", null, response.getNextAlert() ); + assertEquals( "Alert message before change", null, wc.getNextAlert() ); form.setParameter( "choices", "red" ); - assertEquals( "Alert after change", "Selected index is 0", response.popNextAlert() ); + assertEquals( "Alert after change", "Selected index is 0", wc.popNextAlert() ); form.setParameter( "choices", "blue" ); - assertEquals( "Alert after change", "Selected index is 1", response.popNextAlert() ); + 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, response.getNextAlert() ); + assertEquals( "Alert message after JavaScript change", null, wc.getNextAlert() ); } Index: ScriptingTest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/javascript/ScriptingTest.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- ScriptingTest.java 21 Aug 2002 19:06:27 -0000 1.16 +++ ScriptingTest.java 23 Aug 2002 19:33:13 -0000 1.17 @@ -46,14 +46,61 @@ } + 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" ); + response.getLinks()[0].click(); + assertEquals( "Alert message", "Hi there!", wc.popNextAlert() ); + assertEquals( "Current page URL", getHostPath() + "/OnCommand.html", wc.getCurrentPage().getURL().toExternalForm() ); + } + + + public void testInitialJavaScriptURL() throws Exception { + WebConversation wc = new WebConversation(); + WebResponse response = wc.getResponse( "javascript:alert( 'Hi there!' )" ); + assertEquals( "Alert message", "Hi there!", wc.popNextAlert() ); + } + + + public void testJavaScriptURLWithParameters() 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 testSingleCommandOnLoad() throws Exception { defineResource( "OnCommand.html", "<html><head></head>" + "<body onLoad='alert(\"Ouch!\")'></body>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); - assertNotNull( "No alert detected", response.getNextAlert() ); - assertEquals( "Alert message", "Ouch!", response.popNextAlert() ); - assertNull( "Alert should have been removed", response.getNextAlert() ); + assertNotNull( "No alert detected", wc.getNextAlert() ); + assertEquals( "Alert message", "Ouch!", wc.popNextAlert() ); + assertNull( "Alert should have been removed", wc.getNextAlert() ); } @@ -107,7 +154,7 @@ "<body onLoad='sayCheese()'></body>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); - assertEquals( "Alert message", "Cheese!", response.popNextAlert() ); + assertEquals( "Alert message", "Cheese!", wc.popNextAlert() ); } @@ -116,7 +163,7 @@ "<body onLoad='alert(\"Window title is \" + document.title)'></body>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); - assertEquals( "Alert message", "Window title is Amazing!", response.popNextAlert() ); + assertEquals( "Alert message", "Window title is Amazing!", wc.popNextAlert() ); } @@ -135,10 +182,10 @@ "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); - assertEquals( "Alert message", "found 1 form(s)", response.popNextAlert() ); - assertEquals( "Alert message", "found form 'realform'", response.popNextAlert() ); - assertEquals( "Alert message", "did not find form 'noform'", response.popNextAlert() ); - assertNull( "Alert should have been removed", response.getNextAlert() ); + assertEquals( "Alert message", "found 1 form(s)", wc.popNextAlert() ); + assertEquals( "Alert message", "found form 'realform'", wc.popNextAlert() ); + assertEquals( "Alert message", "did not find form 'noform'", wc.popNextAlert() ); + assertNull( "Alert should have been removed", wc.getNextAlert() ); } @@ -159,10 +206,10 @@ "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); - assertEquals( "Alert message", "found 2 link(s)", response.popNextAlert() ); - assertEquals( "Alert message", "found link 'reallink'", response.popNextAlert() ); - assertEquals( "Alert message", "did not find link 'nolink'", response.popNextAlert() ); - assertNull( "Alert should have been removed", response.getNextAlert() ); + assertEquals( "Alert message", "found 2 link(s)", wc.popNextAlert() ); + assertEquals( "Alert message", "found link 'reallink'", wc.popNextAlert() ); + assertEquals( "Alert message", "did not find link 'nolink'", wc.popNextAlert() ); + assertNull( "Alert should have been removed", wc.getNextAlert() ); } @@ -186,7 +233,7 @@ defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name='realform'><input name='color' value='blue'></form>" + - "<a href='nothing.html' onClick=\"document.realform.color.value='green';return false;\">green</a>" + + "<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" ); @@ -251,11 +298,11 @@ "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); - assertEquals( "Alert message", getHostPath() + "/demo.html", response.popNextAlert() ); - assertEquals( "Alert message", getHostPath() + "/guide.html", response.popNextAlert() ); - assertEquals( "Alert message", getHostPath() + "/search.html", response.popNextAlert() ); - assertEquals( "Alert message", getHostPath() + "/sample.html", response.popNextAlert() ); - assertNull( "Alert should have been removed", response.getNextAlert() ); + assertEquals( "Alert message", getHostPath() + "/demo.html", wc.popNextAlert() ); + assertEquals( "Alert message", getHostPath() + "/guide.html", wc.popNextAlert() ); + assertEquals( "Alert message", getHostPath() + "/search.html", wc.popNextAlert() ); + assertEquals( "Alert message", getHostPath() + "/sample.html", wc.popNextAlert() ); + assertNull( "Alert should have been removed", wc.getNextAlert() ); } @@ -276,11 +323,11 @@ "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); - assertEquals( "Alert message", "found 2 images(s)", response.popNextAlert() ); - assertEquals( "Alert message", "found image 'realimage'", response.popNextAlert() ); - assertEquals( "Alert message", "did not find image 'noimage'", response.popNextAlert() ); - assertEquals( "Alert message", "2nd image is pict2.gif", response.popNextAlert() ); - assertNull( "Alert should have been removed", response.getNextAlert() ); + assertEquals( "Alert message", "found 2 images(s)", wc.popNextAlert() ); + assertEquals( "Alert message", "found image 'realimage'", wc.popNextAlert() ); + assertEquals( "Alert message", "did not find image 'noimage'", wc.popNextAlert() ); + assertEquals( "Alert message", "2nd image is pict2.gif", wc.popNextAlert() ); + assertNull( "Alert should have been removed", wc.getNextAlert() ); } |