KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections;
42 import java.util.List;
43
44 import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
45 import com.gargoylesoftware.htmlunit.MockWebConnection;
46 import com.gargoylesoftware.htmlunit.SubmitMethod;
47 import com.gargoylesoftware.htmlunit.WebClient;
48 import com.gargoylesoftware.htmlunit.WebTestCase;
49
50 /**
51  * Tests for HtmlAnchor
52  *
53  * @version $Revision: 1.13 $
54  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
55  */

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

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

71     public void testClick() throws Exception {
72
73         final String htmlContent
74             = "<html><head><title>foo</title></head><body>"
75             + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>"
76             + "<a HREF='http://www.foo2.com' id='a2'>link to foo2</a>"
77             + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>"
78             + "</body></html>";
79
80         final HtmlPage page = loadPage(htmlContent);
81         final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" );
82
83         // Test that the correct value is being passed back up to the server
84
final HtmlPage secondPage = ( HtmlPage )anchor.click();
85
86         final List expectedParameters = Collections.EMPTY_LIST;
87         final MockWebConnection webConnection = getMockConnection(secondPage);
88
89         assertEquals( "url", "http://www.foo2.com",
90             secondPage.getWebResponse().getUrl().toExternalForm() );
91         assertEquals( "method", SubmitMethod.GET, webConnection.getLastMethod() );
92         assertEquals( "parameters", expectedParameters, webConnection.getLastParameters() );
93         assertNotNull( secondPage );
94     }
95     /**
96      * @throws Exception if the test fails
97      */

98     public void testClickAnchorName() throws Exception {
99         final String htmlContent
100             = "<html><head><title>foo</title></head><body>"
101             + "<a HREF='#clickedAnchor' id='a1'>link to foo1</a>"
102             + "</body></html>";
103         final WebClient client = new WebClient();
104
105         final MockWebConnection webConnection = new MockWebConnection( client );
106         webConnection.setDefaultResponse( htmlContent );
107         client.setWebConnection( webConnection );
108
109         final HtmlPage page = ( HtmlPage )client.getPage(
110                 URL_GARGOYLE );
111
112         final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a1" );
113
114         // Test that the correct value is being passed back up to the server
115
final HtmlPage secondPage = ( HtmlPage )anchor.click();
116
117         // The url shouldn't contain the anchor since that isn't sent to the server
118
assertEquals("url", URL_GARGOYLE.toExternalForm(),
119             secondPage.getWebResponse().getUrl().toExternalForm());
120     }
121
122
123     /**
124      * @throws Exception if the test fails
125      */

126     public void testClick_onClickHandler()
127         throws Exception {
128
129         final String firstContent
130             = "<html><head><title>First</title></head><body>"
131             + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>"
132             + "<a HREF='http://second' id='a2' "
133             + "onClick='alert(\"clicked\")'>link to foo2</a>"
134             + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>"
135             + "</body></html>";
136         final String secondContent
137             = "<html><head><title>Second</title></head><body></body></html>";
138
139         final WebClient client = new WebClient();
140         final List collectedAlerts = new ArrayList();
141         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
142
143         final MockWebConnection webConnection = new MockWebConnection( client );
144         webConnection.setResponse(
145             URL_FIRST, firstContent, 200, "OK", "text/html",
146             Collections.EMPTY_LIST );
147         webConnection.setResponse(
148             URL_SECOND, secondContent, 200, "OK", "text/html",
149             Collections.EMPTY_LIST );
150         client.setWebConnection( webConnection );
151
152         final HtmlPage page = ( HtmlPage )client.getPage(URL_FIRST);
153         final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" );
154
155         assertEquals( Collections.EMPTY_LIST, collectedAlerts );
156
157         final HtmlPage secondPage = ( HtmlPage )anchor.click();
158
159         assertEquals( Collections.singletonList("clicked"), collectedAlerts );
160         assertEquals( "Second", secondPage.getTitleText() );
161     }
162
163
164     /**
165      * @throws Exception if the test fails
166      */

167     public void testClick_onClickHandler_returnFalse()
168         throws Exception {
169
170         final String firstContent
171             = "<html><head><title>First</title></head><body>"
172             + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>"
173             + "<a HREF='http://second' id='a2' "
174             + "onClick='alert(\"clicked\");return false;'>link to foo2</a>"
175             + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>"
176             + "</body></html>";
177         final String secondContent
178             = "<html><head><title>Second</title></head><body></body></html>";
179
180         final WebClient client = new WebClient();
181         final List collectedAlerts = new ArrayList();
182         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
183
184         final MockWebConnection webConnection = new MockWebConnection( client );
185         webConnection.setResponse(
186             URL_FIRST, firstContent, 200, "OK", "text/html",
187             Collections.EMPTY_LIST );
188         webConnection.setResponse(
189             URL_SECOND, secondContent, 200, "OK", "text/html",
190             Collections.EMPTY_LIST );
191         client.setWebConnection( webConnection );
192
193         final HtmlPage page = ( HtmlPage )client.getPage(URL_FIRST);
194         final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" );
195
196         assertEquals( Collections.EMPTY_LIST, collectedAlerts );
197
198         final HtmlPage secondPage = ( HtmlPage )anchor.click();
199
200         assertEquals( Collections.singletonList("clicked"), collectedAlerts );
201         assertSame( page, secondPage );
202     }
203
204
205     /**
206      * @throws Exception if the test fails
207      */

208     public void testClick_onClickHandler_javascriptDisabled() throws Exception {
209
210         final String htmlContent
211             = "<html><head><title>foo</title></head><body>"
212             + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>"
213             + "<a HREF='http://www.foo2.com' id='a2' "
214             + "onClick='alert(\"clicked\")'>link to foo2</a>"
215             + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>"
216             + "</body></html>";
217         final WebClient client = new WebClient();
218         client.setJavaScriptEnabled(false);
219
220         final List collectedAlerts = new ArrayList();
221         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
222
223         final MockWebConnection webConnection = new MockWebConnection( client );
224         webConnection.setDefaultResponse( htmlContent );
225         client.setWebConnection( webConnection );
226
227         final HtmlPage page = ( HtmlPage )client.getPage(URL_GARGOYLE);
228         final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" );
229
230         assertEquals( Collections.EMPTY_LIST, collectedAlerts );
231
232         final HtmlPage secondPage = ( HtmlPage )anchor.click();
233
234         assertEquals( Collections.EMPTY_LIST, collectedAlerts );
235         final List expectedParameters = Collections.EMPTY_LIST;
236
237         assertEquals( "url", "http://www.foo2.com",
238             secondPage.getWebResponse().getUrl().toExternalForm() );
239         assertEquals( "method", SubmitMethod.GET, webConnection.getLastMethod() );
240         assertEquals( "parameters", expectedParameters, webConnection.getLastParameters() );
241         assertNotNull( secondPage );
242     }
243
244
245     /**
246      * @throws Exception if the test fails
247      */

248     public void testClick_javascriptUrl() throws Exception {
249
250         final String htmlContent
251             = "<html><head><title>foo</title></head><body>"
252             + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>"
253             + "<a HREF='javascript:alert(\"clicked\")' id='a2'>link to foo2</a>"
254             + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>"
255             + "</body></html>";
256         final List collectedAlerts = new ArrayList();
257         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
258         
259         final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" );
260
261         assertEquals( Collections.EMPTY_LIST, collectedAlerts );
262
263         final HtmlPage secondPage = ( HtmlPage )anchor.click();
264
265         assertEquals( Collections.singletonList("clicked"), collectedAlerts );
266         assertSame( page, secondPage );
267     }
268
269
270     /**
271      * @throws Exception if the test fails
272      */

273     public void testClick_javascriptUrl_javascriptDisabled() throws Exception {
274
275         final String htmlContent
276             = "<html><head><title>foo</title></head><body>"
277             + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>"
278             + "<a HREF='javascript:alert(\"clicked\")' id='a2'>link to foo2</a>"
279             + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>"
280             + "</body></html>";
281         final WebClient client = new WebClient();
282         client.setJavaScriptEnabled(false);
283
284         final List collectedAlerts = new ArrayList();
285         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
286
287         final MockWebConnection webConnection = new MockWebConnection( client );
288         webConnection.setDefaultResponse( htmlContent );
289         client.setWebConnection( webConnection );
290
291         final HtmlPage page = ( HtmlPage )client.getPage(URL_GARGOYLE);
292         final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" );
293
294         assertEquals( Collections.EMPTY_LIST, collectedAlerts );
295
296         final HtmlPage secondPage = ( HtmlPage )anchor.click();
297
298         assertEquals( Collections.EMPTY_LIST, collectedAlerts );
299         assertSame( page, secondPage );
300     }
301
302
303     /**
304      * @throws Exception if the test fails
305      */

306     public void testClick_javascriptUrl_InvalidReturn_RegressionTest() throws Exception {
307
308         final String htmlContent
309             = "<html><head><SCRIPT lang=\"JavaScript\">"
310             + "function doSubmit(formName){"
311             + " return false;"
312             + "}"
313             + "</SCRIPT></head><body><form name=\"formName\" method=\"POST\"action=\"../foo\">"
314             + "<a HREF=\".\" id=\"testJavascript\" name=\"testJavascript\""
315             + "onclick=\"return false;\">Test Link </a>"
316             + "<input type=\"submit\" value=\"Login\" "
317             + "name=\"loginButton\"></form></body></html>";
318
319         final HtmlPage page = loadPage(htmlContent);
320         final HtmlAnchor testAnchor = page.getAnchorByName("testJavascript");
321         testAnchor.click(); // blows up here
322
}
323 }
324
325
Popular Tags