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.Arrays;
042: import java.util.Collections;
043: import java.util.List;
044:
045: import com.gargoylesoftware.htmlunit.BrowserVersion;
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 HtmlButton}.
053: *
054: * @version $Revision: 2132 $
055: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
056: * @author David K. Taylor
057: * @author Brad Clarke
058: * @author Marc Guillemot
059: * @author Ahmed Ashour
060: */
061: public class HtmlButtonTest extends WebTestCase {
062: /**
063: * Create an instance
064: *
065: * @param name The name of the test
066: */
067: public HtmlButtonTest(final String name) {
068: super (name);
069: }
070:
071: /**
072: * @throws Exception if the test fails
073: */
074: public void testButtonClick_onClick() throws Exception {
075: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
076: + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>\n"
077: + " <button type='button' name='button' id='button' "
078: + "onClick='alert(\"foo\")'>Push me</button>\n"
079: + "</form></body></html>";
080: final List collectedAlerts = new ArrayList();
081: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
082: final HtmlButton button = (HtmlButton) page
083: .getHtmlElementById("button");
084:
085: final HtmlPage secondPage = (HtmlPage) button.click();
086:
087: final String[] expectedAlerts = { "foo" };
088: assertEquals(expectedAlerts, collectedAlerts);
089:
090: assertSame(page, secondPage);
091: }
092:
093: /**
094: * @throws Exception if the test fails
095: */
096: public void testSubmitClick_onClick() throws Exception {
097: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
098: + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>\n"
099: + " <button type='submit' name='button' id='button' "
100: + "onClick='alert(\"foo\")'>Push me</button>\n"
101: + "</form></body></html>";
102: final List collectedAlerts = new ArrayList();
103: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
104: final HtmlButton button = (HtmlButton) page
105: .getHtmlElementById("button");
106:
107: final HtmlPage secondPage = (HtmlPage) button.click();
108:
109: final String[] expectedAlerts = { "foo", "bar" };
110: assertEquals(expectedAlerts, collectedAlerts);
111:
112: assertNotSame(page, secondPage);
113: }
114:
115: /**
116: * @throws Exception if the test fails
117: */
118: public void testResetClick_onClick() throws Exception {
119: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
120: + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>\n"
121: + " <button type='reset' name='button' id='button' "
122: + "onClick='alert(\"foo\")'>Push me</button>\n"
123: + "</form></body></html>";
124: final List collectedAlerts = new ArrayList();
125: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
126: final HtmlButton button = (HtmlButton) page
127: .getHtmlElementById("button");
128:
129: final HtmlPage secondPage = (HtmlPage) button.click();
130:
131: final String[] expectedAlerts = { "foo", "reset" };
132: assertEquals(expectedAlerts, collectedAlerts);
133:
134: assertSame(page, secondPage);
135: }
136:
137: /**
138: * @throws Exception if the test fails
139: */
140: public void testReset() throws Exception {
141: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
142: + "<form id='form1'>\n"
143: + "<input type='text' name='textfield1' id='textfield1' value='foo'/>\n"
144: + "<input type='password' name='password1' id='password1' value='foo'/>\n"
145: + "<input type='hidden' name='hidden1' id='hidden1' value='foo'/>\n"
146: + "<input type='radio' name='radioButton' value='foo' checked/>\n"
147: + "<input type='radio' name='radioButton' value='bar'/>\n"
148: + "<input type='checkbox' name='checkBox' value='check'/>\n"
149: + "<select id='select1'>\n"
150: + " <option id='option1' selected value='1'>Option1</option>\n"
151: + " <option id='option2' value='2'>Option2</option>\n"
152: + "</select>\n"
153: + "<textarea id='textarea1'>Foobar</textarea>\n"
154: + "<isindex prompt='Enter some text' id='isindex1'>\n"
155: + "<button type='reset' id='resetButton' value='pushme'/>\n"
156: + "</form></body></html>";
157: final HtmlPage page = loadPage(htmlContent);
158: final HtmlForm form = (HtmlForm) page
159: .getHtmlElementById("form1");
160: final HtmlButton resetInput = (HtmlButton) page
161: .getHtmlElementById("resetButton");
162:
163: // change all the values to something else
164: ((HtmlRadioButtonInput) form
165: .getFirstByXPath("//input[@type='radio' and @name='radioButton' and @value='bar']"))
166: .setChecked(true);
167: ((HtmlCheckBoxInput) form.getInputByName("checkBox"))
168: .setChecked(true);
169: ((HtmlOption) page.getHtmlElementById("option1"))
170: .setSelected(false);
171: ((HtmlOption) page.getHtmlElementById("option2"))
172: .setSelected(true);
173: ((HtmlTextArea) page.getHtmlElementById("textarea1"))
174: .setText("Flintstone");
175: ((HtmlTextInput) page.getHtmlElementById("textfield1"))
176: .setValueAttribute("Flintstone");
177: ((HtmlHiddenInput) page.getHtmlElementById("hidden1"))
178: .setValueAttribute("Flintstone");
179: ((HtmlPasswordInput) page.getHtmlElementById("password1"))
180: .setValueAttribute("Flintstone");
181: ((HtmlIsIndex) page.getHtmlElementById("isindex1"))
182: .setValue("Flintstone");
183:
184: // Check to make sure they did get changed
185: assertEquals("bar", form.getCheckedRadioButton("radioButton")
186: .getValueAttribute());
187: assertTrue(((HtmlCheckBoxInput) form.getInputByName("checkBox"))
188: .isChecked());
189: assertFalse(((HtmlOption) page.getHtmlElementById("option1"))
190: .isSelected());
191: assertTrue(((HtmlOption) page.getHtmlElementById("option2"))
192: .isSelected());
193: assertEquals("Flintstone", ((HtmlTextArea) page
194: .getHtmlElementById("textarea1")).getText());
195: assertEquals("Flintstone", ((HtmlTextInput) page
196: .getHtmlElementById("textfield1")).getValueAttribute());
197: assertEquals("Flintstone", ((HtmlHiddenInput) page
198: .getHtmlElementById("hidden1")).getValueAttribute());
199: assertEquals("Flintstone", ((HtmlIsIndex) page
200: .getHtmlElementById("isindex1")).getValue());
201:
202: final HtmlPage secondPage = (HtmlPage) resetInput.click();
203: assertSame(page, secondPage);
204:
205: // Check to make sure all the values have been set back to their original values.
206: assertEquals("foo", form.getCheckedRadioButton("radioButton")
207: .getValueAttribute());
208: assertFalse(((HtmlCheckBoxInput) form
209: .getInputByName("checkBox")).isChecked());
210: assertTrue(((HtmlOption) page.getHtmlElementById("option1"))
211: .isSelected());
212: assertFalse(((HtmlOption) page.getHtmlElementById("option2"))
213: .isSelected());
214: assertEquals("Foobar", ((HtmlTextArea) page
215: .getHtmlElementById("textarea1")).getText());
216: assertEquals("foo", ((HtmlTextInput) page
217: .getHtmlElementById("textfield1")).getValueAttribute());
218: assertEquals("foo", ((HtmlHiddenInput) page
219: .getHtmlElementById("hidden1")).getValueAttribute());
220: assertEquals("foo", ((HtmlPasswordInput) page
221: .getHtmlElementById("password1")).getValueAttribute());
222: assertEquals("", ((HtmlIsIndex) page
223: .getHtmlElementById("isindex1")).getValue());
224: }
225:
226: /**
227: * @throws Exception if the test fails
228: */
229: public void testButtonTypeSubmit() throws Exception {
230: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
231: + "<form id='form1' method='post' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>\n"
232: + " <button type='submit' name='button' id='button' value='foo'"
233: + " >Push me</button>\n" + "</form></body></html>";
234: final List collectedAlerts = new ArrayList();
235: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
236: final HtmlButton button = (HtmlButton) page
237: .getHtmlElementById("button");
238:
239: final HtmlPage secondPage = (HtmlPage) button.click();
240:
241: final String[] expectedAlerts = { "bar" };
242: assertEquals(expectedAlerts, collectedAlerts);
243:
244: assertNotSame(page, secondPage);
245: final List expectedParameters = Arrays
246: .asList(new Object[] { new KeyValuePair("button", "foo") });
247: final List collectedParameters = getMockConnection(secondPage)
248: .getLastParameters();
249:
250: assertEquals(expectedParameters, collectedParameters);
251: }
252:
253: /**
254: * According to the html spec, the default type for a button is "submit"
255: * @throws Exception if the test fails
256: */
257: public void testDefaultButtonType_StandardsCompliantBrowser()
258: throws Exception {
259: doTestDefaultButtonType(BrowserVersion.FIREFOX_2, "submit");
260: }
261:
262: /**
263: * IE is different than the html spec and has a default type of "button"
264: * @throws Exception if the test fails
265: */
266: public void testDefaultButtonType_InternetExplorer()
267: throws Exception {
268: doTestDefaultButtonType(BrowserVersion.INTERNET_EXPLORER_6_0,
269: "button");
270: }
271:
272: private void doTestDefaultButtonType(
273: final BrowserVersion browserVersion,
274: final String expectedType) throws Exception {
275: final String firstContent = "<html><head><title>First</title></head><body>\n"
276: + "<form id='form1' action='"
277: + URL_SECOND
278: + "' method='post'>\n"
279: + " <button name='button' id='button' value='pushme'>PushMe</button>\n"
280: + "</form></body></html>";
281: final String secondContent = "<html><head><title>Second</title></head><body'></body></html>";
282: final WebClient client = new WebClient(browserVersion);
283:
284: final MockWebConnection webConnection = new MockWebConnection(
285: client);
286: webConnection.setResponse(URL_FIRST, firstContent);
287: webConnection.setResponse(URL_SECOND, secondContent);
288: client.setWebConnection(webConnection);
289:
290: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
291: final HtmlButton button = (HtmlButton) page
292: .getHtmlElementById("button");
293: assertEquals(expectedType, button.getTypeAttribute());
294:
295: final HtmlPage page2 = (HtmlPage) button.click();
296: final List expectedParameters;
297: final String expectedSecondPageTitle;
298: if (expectedType.equals("submit")) {
299: expectedParameters = Collections
300: .singletonList(new KeyValuePair("button", "pushme"));
301: expectedSecondPageTitle = "Second";
302: } else {
303: expectedParameters = Collections.EMPTY_LIST;
304: expectedSecondPageTitle = "First";
305: }
306: assertEquals(expectedParameters, webConnection
307: .getLastParameters());
308: assertEquals(expectedSecondPageTitle, page2.getTitleText());
309: }
310: }
|