KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.List;
45
46 import junit.framework.AssertionFailedError;
47
48 import com.gargoylesoftware.htmlunit.ImmediateRefreshHandler;
49 import com.gargoylesoftware.htmlunit.KeyValuePair;
50 import com.gargoylesoftware.htmlunit.MockWebConnection;
51 import com.gargoylesoftware.htmlunit.SubmitMethod;
52 import com.gargoylesoftware.htmlunit.WebClient;
53 import com.gargoylesoftware.htmlunit.WebTestCase;
54
55 /**
56  * Tests for HtmlPage.
57  *
58  * @version $Revision: 1.32 $
59  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
60  * @author Noboru Sinohara
61  * @author David K. Taylor
62  * @author Andreas Hangler
63  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
64  */

65 public class HtmlPageTest extends WebTestCase {
66
67     /**
68      * Create an instance
69      *
70      * @param name The name of the test
71      */

72     public HtmlPageTest(final String name) {
73         super(name);
74     }
75
76     /**
77      * @exception Exception If the test fails
78      */

79     public void testConstructor() throws Exception {
80         final String htmlContent = "<html>"
81             + "<head><title>foo</title></head>"
82             + "<body>"
83             + "<p>hello world</p>"
84             + "<form id='form1' action='/formSubmit' method='post'>"
85             + "<input type='text' NAME='textInput1' value='textInput1'/>"
86             + "<input type='text' name='textInput2' value='textInput2'/>"
87             + "<input type='hidden' name='hidden1' value='hidden1'/>"
88             + "<input type='submit' name='submitInput1' value='push me'/>"
89             + "</form>"
90             + "</body></html>";
91
92         final HtmlPage page = loadPage(htmlContent);
93         assertEquals("foo", page.getTitleText());
94     }
95
96     /**
97      * @throws Exception if the test fails
98      */

99     public void testGetInputByName() throws Exception {
100         final String htmlContent = "<html>"
101             + "<head><title>foo</title></head>"
102             + "<body>"
103             + "<p>hello world</p>"
104             + "<form id='form1' action='/formSubmit' method='post'>"
105             + "<input type='text' NAME='textInput1' value='textInput1'/>"
106             + "<input type='text' name='textInput2' value='textInput2'/>"
107             + "<input type='hidden' name='hidden1' value='hidden1'/>"
108             + "<input type='submit' name='submitInput1' value='push me'/>"
109             + "</form>"
110             + "</body></html>";
111         final HtmlPage page = loadPage(htmlContent);
112
113         final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
114         final HtmlInput input = form.getInputByName("textInput1");
115         assertEquals("name", "textInput1", input.getNameAttribute());
116
117         assertEquals("value", "textInput1", input.getValueAttribute());
118         assertEquals("type", "text", input.getTypeAttribute());
119     }
120
121     /**
122      * @throws Exception if the test fails
123      */

124     public void testFormSubmit() throws Exception {
125
126         final String htmlContent = "<html>"
127             + "<head><title>foo</title></head>"
128             + "<body>"
129             + "<p>hello world</p>"
130             + "<form id='form1' action='/formSubmit' method='PoSt'>"
131             + "<input type='text' NAME='textInput1' value='textInput1'/>"
132             + "<input type='text' name='textInput2' value='textInput2'/>"
133             + "<input type='hidden' name='hidden1' value='hidden1'/>"
134             + "<input type='submit' name='submitInput1' value='push me'/>"
135             + "</form>"
136             + "</body></html>";
137         final HtmlPage page = loadPage(htmlContent);
138         final MockWebConnection webConnection = getMockConnection(page);
139
140         final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
141         final HtmlInput textInput = form.getInputByName("textInput1");
142         textInput.setValueAttribute("foo");
143
144         final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("submitInput1");
145         final HtmlPage secondPage = (HtmlPage) button.click();
146
147         final List expectedParameters = new ArrayList();
148         expectedParameters.add(new KeyValuePair("textInput1", "foo"));
149         expectedParameters.add(new KeyValuePair("textInput2", "textInput2"));
150         expectedParameters.add(new KeyValuePair("hidden1", "hidden1"));
151         expectedParameters.add(new KeyValuePair("submitInput1", "push me"));
152
153         final URL expectedUrl = new URL("http://www.gargoylesoftware.com/formSubmit");
154         final URL actualUrl = secondPage.getWebResponse().getUrl();
155         assertEquals("url", expectedUrl.toExternalForm(), actualUrl.toExternalForm());
156         assertEquals("method", SubmitMethod.POST, webConnection.getLastMethod());
157         assertEquals("parameters", expectedParameters, webConnection.getLastParameters());
158         assertNotNull(secondPage);
159     }
160
161     /**
162      * Test getHtmlElement() for all elements that can be loaded
163      *
164      * @throws Exception if the test fails
165      */

166     public void testGetHtmlElement() throws Exception {
167
168         final String htmlContent = "<html>"
169             + "<head><title>foo</title></head>"
170             + "<body>"
171             + " <p>hello world</p>"
172             + " <form id='form1' id='form1' action='/formSubmit' method='post'>"
173             + " <input type='text' NAME='textInput1' value='textInput1'/>"
174             + " <button type='submit' name='button1'>foobar</button>"
175             + " <select name='select1'>"
176             + " <option value='option1'>Option1</option>"
177             + " </select>"
178             + " <textarea name='textArea1'>foobar</textarea>"
179             + " </form>"
180             + " <a HREF='http://www.foo.com' name='anchor1'>foo.com</a>"
181             + " <table id='table1'>"
182             + " <tr>"
183             + " <th id='header1'>Header</th>"
184             + " <td id='data1'>Data</td>"
185             + " </tr>"
186             + " </table>"
187             + "</body></html>";
188         final HtmlPage page = loadPage(htmlContent);
189
190         final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
191         assertSame("form1", form, page.getHtmlElementById("form1")); //huh??
192

193         final HtmlInput input = form.getInputByName("textInput1");
194         assertSame("input1", input, form.getInputByName("textInput1")); //??
195

196         final HtmlButton button = (HtmlButton) form.getButtonsByName("button1").get(0);
197         assertSame("button1", button, form.getButtonsByName("button1").get(0));
198
199         final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select1").get(0);
200         assertSame("select1", select, form.getSelectsByName("select1").get(0));
201
202         final HtmlOption option = select.getOptionByValue("option1");
203         assertSame("option1", option, select.getOptionByValue("option1"));
204
205         final HtmlTable table = (HtmlTable) page.getHtmlElementById("table1");
206         assertSame("table1", table, page.getHtmlElementById("table1"));
207
208         final HtmlAnchor anchor = page.getAnchorByName("anchor1");
209         assertSame("anchor1", anchor, page.getAnchorByName("anchor1"));
210         assertSame("anchor3", anchor, page.getAnchorByHref("http://www.foo.com"));
211         assertSame("anchor4", anchor, page.getFirstAnchorByText("foo.com"));
212
213         final HtmlTableRow tableRow = table.getRow(0);
214         assertSame("tableRow1", tableRow, table.getRow(0));
215
216         final HtmlTableHeaderCell tableHeaderCell = (HtmlTableHeaderCell) tableRow.getCell(0);
217         assertSame("tableHeaderCell1", tableHeaderCell, tableRow.getCell(0));
218         assertSame("tableHeaderCell2", tableHeaderCell, page.getHtmlElementById("header1"));
219
220         final HtmlTableDataCell tableDataCell = (HtmlTableDataCell) tableRow.getCell(1);
221         assertSame("tableDataCell1", tableDataCell, tableRow.getCell(1));
222         assertSame("tableDataCell2", tableDataCell, page.getHtmlElementById("data1"));
223
224         final HtmlTextArea textArea = (HtmlTextArea) form.getTextAreasByName("textArea1").get(0);
225         assertSame("textArea1", textArea, form.getTextAreasByName("textArea1").get(0));
226     }
227
228     /**
229      * @throws Exception if the test fails
230      */

231     public void testGetTabbableElements_None() throws Exception {
232
233         final String htmlContent = "<html>"
234             + "<head><title>foo</title></head>"
235             + "<body>"
236             + "<p>hello world</p>"
237             + "<table><tr><td>foo</td></tr></table>"
238             + "</body></html>";
239
240         final HtmlPage page = loadPage(htmlContent);
241
242         assertEquals(Collections.EMPTY_LIST, page.getTabbableElements());
243     }
244
245     /**
246      * @throws Exception if the test fails
247      */

248     public void testGetTabbableElements_OneEnabled_OneDisabled() throws Exception {
249
250         final String htmlContent = "<html>"
251             + "<head><title>foo</title></head>"
252             + "<body>"
253             + "<form><p>hello world</p>"
254             + "<input name='foo' type='submit' disabled='disabled' id='foo'/>"
255             + "<input name='bar' type='submit' id='bar'/>"
256             + "</form></body></html>";
257         final HtmlPage page = loadPage(htmlContent);
258
259         final List expectedElements = new ArrayList();
260         expectedElements.add(page.getHtmlElementById("bar"));
261
262         assertEquals(expectedElements, page.getTabbableElements());
263     }
264
265     /**
266      * @throws Exception if the test fails
267      */

268     public void testGetTabbableElements() throws Exception {
269
270         final String htmlContent = "<html>"
271             + "<head><title>foo</title></head>"
272             + "<body>"
273             + "<a id='a' tabindex='1'>foo</a>"
274             + "<a id='b'>foo</a>"
275             + "<form>"
276             + "<a id='c' tabindex='3'>foo</a>"
277             + "<a id='d' tabindex='2'>foo</a>"
278             + "<a id='e' tabindex='0'>foo</a>"
279             + "</form>"
280             + "<a id='f' tabindex='3'>foo</a>"
281             + "<a id='g' tabindex='1'>foo</a>"
282             + "<a id='q' tabindex='-1'>foo</a>"
283             + "<form><p>hello world</p>"
284             + "<input name='foo' type='submit' disabled='disabled' id='foo'/>"
285             + "<input name='bar' type='submit' id='bar'/>"
286             + "</form></body></html>";
287         final HtmlPage page = loadPage(htmlContent);
288
289         final List expectedElements = Arrays.asList(new Object[] {page.getHtmlElementById("a"),
290                 page.getHtmlElementById("g"), page.getHtmlElementById("d"),
291                 page.getHtmlElementById("c"), page.getHtmlElementById("f"),
292                 page.getHtmlElementById("e"), page.getHtmlElementById("b"),
293                 page.getHtmlElementById("bar")});
294
295         assertEquals(expectedElements, page.getTabbableElements());
296
297         final List expectedIds = Arrays
298                 .asList(new String[] {"a", "g", "d", "c", "f", "e", "b", "bar"});
299         assertEquals(expectedIds, page.getTabbableElementIds());
300     }
301
302     /**
303      * @throws Exception if the test fails
304      */

305     public void testGetHtmlElementByAccessKey() throws Exception {
306
307         final String htmlContent = "<html>"
308             + "<head><title>foo</title></head>"
309             + "<body>"
310             + "<a id='a' accesskey='a'>foo</a>"
311             + "<a id='b'>foo</a>"
312             + "<form>"
313             + "<a id='c' accesskey='c'>foo</a>"
314             + "</form>"
315             + "<form><p>hello world</p>"
316             + "<input name='foo' type='submit' disabled='disabled' id='foo' accesskey='f'/>"
317             + "<input name='bar' type='submit' id='bar'/>"
318             + "</form></body></html>";
319         final HtmlPage page = loadPage(htmlContent);
320
321         assertEquals(page.getHtmlElementById("a"), page.getHtmlElementByAccessKey('A'));
322         assertEquals(page.getHtmlElementById("c"), page.getHtmlElementByAccessKey('c'));
323         assertNull(page.getHtmlElementByAccessKey('z'));
324     }
325
326     /**
327      * @throws Exception if the test fails
328      */

329     public void testGetHtmlElementsByAccessKey() throws Exception {
330
331         final String htmlContent = "<html>"
332             + "<head><title>foo</title></head><body>"
333             + "<a id='a' accesskey='a'>foo</a>"
334             + "<a id='b' accesskey='a'>foo</a>"
335             + "<form>"
336             + "<a id='c' accesskey='c'>foo</a>"
337             + "</form></body></html>";
338         final HtmlPage page = loadPage(htmlContent);
339
340         final List expectedElements = Arrays.asList(new Object[] {page.getHtmlElementById("a"),
341                 page.getHtmlElementById("b")});
342         final List collectedElements = page.getHtmlElementsByAccessKey('a');
343         assertEquals(expectedElements, collectedElements);
344     }
345
346     /**
347      * @throws Exception if the test fails
348      */

349     public void testAssertAllIdAttributesUnique() throws Exception {
350
351         final String htmlContent = "<html>"
352             + "<head><title>foo</title></head>"
353             + "<body>"
354             + "<a id='a' accesskey='a'>foo</a>"
355             + "<a id='b'>foo</a>"
356             + "<form>"
357             + "<a id='c' accesskey='c'>foo</a>"
358             + "</form>"
359             + "<form><p>hello world</p>"
360             + "<input name='foo' type='submit' disabled='disabled' id='foo' accesskey='f'/>"
361             + "<input name='bar' type='submit' id='bar'/>"
362             + "</form></body></html>";
363         final HtmlPage page = loadPage(htmlContent);
364
365         page.assertAllIdAttributesUnique();
366     }
367
368     /**
369      * @throws Exception if the test fails
370      */

371     public void testAssertAllIdAttributesUnique_Duplicates() throws Exception {
372
373         final String htmlContent = "<html>"
374             + "<head><title>foo</title></head>"
375             + "<body>"
376             + "<a id='dupeID' accesskey='a'>foo</a>"
377             + "<a id='b'>foo</a>"
378             + "<form>"
379             + "<a id='c' accesskey='c'>foo</a>"
380             + "</form>"
381             + "<form><p>hello world</p>"
382             + "<input name='foo' type='submit' disabled='disabled' id='dupeID' accesskey='f'/>"
383             + "<input name='bar' type='submit' id='bar'/>"
384             + "</form></body></html>";
385         final HtmlPage page = loadPage(htmlContent);
386
387         try {
388             page.assertAllIdAttributesUnique();
389             fail("Expected AssertionFailedError");
390         }
391         catch (final AssertionFailedError e) {
392             assertTrue("dupeID", e.getMessage().indexOf("dupeID") != -1);
393         }
394     }
395
396     /**
397      * @throws Exception if the test fails
398      */

399     public void testAssertAllAccessKeyAttributesUnique() throws Exception {
400
401         final String htmlContent = "<html>"
402             + "<head><title>foo</title></head>"
403             + "<body>"
404             + "<a id='a' accesskey='a'>foo</a>"
405             + "<a id='b'>foo</a>"
406             + "<form>"
407             + "<a id='c' accesskey='c'>foo</a>"
408             + "</form>"
409             + "<form><p>hello world</p>"
410             + "<input name='foo' type='submit' disabled='disabled' id='foo' accesskey='f'/>"
411             + "<input name='bar' type='submit' id='bar'/>"
412             + "</form></body></html>";
413         final HtmlPage page = loadPage(htmlContent);
414
415         page.assertAllAccessKeyAttributesUnique();
416     }
417
418     /**
419      * @throws Exception if the test fails
420      */

421     public void testAssertAllAccessKeyAttributesUnique_Duplicates() throws Exception {
422
423         final String htmlContent = "<html>"
424             + "<head><title>foo</title></head>"
425             + "<body>"
426             + "<a id='a' accesskey='a'>foo</a>"
427             + "<a id='b'>foo</a>"
428             + "<form>"
429             + "<a id='c' accesskey='c'>foo</a>"
430             + "</form>"
431             + "<form><p>hello world</p>"
432             + "<input name='foo' type='submit' disabled='disabled' id='foo' accesskey='f'/>"
433             + "<input name='bar' type='submit' id='bar' accesskey='a'/>"
434             + "</form></body></html>";
435         final HtmlPage page = loadPage(htmlContent);
436
437         try {
438             page.assertAllAccessKeyAttributesUnique();
439             fail("Expected AssertionFailedError");
440         }
441         catch (final AssertionFailedError e) {
442             //pass
443
}
444
445     }
446
447     /**
448      * @throws Exception if the test fails
449      */

450     public void testAssertAllTabIndexAttributesSet() throws Exception {
451
452         final String htmlContent = "<html>"
453             + "<head><title>foo</title></head>"
454             + "<body>"
455             + "<a id='a' tabindex='1'>foo</a>"
456             + "<form>"
457             + "<a id='c' tabindex='3'>foo</a>"
458             + "<a id='d' tabindex='2'>foo</a>"
459             + "<a id='e' tabindex='0'>foo</a>"
460             + "</form>"
461             + "<a id='f' tabindex='3'>foo</a>"
462             + "<a id='g' tabindex='1'>foo</a>"
463             + "<a id='q' tabindex='5'>foo</a>"
464             + "<form><p>hello world</p>"
465             + "</form></body></html>";
466
467         final HtmlPage page = loadPage(htmlContent);
468
469         page.assertAllTabIndexAttributesSet();
470     }
471
472     /**
473      * @throws Exception if the test fails
474      */

475     public void testAssertAllTabIndexAttributesSet_SomeMissing() throws Exception {
476
477         final String htmlContent = "<html>"
478             + "<head><title>foo</title></head>"
479             + "<body>"
480             + "<a id='a' tabindex='1'>foo</a>"
481             + "<form>"
482             + "<a id='c' tabindex='3'>foo</a>"
483             + "<a id='d' tabindex='2'>foo</a>"
484             + "<a id='e' tabindex='0'>foo</a>"
485             + "</form>"
486             + "<a id='f' tabindex='3'>foo</a>"
487             + "<a id='g'>foo</a>"
488             + "<a id='q' tabindex='1'>foo</a>"
489             + "<form><p>hello world</p>"
490             + "</form></body></html>";
491
492         final HtmlPage page = loadPage(htmlContent);
493
494         try {
495             page.assertAllTabIndexAttributesSet();
496             fail("Expected AssertionFailedError");
497         }
498         catch (final AssertionFailedError e) {
499             //pass
500
}
501     }
502
503     /**
504      * @throws Exception if the test fails
505      */

506     public void testAssertAllTabIndexAttributesSet_BadValue() throws Exception {
507
508         final String htmlContent = "<html>"
509             + "<head><title>foo</title></head>"
510             + "<body>"
511             + "<a id='a' tabindex='1'>foo</a>"
512             + "<form>"
513             + "<a id='c' tabindex='3'>foo</a>"
514             + "<a id='d' tabindex='2'>foo</a>"
515             + "<a id='e' tabindex='0'>foo</a>"
516             + "</form>"
517             + "<a id='f' tabindex='3'>foo</a>"
518             + "<a id='g' tabindex='1'>foo</a>"
519             + "<a id='q' tabindex='300000'>foo</a>"
520             + "<form><p>hello world</p>"
521             + "</form></body></html>";
522
523         final HtmlPage page = loadPage(htmlContent);
524
525         try {
526             page.assertAllTabIndexAttributesSet();
527             fail("Expected AssertionFailedError");
528         }
529         catch (final AssertionFailedError e) {
530             //pass
531
}
532     }
533
534     /**
535      * @throws Exception if the test fails
536      */

537     public void testGetFullQualifiedUrl_NoBaseSpecified() throws Exception {
538         final String htmlContent = "<html><head><title>foo</title></head><body>"
539             + "<form id='form1'>"
540             + "<table><tr><td><input type='text' id='foo'/></td></tr></table>"
541             + "</form></body></html>";
542         final WebClient client = new WebClient();
543
544         final MockWebConnection webConnection = new MockWebConnection(client);
545         webConnection.setDefaultResponse(htmlContent);
546         client.setWebConnection(webConnection);
547
548         final String urlString = URL_GARGOYLE.toExternalForm();
549         final HtmlPage page = (HtmlPage) client.getPage(new URL(urlString));
550
551         assertEquals(urlString, page.getFullyQualifiedUrl("").toExternalForm());
552         assertEquals(urlString + "foo", page.getFullyQualifiedUrl("foo").toExternalForm());
553         assertEquals("http://foo.com/bar", page
554                 .getFullyQualifiedUrl("http://foo.com/bar")
555                 .toExternalForm());
556         assertEquals("mailto:me@foo.com", page
557                 .getFullyQualifiedUrl("mailto:me@foo.com")
558                 .toExternalForm());
559
560         assertEquals(urlString + "foo", page.getFullyQualifiedUrl("foo").toExternalForm());
561         assertEquals(urlString + "bbb", page.getFullyQualifiedUrl("aaa/../bbb").toExternalForm());
562         assertEquals(urlString + "c/d", page.getFullyQualifiedUrl("c/./d").toExternalForm());
563
564         final HtmlPage secondPage = (HtmlPage) client
565                 .getPage(new URL(urlString + "/foo/bar?a=b&c=d"));
566         assertEquals(urlString + "foo/bar", secondPage.getFullyQualifiedUrl("").toExternalForm());
567         assertEquals(urlString + "foo/one", secondPage.getFullyQualifiedUrl("one").toExternalForm());
568         assertEquals(urlString + "two", secondPage.getFullyQualifiedUrl("/two").toExternalForm());
569         assertEquals(urlString + "foo/two?a=b", secondPage
570                 .getFullyQualifiedUrl("two?a=b")
571                 .toExternalForm());
572
573         final HtmlPage thirdPage = (HtmlPage) client.getPage(new URL(
574                 "http://foo.com/dog/cat/one.html"));
575         assertEquals("http://foo.com/dog/cat/one.html", thirdPage
576                 .getFullyQualifiedUrl("")
577                 .toExternalForm());
578         assertEquals("http://foo.com/dog/cat/two.html", thirdPage
579                 .getFullyQualifiedUrl("two.html")
580                 .toExternalForm());
581     }
582     /**
583      * @throws Exception if the test fails
584      */

585     public void testGetFullQualifiedUrl_WithBase() throws Exception {
586         final String htmlContent = "<html><head><title>foo</title><base HREF='http://second'></head><body>"
587             + "<form id='form1'>"
588             + "<table><tr><td><input type='text' id='foo'/></td></tr></table>"
589             + "</form></body></html>";
590         final HtmlPage page = loadPage(htmlContent);
591
592         assertEquals("http://second", page.getFullyQualifiedUrl("").toExternalForm());
593         assertEquals("http://second/foo", page.getFullyQualifiedUrl("foo").toExternalForm());
594         assertEquals("http://foo.com/bar", page
595                 .getFullyQualifiedUrl("http://foo.com/bar")
596                 .toExternalForm());
597         assertEquals("mailto:me@foo.com", page
598                 .getFullyQualifiedUrl("mailto:me@foo.com")
599                 .toExternalForm());
600
601         assertEquals("http://second/foo", page.getFullyQualifiedUrl("foo").toExternalForm());
602         assertEquals("http://second/bbb", page.getFullyQualifiedUrl("aaa/../bbb").toExternalForm());
603         assertEquals("http://second/c/d", page.getFullyQualifiedUrl("c/./d").toExternalForm());
604     }
605
606     /**
607      * @throws Exception if the test fails
608      */

609     public void testOnLoadHandler_BodyStatement() throws Exception {
610         final String htmlContent = "<html><head><title>foo</title>"
611             + "</head><body onLoad='alert(\"foo\")'>"
612             + "</body></html>";
613         final List collectedAlerts = new ArrayList();
614         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
615         assertEquals("foo", page.getTitleText());
616
617         final List expectedAlerts = Arrays.asList(new String[] {"foo"});
618         assertEquals(expectedAlerts, collectedAlerts);
619     }
620
621     /**
622      * If the onload handler contains two statements then only the first would execute
623      * @throws Exception if the test fails
624      */

625     public void testOnLoadHandler_TwoBodyStatements() throws Exception {
626         final String htmlContent = "<html><head><title>foo</title>"
627             + "</head><body onLoad='alert(\"foo\");alert(\"bar\")'>"
628             + "</body></html>";
629         final List collectedAlerts = new ArrayList();
630         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
631         assertEquals("foo", page.getTitleText());
632
633         final List expectedAlerts = Arrays.asList(new String[] {"foo", "bar"});
634         assertEquals(expectedAlerts, collectedAlerts);
635     }
636
637     /**
638      * Regression test for bug 713646
639      * @throws Exception if the test fails
640      */

641     public void testOnLoadHandler_BodyName() throws Exception {
642         final String htmlContent = "<html><head><title>foo</title>"
643             + "<script type='text/javascript'>"
644             + "window.onload=function(){alert('foo')}</script>"
645             + "</head><body></body></html>";
646         final List collectedAlerts = new ArrayList();
647         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
648         assertEquals("foo", page.getTitleText());
649
650         final List expectedAlerts = Arrays.asList(new String[] {"foo"});
651         assertEquals(expectedAlerts, collectedAlerts);
652     }
653
654     /**
655      * Regression test for bug 713646
656      * @throws Exception if the test fails
657      */

658     public void testOnLoadHandler_BodyName_NotAFunction() throws Exception {
659         final String htmlContent = "<html><head><title>foo</title></head>"
660             + "<body onLoad='foo=4711'>"
661             + "<a name='alert' HREF='javascript:alert(foo)'/>"
662             + "</body></html>";
663         final List collectedAlerts = new ArrayList();
664         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
665         assertEquals("foo", page.getTitleText());
666
667         page.getAnchorByName("alert").click();
668
669         final List expectedAlerts = Arrays.asList(new String[] {"4711"});
670         assertEquals(expectedAlerts, collectedAlerts);
671     }
672
673     /**
674      * Regression test for window.onload property
675      * @throws Exception if the test fails
676      */

677     public void testOnLoadHandler_ScriptName() throws Exception {
678         final String htmlContent = "<html><head><title>foo</title>"
679             + "<script type='text/javascript'>\n"
680             + "load=function(){alert('foo')};\n"
681             + "onload=load\n"
682             + "</script></head><body></body></html>";
683         final List collectedAlerts = new ArrayList();
684         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
685         assertEquals("foo", page.getTitleText());
686
687         final List expectedAlerts = Arrays.asList(new String[] {"foo"});
688         assertEquals(expectedAlerts, collectedAlerts);
689     }
690
691     /**
692      * Regression test for window.onload property
693      * @throws Exception if the test fails
694      */

695     public void testOnLoadHandler_ScriptNameRead() throws Exception {
696         final String htmlContent = "<html><head><title>foo</title>"
697             + "<script type='text/javascript'>\n"
698             + "load=function(){};\n"
699             + "onload=load;\n"
700             + "alert(onload);\n"
701             + "</script></head><body></body></html>";
702         final List collectedAlerts = new ArrayList();
703         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
704         assertEquals("foo", page.getTitleText());
705
706         final List expectedAlerts = Arrays.asList(new String[] {"\nfunction () {\n}\n"});
707         assertEquals(expectedAlerts, collectedAlerts);
708     }
709
710     /**
711      * @throws Exception if the test fails
712      */

713     public void testEmbeddedMetaTag_Regression() throws Exception {
714
715         final String htmlContent = "<html><head><title>foo</title>"
716             + "</head><body>"
717             + "<table><tr><td>\n"
718             + "<meta name=vs_targetSchema content=\"http://schemas.microsoft.com/intellisense/ie5\">"
719             + "<form name='form1'>"
720             + " <input type='text' name='textfield1' id='textfield1' value='foo' />"
721             + " <input type='text' name='textfield2' id='textfield2'/>"
722             + "</form>"
723             + "</td></tr></table>"
724             + "</body></html>";
725         final List collectedAlerts = new ArrayList();
726
727         // This used to blow up on page load
728
final HtmlPage page = loadPage(htmlContent, collectedAlerts);
729         assertEquals("foo", page.getTitleText());
730
731         final List expectedAlerts = Collections.EMPTY_LIST;
732         assertEquals(expectedAlerts, collectedAlerts);
733     }
734
735     /**
736      * @throws Exception if the test fails
737      */

738     public void testGetPageEncoding() throws Exception {
739
740         final String htmlContent = "<html><head>"
741             + "<title>foo</title>"
742             + "<meta http-equiv='Content-Type' content='text/html ;charset=Shift_JIS'>"
743             + "</head><body>"
744             + "<table><tr><td>\n"
745             + "<meta name=vs_targetSchema content=\"http://schemas.microsoft.com/intellisense/ie5\">"
746             + "<form name='form1'>"
747             + " <input type='text' name='textfield1' id='textfield1' value='foo' />"
748             + " <input type='text' name='textfield2' id='textfield2'/>"
749             + "</form>"
750             + "</td></tr></table>"
751             + "</body></html>";
752         final HtmlPage page = loadPage(htmlContent);
753
754         assertEquals("Shift_JIS", page.getPageEncoding());
755     }
756
757     /**
758      * @throws Exception if the test fails
759      */

760     public void testGetAllForms() throws Exception {
761
762         final String htmlContent = "<html>"
763             + "<head><title>foo</title></head>"
764             + "<body>"
765             + "<form name='one'>"
766             + "<a id='c' accesskey='c'>foo</a>"
767             + "</form>"
768             + "<form name='two'>"
769             + "<a id='c' accesskey='c'>foo</a>"
770             + "</form>"
771             + "<input name='foo' type='submit' disabled='disabled' id='foo' accesskey='f'/>"
772             + "<input name='bar' type='submit' id='bar'/>"
773             + "</form></body></html>";
774
775         final HtmlPage page = loadPage(htmlContent);
776
777         final List expectedForms = Arrays.asList(new HtmlForm[] {page.getFormByName("one"),
778                 page.getFormByName("two")});
779         assertEquals(expectedForms, page.getAllForms());
780     }
781
782     /**
783      * Test auto-refresh from a meta tag.
784      * @throws Exception if the test fails
785      */

786     public void testRefresh_MetaTag_DefaultRefreshHandler() throws Exception {
787
788         final String firstContent = "<html><head><title>first</title>"
789             + "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL=http://second\">"
790             + "</head><body></body></html>";
791         final String secondContent = "<html><head><title>second</title></head><body></body></html>";
792
793         final WebClient client = new WebClient();
794
795         final MockWebConnection webConnection = new MockWebConnection(client);
796         webConnection.setResponse(URL_FIRST, firstContent);
797         webConnection.setResponse(URL_SECOND, secondContent);
798         client.setWebConnection(webConnection);
799
800         final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
801
802         assertEquals("second", page.getTitleText());
803     }
804
805     /**
806      * Test auto-refresh from a meta tag with no URL.
807      * @throws Exception if the test fails
808      */

809     public void testRefresh_MetaTag_NoUrl() throws Exception {
810
811         final String firstContent = "<html><head><title>first</title>"
812             + "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1\">"
813             + "</head><body></body></html>";
814
815         final WebClient client = new WebClient();
816         final List collectedItems = new ArrayList();
817         client.setRefreshHandler(new LoggingRefreshHandler(collectedItems));
818
819         final MockWebConnection webConnection = new MockWebConnection(client);
820         webConnection.setResponse(URL_FIRST, firstContent);
821         client.setWebConnection(webConnection);
822
823         client.getPage(URL_FIRST);
824
825         // avoid using equals() on URL because it takes to much time (due to ip resolution)
826
assertEquals("first", collectedItems.get(0));
827         assertEquals(URL_FIRST.toExternalForm(), ((URL) collectedItems.get(1)).toExternalForm());
828         assertEquals(new Integer(1), collectedItems.get(2));
829     }
830
831     /**
832      * Ensures that if a page is supposed to refresh itself every certain amount of
833      * time, and the ImmediateRefreshHandler is being used, an OOME is avoided by
834      * not performing the refresh.
835      * @throws Exception if the test fails
836      */

837     public void testRefresh_ImmediateRefresh_AvoidOOME() throws Exception {
838
839         final String firstContent = "<html><head><title>first</title>"
840             + "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1\">"
841             + "</head><body></body></html>";
842
843         final WebClient client = new WebClient();
844         assertInstanceOf(client.getRefreshHandler(), ImmediateRefreshHandler.class);
845         try {
846             loadPage(firstContent);
847             fail("should have thrown");
848         }
849         catch (final RuntimeException e){
850             assertTrue(e.getMessage().indexOf("could have caused an OutOfMemoryError") > -1);
851         }
852         Thread.sleep(1000);
853     }
854
855     /**
856      * Test auto-refresh from a meta tag with url quoted.
857      * @throws Exception if the test fails
858      */

859     public void testRefresh_MetaTagQuoted() throws Exception {
860         final String firstContent = "<html><head><title>first</title>"
861             + "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=\"http://second\"'>"
862             + "</head><body></body></html>";
863         final String secondContent = "<html><head><title>second</title></head><body></body></html>";
864
865         final WebClient client = new WebClient();
866
867         final MockWebConnection webConnection = new MockWebConnection(client);
868         webConnection.setResponse(URL_FIRST, firstContent);
869         webConnection.setResponse(URL_SECOND, secondContent);
870         client.setWebConnection(webConnection);
871
872         final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
873
874         assertEquals("second", page.getTitleText());
875     }
876
877     /**
878      * Test auto-refresh from a meta tag with url partly quoted.
879      * @throws Exception if the test fails
880      */

881     public void testRefresh_MetaTagPartlyQuoted() throws Exception {
882         final String firstContent = "<html><head><title>first</title>"
883             + "<META HTTP-EQUIV='Refresh' CONTENT=\"0;URL='http://second\">"
884             + "</head><body></body></html>";
885         final String secondContent = "<html><head><title>second</title></head><body></body></html>";
886
887         final WebClient client = new WebClient();
888
889         final MockWebConnection webConnection = new MockWebConnection(client);
890         webConnection.setResponse(URL_FIRST, firstContent);
891         webConnection.setResponse(URL_SECOND, secondContent);
892         client.setWebConnection(webConnection);
893
894         final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
895
896         assertEquals("second", page.getTitleText());
897     }
898
899     /**
900      * Test auto-refresh from a meta tag inside noscript.
901      * @throws Exception if the test fails
902      */

903     public void testRefresh_MetaTagNoScript() throws Exception {
904
905         final String firstContent = "<html><head><title>first</title>"
906             + "<noscript>"
907             + "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=http://second\">"
908             + "</noscript>"
909             + "</head><body></body></html>";
910         final String secondContent = "<html><head><title>second</title></head><body></body></html>";
911
912         final WebClient client = new WebClient();
913
914         final MockWebConnection webConnection = new MockWebConnection(client);
915         webConnection.setResponse(URL_FIRST, firstContent);
916         webConnection.setResponse(URL_SECOND, secondContent);
917         client.setWebConnection(webConnection);
918
919         HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
920         assertEquals("first", page.getTitleText());
921
922         client.setJavaScriptEnabled(false);
923         page = (HtmlPage) client.getPage(URL_FIRST);
924         assertEquals("second", page.getTitleText());
925     }
926
927     /**
928      * Test auto-refresh from a meta tag with a refresh handler that doesn't refresh.
929      * @throws Exception if the test fails
930      */

931     public void testRefresh_MetaTag_CustomRefreshHandler() throws Exception {
932
933         final String firstContent = "<html><head><title>first</title>"
934             + "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL=http://second\">"
935             + "</head><body></body></html>";
936         final String secondContent = "<html><head><title>second</title></head><body></body></html>";
937
938         final WebClient client = new WebClient();
939         final List collectedItems = new ArrayList();
940         client.setRefreshHandler(new LoggingRefreshHandler(collectedItems));
941
942         final MockWebConnection webConnection = new MockWebConnection(client);
943         webConnection.setResponse(URL_FIRST, firstContent);
944         webConnection.setResponse(URL_SECOND, secondContent);
945         client.setWebConnection(webConnection);
946
947         final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
948
949         assertEquals("first", page.getTitleText());
950
951         // avoid using equals() on URL because it takes to much time (due to ip resolution)
952
assertEquals("first", collectedItems.get(0));
953         assertEquals(URL_SECOND.toExternalForm(), ((URL) collectedItems.get(1)).toExternalForm());
954         assertEquals(new Integer(3), collectedItems.get(2));
955     }
956
957     /**
958      * Test auto-refresh from a response header.
959      * @throws Exception if the test fails
960      */

961     public void testRefresh_HttpResponseHeader() throws Exception {
962
963         final String firstContent = "<html><head><title>first</title>"
964             + "</head><body></body></html>";
965         final String secondContent = "<html><head><title>second</title></head><body></body></html>";
966
967         final WebClient client = new WebClient();
968
969         final MockWebConnection webConnection = new MockWebConnection(client);
970         webConnection.setResponse(URL_FIRST, firstContent, 200, "OK", "text/html", Collections
971                 .singletonList(new KeyValuePair("Refresh", "3;URL=http://second")));
972         webConnection.setResponse(URL_SECOND, secondContent);
973         client.setWebConnection(webConnection);
974
975         final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
976
977         assertEquals("second", page.getTitleText());
978     }
979
980     /**
981      * @throws Exception if the test fails
982      */

983     public void testDocumentElement() throws Exception {
984
985         final String htmlContent = "<html>"
986             + "<head><title>foo</title></head>"
987             + "<body>"
988             + "</body></html>";
989         final HtmlPage page = loadPage(htmlContent);
990
991         final HtmlElement root = page.getDocumentElement();
992
993         assertNotNull(root);
994         assertEquals("html", root.getTagName());
995         assertSame(page, root.getParentNode());
996     }
997
998     /**
999      * @throws Exception if the test fails
1000     */

1001    public void testDocumentNodeType() throws Exception {
1002
1003        final String htmlContent = "<html>"
1004            + "<head><title>foo</title></head>"
1005            + "<body>"
1006            + "</body></html>";
1007        final HtmlPage page = loadPage(htmlContent);
1008
1009        final HtmlElement root = page.getDocumentElement();
1010
1011        assertEquals(DomNode.DOCUMENT_NODE, page.getNodeType());
1012        assertEquals(DomNode.ELEMENT_NODE, root.getNodeType());
1013        assertEquals("#document", page.getNodeName());
1014    }
1015
1016    /**
1017     *
1018     * @throws Exception if the test fails
1019     */

1020    public void testDeregisterFrameWithoutSrc() throws Exception {
1021
1022        final String htmlContent = "<html>"
1023            + "<head><title>foo</title></head>"
1024            + "<body>"
1025            + "<iframe />"
1026            + "<a HREF='about:blank'>link</a>"
1027            + "</body></html>";
1028
1029        final HtmlPage page = loadPage(htmlContent);
1030        final HtmlAnchor link = (HtmlAnchor) page.getAnchors().get(0);
1031        link.click();
1032    }
1033
1034    /**
1035     * Test that a return statement in onload doesn't throw any exception
1036     * @throws Exception if the test fails
1037     */

1038    public void testOnLoadReturn() throws Exception {
1039        final String htmlContent = "<html><head><title>foo</title></head>\n"
1040            + "<body onload='return true'>\n"
1041            + "</body></html>";
1042
1043        loadPage(htmlContent);
1044    }
1045
1046    /**
1047     * Test that wrong formed html code is parsed like browsers do
1048     * @throws Exception if the test fails
1049     */

1050    public void testWrongHtml_TagBeforeHtml() throws Exception {
1051        if (true) {
1052            notImplemented();
1053            return;
1054        }
1055
1056        final String htmlContent = "<div>"
1057            + "<html>"
1058            + "<head><title>foo</title>"
1059            + "<script>"
1060            + "var toto = 12345;"
1061            + "</script>"
1062            + "</head>"
1063            + "<body onload='alert(toto)'>"
1064            + "blabla"
1065            + "</body>"
1066            + "</html>";
1067        final List collectedAlerts = new ArrayList();
1068        final HtmlPage page = loadPage(htmlContent, collectedAlerts);
1069        final List expectedAlerts = Arrays.asList(new String[] {"12345"});
1070        createTestPageForRealBrowserIfNeeded(htmlContent, expectedAlerts);
1071
1072        assertEquals("foo", page.getTitleText());
1073
1074        assertEquals(expectedAlerts, collectedAlerts);
1075    }
1076}
1077
Popular Tags