001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import java.util.ArrayList;
041: import java.util.Collections;
042: import java.util.List;
043:
044: import com.gargoylesoftware.htmlunit.BrowserVersion;
045: import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
046: import com.gargoylesoftware.htmlunit.KeyValuePair;
047: import com.gargoylesoftware.htmlunit.MockWebConnection;
048: import com.gargoylesoftware.htmlunit.WebClient;
049: import com.gargoylesoftware.htmlunit.WebTestCase;
050:
051: /**
052: * Tests for {@link HtmlSubmitInput}.
053: *
054: * @version $Revision: 2132 $
055: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
056: * @author Marc Guillemot
057: * @author Ahmed Ashour
058: */
059: public class HtmlSubmitInputTest extends WebTestCase {
060: /**
061: * Create an instance
062: *
063: * @param name The name of the test
064: */
065: public HtmlSubmitInputTest(final String name) {
066: super (name);
067: }
068:
069: /**
070: * @throws Exception if the test fails
071: */
072: public void testSubmit() throws Exception {
073: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
074: + "<form id='form1' method='post'>\n"
075: + "<input type='submit' name='aButton' value='foo'/>\n"
076: + "<input type='suBMit' name='button' value='foo'/>\n"
077: + "<input type='submit' name='anotherButton' value='foo'/>\n"
078: + "</form></body></html>";
079: final HtmlPage page = loadPage(htmlContent);
080: final MockWebConnection webConnection = getMockConnection(page);
081:
082: final HtmlForm form = (HtmlForm) page
083: .getHtmlElementById("form1");
084:
085: final HtmlSubmitInput submitInput = (HtmlSubmitInput) form
086: .getInputByName("button");
087: final HtmlPage secondPage = (HtmlPage) submitInput.click();
088: assertEquals("foo", secondPage.getTitleText());
089:
090: assertEquals(Collections.singletonList(new KeyValuePair(
091: "button", "foo")), webConnection.getLastParameters());
092: }
093:
094: /**
095: * @throws Exception if the test fails
096: */
097: public void testClick_onClick() throws Exception {
098: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
099: + "<form id='form1' onSubmit='alert(\"bar\")'>\n"
100: + " <input type='submit' name='button' value='foo' onClick='alert(\"foo\")'/>\n"
101: + "</form></body></html>";
102: final List collectedAlerts = new ArrayList();
103: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
104:
105: final HtmlForm form = (HtmlForm) page
106: .getHtmlElementById("form1");
107: final HtmlSubmitInput submitInput = (HtmlSubmitInput) form
108: .getInputByName("button");
109:
110: submitInput.click();
111:
112: final String[] expectedAlerts = { "foo", "bar" };
113: assertEquals(expectedAlerts, collectedAlerts);
114: }
115:
116: /**
117: * @throws Exception if the test fails
118: */
119: public void testClick_onClick_JavascriptReturnsTrue()
120: throws Exception {
121: final String firstContent = "<html><head><title>First</title></head><body>\n"
122: + "<form name='form1' method='get' action='"
123: + URL_SECOND
124: + "'>\n"
125: + "<input name='button' type='submit' value='PushMe' id='button1'"
126: + "onclick='return true'/></form>\n" + "</body></html>";
127: final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
128:
129: final WebClient client = new WebClient();
130: final List collectedAlerts = new ArrayList();
131: client.setAlertHandler(new CollectingAlertHandler(
132: collectedAlerts));
133:
134: final MockWebConnection webConnection = new MockWebConnection(
135: client);
136: webConnection.setResponse(URL_FIRST, firstContent);
137: webConnection.setDefaultResponse(secondContent);
138:
139: client.setWebConnection(webConnection);
140:
141: final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
142: final HtmlSubmitInput input = (HtmlSubmitInput) firstPage
143: .getHtmlElementById("button1");
144: final HtmlPage secondPage = (HtmlPage) input.click();
145: assertEquals("Second", secondPage.getTitleText());
146: }
147:
148: /**
149: * @throws Exception If the test fails
150: */
151: public void testDefaultValue() throws Exception {
152: final String[] expectedAlertsIE = { "Submit Query" };
153: testDefaultValue(BrowserVersion.INTERNET_EXPLORER_6_0,
154: expectedAlertsIE);
155: final String[] expectedAlertsFF = { "" };
156: testDefaultValue(BrowserVersion.FIREFOX_2, expectedAlertsFF);
157: }
158:
159: private void testDefaultValue(final BrowserVersion browserVersion,
160: final String[] expectedAlerts) throws Exception {
161: final String html = "<html><head>\n" + "<script>\n"
162: + " function test() {\n"
163: + " alert(document.getElementById('myId').value);\n"
164: + " }\n" + "</script>\n" + "</head>\n"
165: + "<body onload='test()'>\n" + "<form action='"
166: + URL_SECOND + "'>\n"
167: + " <input type='submit' id='myId'>\n" + "</form>\n"
168: + "</body></html>";
169:
170: final List collectedAlerts = new ArrayList();
171:
172: final HtmlPage page = loadPage(browserVersion, html,
173: collectedAlerts);
174: assertEquals(expectedAlerts, collectedAlerts);
175: assertTrue(page.asText().indexOf("Submit Query") > -1);
176: assertFalse(page.asXml().indexOf("Submit Query") > -1);
177: }
178:
179: /**
180: * @throws Exception If the test fails
181: */
182: public void testEmptyValue() throws Exception {
183: final String html = "<html><head>\n" + "<script>\n"
184: + " function test() {\n"
185: + " alert(document.getElementById('myId').value);\n"
186: + " }\n" + "</script>\n" + "</head>\n"
187: + "<body onload='test()'>\n" + "<form action='"
188: + URL_SECOND + "'>\n"
189: + " <input type='submit' id='myId' value=''>\n"
190: + "</form>\n" + "</body></html>";
191:
192: final String[] expectedAlerts = { "" };
193: final List collectedAlerts = new ArrayList();
194:
195: final HtmlPage page = loadPage(html, collectedAlerts);
196: assertEquals(expectedAlerts, collectedAlerts);
197: assertFalse(page.asText().indexOf("Submit Query") > -1);
198: assertTrue(page.asXml().indexOf("value=\"\"") > -1);
199: }
200:
201: /**
202: * @throws Exception If the test fails
203: */
204: public void testOutsideForm() throws Exception {
205: final String html = "<html><head></head>\n"
206: + "<body>\n"
207: + "<input id='myInput' type='submit' onclick='alert(1)'>\n"
208: + "</body></html>";
209:
210: final String[] expectedAlerts = { "1" };
211: final List collectedAlerts = new ArrayList();
212: final HtmlPage page = loadPage(html, collectedAlerts);
213: final HtmlSubmitInput input = (HtmlSubmitInput) page
214: .getHtmlElementById("myInput");
215: input.click();
216:
217: assertEquals(expectedAlerts, collectedAlerts);
218: }
219: }
|