[Httpunit-commit] CVS: httpunit/test/com/meterware/httpunit/javascript DocumentScriptingTest.java,1.
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-12-05 04:06:58
|
Update of /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/javascript
In directory sc8-pr-cvs1:/tmp/cvs-serv27254/test/com/meterware/httpunit/javascript
Modified Files:
DocumentScriptingTest.java FormScriptingTest.java
FrameScriptingTest.java
Log Message:
from Andrew Bickerton: Support form.length, control.type, control.defaultValue
Index: DocumentScriptingTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/javascript/DocumentScriptingTest.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- DocumentScriptingTest.java 2 Apr 2003 15:31:12 -0000 1.10
+++ DocumentScriptingTest.java 5 Dec 2003 04:06:53 -0000 1.11
@@ -273,7 +273,7 @@
public void testWriteToNewDocument() throws Exception {
- defineWebPage( "OnCommand", "<a href='#' onclick=\"w = window.open( '', 'sample' );w.document.open( 'text/plain' ); w.document.write( 'You made it!' );w.document.close()\" >" );
+ defineWebPage( "OnCommand", "<a href='#' onclick=\"window.open( '', 'empty' );w = window.open( '', 'sample' );w.document.open( 'text/plain' ); w.document.write( 'You made it!' );w.document.close()\" >" );
WebConversation wc = new WebConversation();
WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" );
WebLink link = response.getLinks()[0];
@@ -283,6 +283,7 @@
assertEquals( "Content Type", "text/plain", ww.getCurrentPage().getContentType() );
link.click();
assertEquals( "Generated page", "You made it!", ww.getCurrentPage().getText() );
+ assertEquals( "Empty page", "", wc.getOpenWindow("empty").getCurrentPage().getText() );
}
Index: FormScriptingTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/javascript/FormScriptingTest.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- FormScriptingTest.java 19 Sep 2003 18:53:43 -0000 1.25
+++ FormScriptingTest.java 5 Dec 2003 04:06:54 -0000 1.26
@@ -25,6 +25,8 @@
import java.io.IOException;
import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
import junit.textui.TestRunner;
import junit.framework.TestSuite;
@@ -839,6 +841,149 @@
WebResponse response = wc.getResponse( getHostPath() + "/index.html" );
WebForm form = response.getFormWithName( "formName" );
assertEquals( "Changed value", "Hello World!", form.getParameterValue( "inputName" ) );
+ }
+
+ /**
+ * Test that JavaScript can correctly access the 'type' property for every kind of form control.
+ * @throws Exception
+ */
+ public void testElementTypeAccess() throws Exception {
+ defineWebPage( "Default", "<script language=JavaScript>\n" +
+ "function CheckForm() {\n" +
+ " var len = document.myForm.elements.length;\n" +
+ " for (var index = 0; index < len; index++) {\n" +
+ " var control = document.myForm.elements[index];\n" +
+ " confirm(control.type);\n" +
+ " }\n" +
+ " return true;\n" +
+ "}\n" +
+ "</script>" +
+ "<form name=myForm method=POST>" +
+ " <input type=\"text\" name=\"textfield\">" +
+ " <textarea name=\"textarea\"></textarea>" +
+ " <input type=\"password\" name=\"password\">" +
+ " <input type=\"submit\" name=\"submit\" value=\"Submit\" onClick=\"return Check()\">" +
+ " <input type=\"reset\" name=\"reset\" value=\"Reset\">" +
+ " <input type=\"button\" name=\"button\" value=\"Button\">" +
+ " <input type=\"checkbox\" name=\"checkbox\" value=\"checkbox\">" +
+ " <input type=\"radio\" name=\"radiobutton\" value=\"radiobutton\">" +
+ " <select name=\"select\">" +
+ " <option value=\"1\">One</option>" +
+ " <option value=\"2\">Two</option>" +
+ " </select>" +
+ " <select name=\"select2\" size=\"2\" multiple>" +
+ " <option value=\"1\">One</option>" +
+ " <option value=\"2\">Two</option>" +
+ " </select>" +
+ " <input type=\"file\" name=\"fileField\">" +
+ " <input type=\"image\" name=\"imageField\" src=\"img.gif\">" +
+ " <input type=\"hidden\" name=\"hiddenField\">" +
+ " <button name=\"html4-button\" type=\"button\">html4-button</button>" +
+ " <button name=\"html4-submit\" type=\"submit\">html4-submit</button>" +
+ " <button name=\"html4-reset\" type=\"reset\">html4-reset</button>" +
+ " <button name=\"html4-default\">html4-default</button>" +
+ "</form>" +
+ "<script language=JavaScript>\n" +
+ " CheckForm();\n" +
+ "</script>\n");
+
+ String[] expectedTypes = new String[]{
+ "text", "textarea", "password", "submit", "reset", "button",
+ "checkbox", "radio", "select-one", "select-multiple", "file",
+ "image", "hidden", "button", "submit", "reset", "submit"
+ };
+
+ final PromptCollector collector = new PromptCollector();
+ WebConversation wc = new WebConversation();
+ wc.setDialogResponder(collector);
+ wc.getResponse( getHostPath() + "/Default.html" );
+ assertMatchingSet("Set of types on form", expectedTypes, collector.confirmPromptsSeen.toArray());
+ }
+
+ static class PromptCollector implements DialogResponder {
+ public List confirmPromptsSeen = new ArrayList();
+ public List responsePromptSeen = new ArrayList();
+ public boolean getConfirmation(String confirmationPrompt) {
+ confirmPromptsSeen.add(confirmationPrompt);
+ return true;
+ }
+
+ public String getUserResponse(String prompt, String defaultResponse) {
+ responsePromptSeen.add(prompt);
+ return null;
+ }
+ }
+
+ /**
+ * Test that the length (number of controls) of a form can be accessed from JavaScript.
+ * @throws Exception
+ */
+ public void testFormLength () throws Exception {
+ defineWebPage("Default", "<script language=JavaScript>\n" +
+ "function CheckForm()\n"+
+ "{\n"+
+ "confirm (document.myForm.length);\n" +
+ "return true;\n" +
+ "}\n" +
+ "</script>" +
+ "<form name=myForm method=POST>" +
+ " <input type=\"text\" name=\"first_name\" value=\"Fred\">" +
+ " <input type=\"text\" name=\"last_name\" value=\"Bloggs\">" +
+ "</form>" +
+ "<script language=JavaScript>\n" +
+ " CheckForm();\n" +
+ "</script>\n");
+
+ String[] expectedPrompts = new String[]{"2"};
+
+ final PromptCollector collector = new PromptCollector();
+ WebConversation wc = new WebConversation();
+ wc.setDialogResponder(collector);
+ wc.getResponse( getHostPath() + "/Default.html" );
+ assertMatchingSet("Length of form", expectedPrompts, collector.confirmPromptsSeen.toArray());
+ }
+
+ /**
+ * Test that the JavaScript 'value' and 'defaultValue' properties of a text input are distinct.
+ * 'defaultValue' should represent the 'value' attribute of the input element.
+ * 'value' should initially match 'defaultValue', but setting it should not affect the 'defaultValue'.
+ * @throws Exception
+ */
+ public void testElementDefaultValue () throws Exception {
+ defineWebPage("Default", "<script language=JavaScript>\n" +
+ "function CheckForm()\n"+
+ "{\n"+
+ "var i;\n" +
+ "var form_length=document.myForm.elements.length;\n" +
+ "for ( i=0 ; i< form_length; i++ )\n" +
+ "{\n"+
+ " confirm (document.myForm.elements[i].value);\n " +
+ "}\n"+
+ "document.myForm.elements[2].value = \"Charles\"\n" +
+ "for ( i=0 ; i< form_length; i++ )\n" +
+ "{\n"+
+ " confirm (document.myForm.elements[i].defaultValue);\n " +
+ "}\n"+
+ "confirm(document.myForm.elements[2].value);\n" +
+ "return true;\n" +
+ "}\n" +
+ "</script>" +
+ "<form name=myForm method=POST>" +
+ " <input type=\"text\" name=\"first_name\" value=\"Alpha\">" +
+ " <input type=\"text\" name=\"last_name\" value=\"Bravo\">" +
+ " <input type=\"text\" name=\"last_name\" value=\"Charlie\">" +
+ "</form>" +
+ "<script language=JavaScript>\n" +
+ " CheckForm();\n" +
+ "</script>\n");
+
+ String[] expectedValues = new String[]{"Alpha", "Bravo", "Charlie", "Alpha", "Bravo", "Charlie", "Charles"};
+
+ final PromptCollector collector = new PromptCollector();
+ WebConversation wc = new WebConversation();
+ wc.setDialogResponder(collector);
+ wc.getResponse( getHostPath() + "/Default.html" );
+ assertMatchingSet("Values seen by JavaScript", expectedValues, collector.confirmPromptsSeen.toArray());
}
}
Index: FrameScriptingTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/javascript/FrameScriptingTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- FrameScriptingTest.java 24 Mar 2003 15:04:33 -0000 1.2
+++ FrameScriptingTest.java 5 Dec 2003 04:06:54 -0000 1.3
@@ -196,5 +196,27 @@
assertEquals( "Result of click", "You made it!", result.getText() );
}
+ /**
+ * Verifies that when JavaScript overwrites an empty frame, other empty frames stay empty.
+ */
+ public void testJavaScriptOverwritingBlankFrame() throws Exception {
+ defineResource("Frames.html",
+ "<HTML><HEAD><TITLE>Initial</TITLE></HEAD>" +
+ "<FRAMESET>" +
+ " <FRAME src='Writer.html' name='red'>" +
+ " <FRAME name=green>" +
+ " <FRAME name=blue>" +
+ "</FRAMESET></HTML>");
+ defineWebPage("Writer", "This page overwrites an empty frame. <script language=\"JavaScript\">"
+ + " window.parent.green.document.open(\"text/html\")\n"
+ + " window.parent.green.document.write(\"Generated <a href=\\\"#\\\">link</a>\")\n"
+ + " window.parent.green.document.close()\n"
+ + "</script>");
+
+ _wc.getResponse(getHostPath() + "/Frames.html");
+ assertEquals("Links in green frame", 1, _wc.getFrameContents("green").getLinks().length);
+ assertEquals("Links in blue frame", 0, _wc.getFrameContents("blue").getLinks().length);
+ }
+
private WebConversation _wc;
}
|