KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > HtmlSubmitInputTest


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.html;
39
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.List;
44
45 import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
46 import com.gargoylesoftware.htmlunit.KeyValuePair;
47 import com.gargoylesoftware.htmlunit.MockWebConnection;
48 import com.gargoylesoftware.htmlunit.WebClient;
49 import com.gargoylesoftware.htmlunit.WebTestCase;
50
51 /**
52  * Tests for HtmlSubmitInput
53  *
54  * @version $Revision: 1.13 $
55  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
56  */

57 public class HtmlSubmitInputTest extends WebTestCase {
58     /**
59      * Create an instance
60      *
61      * @param name The name of the test
62      */

63     public HtmlSubmitInputTest( final String name ) {
64         super( name );
65     }
66
67
68     /**
69      * @throws Exception if the test fails
70      */

71     public void testSubmit() throws Exception {
72         final String htmlContent
73             = "<html><head><title>foo</title></head><body>"
74             + "<form id='form1'>"
75             + "<input type='submit' name='aButton' value='foo'/>"
76             + "<input type='suBMit' name='button' value='foo'/>"
77             + "<input type='submit' name='anotherButton' value='foo'/>"
78             + "</form></body></html>";
79         final HtmlPage page = loadPage(htmlContent);
80         final MockWebConnection webConnection = getMockConnection(page);
81         
82         final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" );
83
84         final HtmlSubmitInput submitInput = (HtmlSubmitInput)form.getInputByName("button");
85         final HtmlPage secondPage = (HtmlPage)submitInput.click();
86         assertEquals("foo", secondPage.getTitleText());
87
88         assertEquals(
89             Collections.singletonList(new KeyValuePair("button", "foo")),
90             webConnection.getLastParameters() );
91     }
92
93
94     /**
95      * @throws Exception if the test fails
96      */

97     public void testClick_onClick() throws Exception {
98         final String htmlContent
99             = "<html><head><title>foo</title></head><body>"
100             + "<form id='form1' onSubmit='alert(\"bar\")'>"
101             + " <input type='submit' name='button' value='foo' onClick='alert(\"foo\")'/>"
102             + "</form></body></html>";
103         final List collectedAlerts = new ArrayList();
104         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
105         
106         final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" );
107         final HtmlSubmitInput submitInput = (HtmlSubmitInput)form.getInputByName("button");
108
109         submitInput.click();
110
111         final List expectedAlerts = Arrays.asList( new String[]{"foo","bar"} );
112         assertEquals( expectedAlerts, collectedAlerts );
113     }
114
115
116     /**
117      * @throws Exception if the test fails
118      */

119     public void testClick_onClick_JavascriptReturnsTrue() throws Exception {
120
121         final String firstContent
122             = "<html><head><title>First</title></head><body>"
123             + "<form name='form1' method='get' action='http://second'>"
124             + "<input name='button' type='submit' value='PushMe' id='button1'"
125             + "onclick='return true'/></form>"
126             + "</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(collectedAlerts) );
132
133         final MockWebConnection webConnection = new MockWebConnection( client );
134         webConnection.setResponse(URL_FIRST, firstContent);
135         webConnection.setResponse(URL_SECOND, secondContent);
136
137         client.setWebConnection( webConnection );
138
139         final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
140         final HtmlSubmitInput input = (HtmlSubmitInput)firstPage.getHtmlElementById("button1");
141         final HtmlPage secondPage = (HtmlPage)input.click();
142         assertEquals("Second", secondPage.getTitleText());
143     }
144 }
145
Popular Tags