KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > DocumentTest


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.javascript.host;
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 org.apache.commons.httpclient.Cookie;
47 import org.apache.commons.httpclient.HttpState;
48
49 import com.gargoylesoftware.htmlunit.BrowserVersion;
50 import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
51 import com.gargoylesoftware.htmlunit.ElementNotFoundException;
52 import com.gargoylesoftware.htmlunit.KeyValuePair;
53 import com.gargoylesoftware.htmlunit.MockWebConnection;
54 import com.gargoylesoftware.htmlunit.ScriptException;
55 import com.gargoylesoftware.htmlunit.WebClient;
56 import com.gargoylesoftware.htmlunit.WebTestCase;
57 import com.gargoylesoftware.htmlunit.html.DomNode;
58 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
59 import com.gargoylesoftware.htmlunit.html.HtmlElement;
60 import com.gargoylesoftware.htmlunit.html.HtmlInlineFrame;
61 import com.gargoylesoftware.htmlunit.html.HtmlPage;
62 import com.gargoylesoftware.htmlunit.html.BaseFrame.FrameWindow;
63
64 /**
65  * Tests for Document
66  *
67  * @version $Revision: 1.77 $
68  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
69  * @author David K. Taylor
70  * @author Barnaby Court
71  * @author Chris Erskine
72  * @author Marc Guillemot
73  * @author Michael Ottati
74  * @author <a HREF="mailto:george@murnock.com">George Murnock</a>
75  */

76 public class DocumentTest extends WebTestCase {
77     /**
78      * Create an instance
79      * @param name The name of the test
80      */

81     public DocumentTest( final String name ) {
82         super(name);
83     }
84
85     /**
86      * @throws Exception if the test fails
87      */

88     public void testFormsAccessor_TwoForms() throws Exception {
89         final String content
90             = "<html><head><title>foo</title><script>\n"
91             + "function doTest(){\n"
92             + " alert(document.forms.length)\n"
93             + " for( var i=0; i< document.forms.length; i++) {\n"
94             + " alert( document.forms[i].name )\n"
95             + " }\n"
96             + "}\n"
97             + "</script></head><body onload='doTest()'>\n"
98             + "<p>hello world</p>"
99             + "<form name='form1'>"
100             + " <input type='text' name='textfield1' value='foo' />"
101             + "</form>"
102             + "<form name='form2'>"
103             + " <input type='text' name='textfield2' value='foo' />"
104             + "</form>"
105             + "</body></html>";
106
107         final List collectedAlerts = new ArrayList();
108         final HtmlPage page = loadPage(content, collectedAlerts);
109         assertEquals("foo", page.getTitleText());
110
111         final List expectedAlerts = Arrays.asList( new String[]{
112             "2", "form1", "form2"
113         } );
114
115         assertEquals( expectedAlerts, collectedAlerts );
116     }
117
118     /**
119      * Previously, forms with no names were not being returned by document.forms.
120      * @throws Exception if the test fails
121      */

122     public void testFormsAccessor_FormWithNoName() throws Exception {
123         final String content
124             = "<html><head><title>foo</title><script>\n"
125             + "function doTest(){\n"
126             + " alert(document.forms.length)\n"
127             + "}\n"
128             + "</script></head><body onload='doTest()'>\n"
129             + "<p>hello world</p>"
130             + "<form>"
131             + " <input type='text' name='textfield1' value='foo' />"
132             + "</form>"
133             + "</body></html>";
134
135         final List collectedAlerts = new ArrayList();
136         final HtmlPage page = loadPage(content, collectedAlerts);
137         assertEquals("foo", page.getTitleText());
138
139         final List expectedAlerts = Collections.singletonList("1");
140
141         assertEquals( expectedAlerts, collectedAlerts );
142     }
143
144     /**
145      * @throws Exception if the test fails
146      */

147     public void testFormsAccessor_NoForms() throws Exception {
148         final String content
149             = "<html><head><title>foo</title><script>\n"
150             + "function doTest(){\n"
151             + " alert(document.forms.length)\n"
152             + " for( var i=0; i< document.forms.length; i++) {\n"
153             + " alert( document.forms[i].name )\n"
154             + " }\n"
155             +"}\n"
156             + "</script></head><body onload='doTest()'>\n"
157             + "<p>hello world</p>"
158             + "</body></html>";
159
160         final List collectedAlerts = new ArrayList();
161         final HtmlPage page = loadPage(content, collectedAlerts);
162         assertEquals("foo", page.getTitleText());
163
164         final List expectedAlerts = Arrays.asList( new String[]{
165             "0"
166         } );
167
168         assertEquals( expectedAlerts, collectedAlerts );
169     }
170
171     /**
172      * @throws Exception if the test fails
173      */

174     public void testDocumentWrite_AssignedToVar() throws Exception {
175         final String content
176             = "<html><head><title>foo</title><script>\n"
177             + "function doTheFoo() {\n"
178             + "var d=document.writeln\n"
179             + "d('foo')\n"
180             + "document.writeln('foo')\n"
181             + "}"
182             + "</script></head><body onload='doTheFoo()'>\n"
183             + "<p>hello world</p>"
184             + "</body></html>";
185
186         final List collectedAlerts = new ArrayList();
187         final HtmlPage page = loadPage(content, collectedAlerts);
188         assertEquals("foo", page.getTitleText());
189
190         final List expectedAlerts = Collections.EMPTY_LIST;
191
192         assertEquals( expectedAlerts, collectedAlerts );
193     }
194
195     /**
196      * @throws Exception if the test fails
197      */

198     public void testFormArray() throws Exception {
199         final WebClient client = new WebClient();
200         final MockWebConnection webConnection = new MockWebConnection( client );
201
202         final String firstContent
203             = "<html><head><SCRIPT lang='JavaScript'>"
204             + " function doSubmit(formName){"
205             + " var form = document.forms[formName];" // This line used to blow up
206
+ " form.submit()"
207             + "}"
208             + "</SCRIPT></head><body><form name='formName' method='POST' "
209             + "action='http://second'>"
210             + "<a HREF='.' id='testJavascript' name='testJavascript' "
211             + "onclick=\" doSubmit('formName');return false;\">"
212             + "Test Link </a><input type='submit' value='Login' "
213             + "name='loginButton'></form>"
214             + "</body></html> ";
215         final String secondContent
216             = "<html><head><title>second</title></head><body>\n"
217             + "<p>hello world</p>"
218             + "</body></html>";
219
220         webConnection.setResponse(URL_FIRST, firstContent);
221         webConnection.setResponse(URL_SECOND, secondContent);
222         client.setWebConnection( webConnection );
223
224         final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST);
225         assertEquals( "", page.getTitleText() );
226
227         final HtmlAnchor testAnchor = page.getAnchorByName("testJavascript");
228         final HtmlPage secondPage = (HtmlPage)testAnchor.click();
229         assertEquals( "second", secondPage.getTitleText() );
230     }
231
232
233     /**
234      * Test that forms is a live collection
235      * @throws Exception if the test fails
236      */

237     public void testFormsLive() throws Exception {
238         final String content =
239             "<html>"
240             + "<head>"
241             + "<script>"
242             + "var oCol = document.forms;"
243             + "alert(oCol.length);"
244             + "function test()"
245             + "{"
246             + " alert(oCol.length);"
247             + " alert(document.forms.length);"
248             + " alert(document.forms == oCol);"
249             + "}"
250             + "</script>"
251             + "</head>"
252             + "<body onload='test()'>"
253             + "<form id='myForm' action='foo.html'>"
254             + "</form>"
255             + "</body>"
256             + "</html>";
257         
258         
259         final List collectedAlerts = new ArrayList();
260         final List expectedAlerts = Arrays.asList( new String[]{
261             "0", "1", "1", "true"
262         } );
263         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
264         loadPage(content, collectedAlerts);
265
266         assertEquals(expectedAlerts, collectedAlerts);
267     }
268
269     /**
270      * Tests for document.links
271      * @throws Exception if the test fails
272      */

273     public void testLinks() throws Exception {
274         final String content =
275             "<html>"
276             + "<head>"
277             + "<script>"
278             + "var oCol = document.links;"
279             + "alert(oCol.length);"
280             + "function test()"
281             + "{"
282             + " alert(oCol.length);"
283             + " alert(document.links.length);"
284             + " alert(document.links == oCol);"
285             + " alert(document.links[0].id);"
286             + "}"
287             + "</script>"
288             + "</head>"
289             + "<body onload='test()'>"
290             + "<a HREF='foo.html' id='firstLink'>foo</a>"
291             + "<a HREF='foo2.html'>foo2</a>"
292             + "<a name='end'/>"
293             + "<a HREF=''>null2</a>"
294             + "</body>"
295             + "</html>";
296         
297         
298         final List collectedAlerts = new ArrayList();
299         final List expectedAlerts = Arrays.asList( new String[]{
300             "0", "3", "3", "true", "firstLink"
301         } );
302         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
303         loadPage(content, collectedAlerts);
304
305         assertEquals(expectedAlerts, collectedAlerts);
306     }
307
308     /**
309      * Ensures that <tt>document.createElement()</tt> works correctly.
310      * @throws Exception if the test fails.
311      */

312     public void testDocumentCreateElement() throws Exception {
313
314         final String htmlContent
315             = "<html>\n"
316             + " <head>\n"
317             + " <title>First</title>\n"
318             + " <script>\n"
319             + " function doTest() {\n"
320             + " // Create a DIV element.\n"
321             + " var div1 = document.createElement('div');\n"
322             + " div1.id = 'div1';\n"
323             + " document.body.appendChild(div1);\n"
324             + " alert(div1.tagName);\n"
325             + " alert(div1.nodeType);\n"
326             + " alert(div1.nodeValue);\n"
327             + " alert(div1.nodeName);\n"
328             + " // Create an INPUT element.\n"
329             + " var input = document.createElement('input');\n"
330             + " input.id = 'text1id';\n"
331             + " input.name = 'text1name';\n"
332             + " input.value = 'text1value';\n"
333             + " var form = document.getElementById('form1');\n"
334             + " form.appendChild(input);\n"
335             + " alert(document.getElementById('button1id').value);\n"
336             + " alert(document.getElementById('text1id').value);\n"
337             + " // The default type of an INPUT element is 'text'.\n"
338             + " alert(document.getElementById('text1id').type);\n"
339             + " }\n"
340             + " </script>\n"
341             + " </head>\n"
342             + " <body onload='doTest()'>\n"
343             + " <form name='form1' id='form1'>\n"
344             + " <input type='button' id='button1id' name='button1name' value='button1value'/>\n"
345             + " This is form1.\n"
346             + " </form>\n"
347             + " </body>\n"
348             + "</html>\n";
349
350         final List expectedAlerts = Arrays.asList(
351                 new String[]{ "DIV", "1", "null", "DIV", "button1value", "text1value", "text" } );
352         createTestPageForRealBrowserIfNeeded(htmlContent, expectedAlerts);
353         
354         final List collectedAlerts = new ArrayList();
355         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
356         assertEquals( "First", page.getTitleText() );
357
358         assertEquals( expectedAlerts, collectedAlerts );
359
360         final HtmlElement div1 = page.getHtmlElementById("div1");
361         assertEquals("div", div1.getTagName());
362         assertEquals(1, div1.getNodeType());
363         assertEquals(null, div1.getNodeValue());
364         assertEquals("div", div1.getNodeName());
365
366     }
367
368     /**
369      * Regression test for createTextNode
370      * @throws Exception if the test fails
371      */

372     public void testDocumentCreateTextNode() throws Exception {
373         final String htmlContent
374             = "<html><head><title>First</title><script>"
375             + "function doTest() {\n"
376             + " var text1=document.createTextNode('Some Text');\n"
377             + " var body1=document.getElementById('body');\n"
378             + " body1.appendChild(text1);\n"
379             + " alert(text1.data);\n"
380             + " alert(text1.length);\n"
381             + " alert(text1.nodeType);\n"
382             + " alert(text1.nodeValue);\n"
383             + " alert(text1.nodeName);\n"
384             + "}\n"
385             + "</script></head><body onload='doTest()' id='body'>"
386             + "</body></html>";
387
388         final List collectedAlerts = new ArrayList();
389         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
390         assertEquals( "First", page.getTitleText() );
391
392         final DomNode div1 =
393             page.getHtmlElementById("body").getLastChild();
394         assertEquals(3, div1.getNodeType());
395         assertEquals("Some Text", div1.getNodeValue());
396         assertEquals("#text", div1.getNodeName());
397
398         final List expectedAlerts = Arrays.asList( new String[]{
399             "Some Text", "9", "3", "Some Text", "#text"
400         } );
401         assertEquals( expectedAlerts, collectedAlerts );
402     }
403
404     /**
405      * Regression test for RFE 741930
406      * @throws Exception if the test fails
407      */

408     public void testAppendChild() throws Exception {
409         final String content
410             = "<html><head><title>foo</title><script>\n"
411             + "function doTest(){\n"
412             + " var form = document.forms['form1'];\n"
413             + " var div = document.createElement( 'DIV' );\n"
414             + " form.appendChild( div );\n"
415             + " var elements = document.getElementsByTagName('DIV');\n"
416             + " alert( elements.length )\n"
417             + "}\n"
418             + "</script></head><body onload='doTest()'>\n"
419             + "<p>hello world</p>"
420             + "<form name='form1'>"
421             + "</form>"
422             + "</body></html>";
423
424         final List collectedAlerts = new ArrayList();
425         final HtmlPage page = loadPage(content, collectedAlerts);
426         assertEquals("foo", page.getTitleText());
427
428         final List expectedAlerts = Arrays.asList( new String[]{
429             "1"
430         } );
431
432         assertEquals( expectedAlerts, collectedAlerts );
433     }
434
435     /**
436      * Regression test for appendChild of a text node
437      * @throws Exception if the test fails
438      */

439     public void testAppendChild_textNode() throws Exception {
440         final String content
441             = "<html><head><title>foo</title><script>\n"
442             + "function doTest(){\n"
443             + " var form = document.forms['form1'];\n"
444             + " var child = document.createTextNode( 'Some Text' );\n"
445             + " form.appendChild( child );\n"
446             + " alert( form.lastChild.data )\n"
447             + "}\n"
448             + "</script></head><body onload='doTest()'>\n"
449             + "<p>hello world</p>"
450             + "<form name='form1'>"
451             + "</form>"
452             + "</body></html>";
453
454         final List collectedAlerts = new ArrayList();
455         final HtmlPage page = loadPage(content, collectedAlerts);
456         assertEquals("foo", page.getTitleText());
457
458         final List expectedAlerts = Arrays.asList( new String[]{
459             "Some Text"
460         } );
461
462         assertEquals( expectedAlerts, collectedAlerts );
463     }
464
465     /**
466      * Regression test for cloneNode
467      * @throws Exception if the test fails
468      */

469     public void testCloneNode() throws Exception {
470         final String content
471             = "<html><head><title>foo</title><script>\n"
472             + "function doTest(){\n"
473             + " var form = document.forms['form1'];\n"
474             + " var cloneShallow = form.cloneNode(false);\n"
475             + " alert( cloneShallow!=null )\n"
476             + " alert( cloneShallow.firstChild==null )\n"
477             + " var cloneDeep = form.cloneNode(true);\n"
478             + " alert( cloneDeep!=null )\n"
479             + " alert( cloneDeep.firstChild!=null )\n"
480             + "}\n"
481             + "</script></head><body onload='doTest()'>\n"
482             + "<form name='form1'>"
483             + "<p>hello world</p>"
484             + "</form>"
485             + "</body></html>";
486
487         final List collectedAlerts = new ArrayList();
488         final HtmlPage page = loadPage(content, collectedAlerts);
489         assertEquals("foo", page.getTitleText());
490
491         final List expectedAlerts = Arrays.asList( new String[]{
492             "true", "true", "true", "true"
493         } );
494
495         assertEquals( expectedAlerts, collectedAlerts );
496     }
497
498     /**
499      * Regression test for insertBefore
500      * @throws Exception if the test fails
501      */

502     public void testInsertBefore() throws Exception {
503         final String content
504             = "<html><head><title>foo</title><script>\n"
505             + "function doTest(){\n"
506             + " var form = document.forms['form1'];\n"
507             + " var oldChild = document.getElementById('oldChild');\n"
508             + " var div = document.createElement( 'DIV' );\n"
509             + " form.insertBefore( div, oldChild );\n"
510             + " alert( form.firstChild==div )\n"
511             + "}\n"
512             + "</script></head><body onload='doTest()'>\n"
513             + "<form name='form1'><div id='oldChild'/></form>"
514             + "</body></html>";
515         final List collectedAlerts = new ArrayList();
516         final HtmlPage page = loadPage(content, collectedAlerts);
517         assertEquals("foo", page.getTitleText());
518
519         final List expectedAlerts = Arrays.asList( new String[]{
520             "true"
521         } );
522
523         assertEquals( expectedAlerts, collectedAlerts );
524     }
525
526     /**
527      * @throws Exception if the test fails
528      */

529     public void testGetElementById() throws Exception {
530         final String content
531             = "<html><head><title>First</title><script>"
532             + "function doTest() {\n"
533             + " alert(top.document.getElementById('input1').value);\n"
534             + " alert(document.getElementById(''));\n"
535             + " alert(document.getElementById('non existing'));\n"
536             + "}\n"
537             + "</script></head><body onload='doTest()'>"
538             + "<form id='form1'>"
539             + "<input id='input1' name='foo' type='text' value='bar' />"
540             + "</form>"
541             + "</body></html>";
542
543         final List collectedAlerts = new ArrayList();
544         final HtmlPage page = loadPage(content, collectedAlerts);
545         assertEquals( "First", page.getTitleText() );
546
547         final List expectedAlerts = Arrays.asList(new String[] {"bar", "null", "null"});
548         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
549         assertEquals( expectedAlerts, collectedAlerts );
550     }
551
552     /**
553      * @throws Exception if the test fails
554      */

555     public void testGetElementById_resetId() throws Exception {
556         final String content
557             = "<html><head><title>First</title><script>"
558             + "function doTest() {\n"
559             + " var input1=top.document.getElementById('input1');\n"
560             + " input1.id='newId';\n"
561             + " alert(top.document.getElementById('newId').value);\n"
562             + " alert(top.document.getElementById('input1'));\n"
563             + "}\n"
564             + "</script></head><body onload='doTest()'>"
565             + "<form id='form1'>"
566             + "<input id='input1' name='foo' type='text' value='bar' />"
567             + "</form>"
568             + "</body></html>";
569
570         final List collectedAlerts = new ArrayList();
571         final HtmlPage page = loadPage(content, collectedAlerts);
572         assertEquals( "First", page.getTitleText() );
573
574         final List expectedAlerts = Arrays.asList( new String[] {
575             "bar", "null"} );
576         assertEquals( expectedAlerts, collectedAlerts );
577     }
578
579     /**
580      * @throws Exception if the test fails
581      */

582     public void testGetElementById_setNewId() throws Exception {
583         final String firstContent
584             = "<html><head><title>First</title><script>"
585             + "function doTest() {\n"
586             + " var div1=top.document.getElementById('div1');\n"
587             + " div1.nextSibling.id='newId';\n"
588             + " alert(top.document.getElementById('newId').value);\n"
589             + "}\n"
590             + "</script></head><body onload='doTest()'>"
591             + "<form id='form1'>"
592             + "<div id='div1'/><input name='foo' type='text' value='bar' />"
593             + "</form>"
594             + "</body></html>";
595         final List collectedAlerts = new ArrayList();
596         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
597         assertEquals( "First", firstPage.getTitleText() );
598
599         final List expectedAlerts = Collections.singletonList("bar");
600         assertEquals( expectedAlerts, collectedAlerts );
601     }
602
603     /**
604      * Regression test for bug 740665
605      * @throws Exception if the test fails
606      */

607     public void testGetElementById_divId() throws Exception {
608         final String firstContent
609             = "<html><head><title>First</title><script>"
610             + "function doTest() {\n"
611             + " var element = document.getElementById('id1');\n"
612             + " alert(element.id);\n"
613             + "}\n"
614             + "</script></head><body onload='doTest()'>"
615             + "<div id='id1'></div></body></html>";
616
617         final List collectedAlerts = new ArrayList();
618         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
619         assertEquals( "First", firstPage.getTitleText() );
620
621         final List expectedAlerts = Collections.singletonList("id1");
622         assertEquals( expectedAlerts, collectedAlerts );
623     }
624
625     /**
626      * Regression test for bug 740665
627      * @throws Exception if the test fails
628      */

629     public void testGetElementById_scriptId() throws Exception {
630         final String firstContent
631             = "<html><head><title>First</title><script id='script1'>"
632             + "function doTest() {\n"
633             + " alert(top.document.getElementById('script1').id);\n"
634             + "}\n"
635             + "</script></head><body onload='doTest()'>"
636             + "</body></html>";
637
638         final List collectedAlerts = new ArrayList();
639         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
640         assertEquals( "First", firstPage.getTitleText() );
641
642         final List expectedAlerts = Collections.singletonList("script1");
643         assertEquals( expectedAlerts, collectedAlerts );
644     }
645
646     /**
647      * Regression test for bug 740665
648      * @throws Exception if the test fails
649      */

650     public void testGetElementById_scriptType() throws Exception {
651         final String firstContent
652             = "<html><head><title>First</title>"
653             + "<script id='script1' type='text/javascript'>\n"
654             + "doTest=function () {\n"
655             + " alert(top.document.getElementById('script1').type);\n"
656             + "}\n"
657             + "</script></head><body onload='doTest()'>"
658             + "</body></html>";
659
660         final List collectedAlerts = new ArrayList();
661         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
662         assertEquals( "First", firstPage.getTitleText() );
663
664         final List expectedAlerts = Collections.singletonList("text/javascript");
665         assertEquals( expectedAlerts, collectedAlerts );
666     }
667
668     /**
669      * Regression test for bug 740665
670      * @throws Exception if the test fails
671      */

672     public void testGetElementById_scriptSrc() throws Exception {
673         final WebClient webClient = new WebClient();
674         final MockWebConnection webConnection = new MockWebConnection( webClient );
675         webClient.setWebConnection( webConnection );
676
677         final String firstContent
678             = "<html><head><title>First</title>"
679             + "<script id='script1' SRC='http://script'>\n"
680             + "</script></head><body onload='doTest()'>"
681             + "</body></html>";
682         webConnection.setResponse(URL_FIRST, firstContent);
683
684         final String scriptContent
685             = "doTest=function () {\n"
686             + " alert(top.document.getElementById('script1').src);\n"
687             + "}\n";
688         webConnection.setResponse(new URL("http://script"), scriptContent, "text/javascript");
689
690         final List collectedAlerts = new ArrayList();
691         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
692
693         final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
694         assertEquals( "First", firstPage.getTitleText() );
695
696         final List expectedAlerts = Collections.singletonList("http://script");
697         assertEquals( expectedAlerts, collectedAlerts );
698     }
699
700     /**
701      * Regression test for parentNode with nested elements
702      * @throws Exception if the test fails
703      */

704     public void testParentNode_Nested() throws Exception {
705         final String firstContent
706             = "<html><head><title>First</title><script>"
707             + "function doTest() {\n"
708             + " var div1=document.getElementById('childDiv');\n"
709             + " alert(div1.parentNode.id);\n"
710             + "}\n"
711             + "</script></head><body onload='doTest()'>"
712             + "<div id='parentDiv'><div id='childDiv'></div></div>"
713             + "</body></html>";
714
715         final List collectedAlerts = new ArrayList();
716         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
717         assertEquals( "First", firstPage.getTitleText() );
718
719         final HtmlElement div1 = firstPage.getHtmlElementById("childDiv");
720         assertEquals("parentDiv",
721             ((HtmlElement) div1.getParentNode()).getAttributeValue("id"));
722
723         final List expectedAlerts = Collections.singletonList("parentDiv");
724         assertEquals( expectedAlerts, collectedAlerts );
725     }
726
727     /**
728      * Regression test for parentNode of document
729      * @throws Exception if the test fails
730      */

731     public void testParentNode_Document() throws Exception {
732         final String firstContent
733             = "<html><head><title>First</title><script>"
734             + "function doTest() {\n"
735             + " alert(document.parentNode==null);\n"
736             + "}\n"
737             + "</script></head><body onload='doTest()'>"
738             + "</body></html>";
739
740         final List collectedAlerts = new ArrayList();
741         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
742         assertEquals( "First", firstPage.getTitleText() );
743
744         final List expectedAlerts = Collections.singletonList("true");
745         assertEquals( expectedAlerts, collectedAlerts );
746     }
747
748     /**
749      * Regression test for parentNode and createElement
750      * @throws Exception if the test fails
751      */

752     public void testParentNode_CreateElement() throws Exception {
753         final String firstContent
754             = "<html><head><title>First</title><script>"
755             + "function doTest() {\n"
756             + " var div1=document.createElement('div');\n"
757             + " alert(div1.parentNode==null);\n"
758             + "}\n"
759             + "</script></head><body onload='doTest()'>"
760             + "</body></html>";
761
762         final List collectedAlerts = new ArrayList();
763         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
764         assertEquals( "First", firstPage.getTitleText() );
765
766         final List expectedAlerts = Collections.singletonList("true");
767         assertEquals( expectedAlerts, collectedAlerts );
768     }
769
770     /**
771      * Regression test for parentNode and appendChild
772      * @throws Exception if the test fails
773      */

774     public void testParentNode_AppendChild() throws Exception {
775         final String firstContent
776             = "<html><head><title>First</title><script>"
777             + "function doTest() {\n"
778             + " var childDiv=document.getElementById('childDiv');\n"
779             + " var parentDiv=document.getElementById('parentDiv');\n"
780             + " parentDiv.appendChild(childDiv);\n"
781             + " alert(childDiv.parentNode.id);\n"
782             + "}\n"
783             + "</script></head><body onload='doTest()'>"
784             + "<div id='parentDiv'></div><div id='childDiv'></div>"
785             + "</body></html>";
786
787         final List collectedAlerts = new ArrayList();
788         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
789         assertEquals( "First", firstPage.getTitleText() );
790
791         final HtmlElement childDiv = firstPage.getHtmlElementById("childDiv");
792         assertEquals("parentDiv",
793             ((HtmlElement) childDiv.getParentNode()).getAttributeValue("id"));
794
795         final List expectedAlerts = Collections.singletonList("parentDiv");
796         assertEquals( expectedAlerts, collectedAlerts );
797     }
798
799     /**
800      * Regression test for documentElement of document
801      * @throws Exception if the test fails
802      */

803     public void testDocumentElement() throws Exception {
804         final String firstContent
805             = "<html><head><title>First</title><script>"
806             + "function doTest() {\n"
807             + " alert(document.documentElement!=null);\n"
808             + " alert(document.documentElement.tagName);\n"
809             + " alert(document.documentElement.parentNode==document);\n"
810             + "}\n"
811             + "</script></head><body onload='doTest()'>"
812             + "</body></html>";
813
814         final List collectedAlerts = new ArrayList();
815         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
816         assertEquals( "First", firstPage.getTitleText() );
817
818         final List expectedAlerts = Arrays.asList( new String[] {
819             "true", "HTML", "true"} );
820         assertEquals( expectedAlerts, collectedAlerts );
821     }
822
823     /**
824      * Regression test for firstChild with nested elements
825      * @throws Exception if the test fails
826      */

827     public void testFirstChild_Nested() throws Exception {
828         final String firstContent
829             = "<html><head><title>First</title><script>"
830             + "function doTest() {\n"
831             + " var div1=document.getElementById('parentDiv');\n"
832             + " alert(div1.firstChild.id);\n"
833             + "}\n"
834             + "</script></head><body onload='doTest()'>"
835             + "<div id='parentDiv'><div id='childDiv'/><div id='childDiv2'/></div>"
836             + "</body></html>";
837
838         final List collectedAlerts = new ArrayList();
839         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
840         assertEquals( "First", firstPage.getTitleText() );
841
842         final HtmlElement div1 = firstPage.getHtmlElementById("parentDiv");
843         assertEquals("childDiv",
844             ((HtmlElement) div1.getFirstChild()).getAttributeValue("id"));
845
846         final List expectedAlerts = Collections.singletonList("childDiv");
847         assertEquals( expectedAlerts, collectedAlerts );
848     }
849
850
851     /**
852      * Regression test for firstChild and appendChild
853      * @throws Exception if the test fails
854      */

855     public void testFirstChild_AppendChild() throws Exception {
856         final String firstContent
857             = "<html><head><title>First</title><script>"
858             + "function doTest() {\n"
859             + " var childDiv=document.getElementById('childDiv');\n"
860             + " var parentDiv=document.getElementById('parentDiv');\n"
861             + " parentDiv.appendChild(childDiv);\n"
862             + " var childDiv2=document.getElementById('childDiv2');\n"
863             + " parentDiv.appendChild(childDiv2);\n"
864             + " alert(parentDiv.firstChild.id);\n"
865             + "}\n"
866             + "</script></head><body onload='doTest()'>"
867             + "<div id='parentDiv'/><div id='childDiv'/><div id='childDiv2'/>"
868             + "</body></html>";
869
870         final List collectedAlerts = new ArrayList();
871         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
872         assertEquals( "First", firstPage.getTitleText() );
873
874         final HtmlElement parentDiv = firstPage.getHtmlElementById("parentDiv");
875         assertEquals("childDiv",
876             ((HtmlElement) parentDiv.getFirstChild()).getAttributeValue("id"));
877
878         final List expectedAlerts = Collections.singletonList("childDiv");
879         assertEquals( expectedAlerts, collectedAlerts );
880     }
881
882     /**
883      * Regression test for lastChild with nested elements
884      * @throws Exception if the test fails
885      */

886     public void testLastChild_Nested() throws Exception {
887         final String lastContent
888             = "<html><head><title>Last</title><script>"
889             + "function doTest() {\n"
890             + " var div1=document.getElementById('parentDiv');\n"
891             + " alert(div1.lastChild.id);\n"
892             + "}\n"
893             + "</script></head><body onload='doTest()'>"
894             + "<div id='parentDiv'><div id='childDiv1'/><div id='childDiv'/></div>"
895             + "</body></html>";
896
897         final List collectedAlerts = new ArrayList();
898         final HtmlPage lastPage = loadPage(lastContent, collectedAlerts);
899         assertEquals( "Last", lastPage.getTitleText() );
900
901         final HtmlElement parentDiv = lastPage.getHtmlElementById("parentDiv");
902         assertEquals("childDiv",
903             ((HtmlElement) parentDiv.getLastChild()).getAttributeValue("id"));
904
905         final List expectedAlerts = Collections.singletonList("childDiv");
906         assertEquals( expectedAlerts, collectedAlerts );
907     }
908
909     /**
910      * Regression test for lastChild and appendChild
911      * @throws Exception if the test fails
912      */

913     public void testLastChild_AppendChild() throws Exception {
914         final String lastContent
915             = "<html><head><title>Last</title><script>"
916             + "function doTest() {\n"
917             + " var childDiv1=document.getElementById('childDiv1');\n"
918             + " var parentDiv=document.getElementById('parentDiv');\n"
919             + " parentDiv.appendChild(childDiv1);\n"
920             + " var childDiv=document.getElementById('childDiv');\n"
921             + " parentDiv.appendChild(childDiv);\n"
922             + " alert(parentDiv.lastChild.id);\n"
923             + "}\n"
924             + "</script></head><body onload='doTest()'>"
925             + "<div id='parentDiv'/><div id='childDiv1'/><div id='childDiv'/>"
926             + "</body></html>";
927
928         final List collectedAlerts = new ArrayList();
929         final HtmlPage lastPage = loadPage(lastContent, collectedAlerts);
930         assertEquals( "Last", lastPage.getTitleText() );
931
932         final HtmlElement parentDiv = lastPage.getHtmlElementById("parentDiv");
933         assertEquals("childDiv",
934             ((HtmlElement) parentDiv.getLastChild()).getAttributeValue("id"));
935
936         final List expectedAlerts = Collections.singletonList("childDiv");
937         assertEquals( expectedAlerts, collectedAlerts );
938     }
939
940     /**
941      * Regression test for nextSibling with nested elements
942      * @throws Exception if the test fails
943      */

944     public void testNextSibling_Nested() throws Exception {
945         final String lastContent
946             = "<html><head><title>Last</title><script>"
947             + "function doTest() {\n"
948             + " var div1=document.getElementById('previousDiv');\n"
949             + " alert(div1.nextSibling.id);\n"
950             + "}\n"
951             + "</script></head><body onload='doTest()'>"
952             + "<div id='parentDiv'><div id='previousDiv'/><div id='nextDiv'/></div>"
953             + "</body></html>";
954
955         final List collectedAlerts = new ArrayList();
956         final HtmlPage lastPage = loadPage(lastContent, collectedAlerts);
957         assertEquals( "Last", lastPage.getTitleText() );
958
959         final HtmlElement div1 = lastPage.getHtmlElementById("previousDiv");
960         assertEquals("nextDiv",
961             ((HtmlElement) div1.getNextSibling()).getAttributeValue("id"));
962
963         final List expectedAlerts = Collections.singletonList("nextDiv");
964         assertEquals( expectedAlerts, collectedAlerts );
965     }
966
967     /**
968      * Regression test for nextSibling and appendChild
969      * @throws Exception if the test fails
970      */

971     public void testNextSibling_AppendChild() throws Exception {
972         final String lastContent
973             = "<html><head><title>Last</title><script>"
974             + "function doTest() {\n"
975             + " var previousDiv=document.getElementById('previousDiv');\n"
976             + " var parentDiv=document.getElementById('parentDiv');\n"
977             + " parentDiv.appendChild(previousDiv);\n"
978             + " var nextDiv=document.getElementById('nextDiv');\n"
979             + " parentDiv.appendChild(nextDiv);\n"
980             + " alert(previousDiv.nextSibling.id);\n"
981             + "}\n"
982             + "</script></head><body onload='doTest()'>"
983             + "<div id='parentDiv'/><div id='junk1'/><div id='previousDiv'/><div id='junk2'/><div id='nextDiv'/>"
984             + "</body></html>";
985
986         final List collectedAlerts = new ArrayList();
987         final HtmlPage lastPage = loadPage(lastContent, collectedAlerts);
988         assertEquals( "Last", lastPage.getTitleText() );
989
990         final HtmlElement previousDiv = lastPage.getHtmlElementById("previousDiv");
991         assertEquals("nextDiv",
992             ((HtmlElement) previousDiv.getNextSibling()).getAttributeValue("id"));
993
994         final List expectedAlerts = Collections.singletonList("nextDiv");
995         assertEquals( expectedAlerts, collectedAlerts );
996     }
997
998     /**
999      * Regression test for previousSibling with nested elements
1000     * @throws Exception if the test fails
1001     */

1002    public void testPreviousSibling_Nested() throws Exception {
1003        final String lastContent
1004            = "<html><head><title>Last</title><script>"
1005            + "function doTest() {\n"
1006            + " var div1=document.getElementById('nextDiv');\n"
1007            + " alert(div1.previousSibling.id);\n"
1008            + "}\n"
1009            + "</script></head><body onload='doTest()'>"
1010            + "<div id='parentDiv'><div id='previousDiv'/><div id='nextDiv'/></div>"
1011            + "</body></html>";
1012
1013        final List collectedAlerts = new ArrayList();
1014        final HtmlPage lastPage = loadPage(lastContent, collectedAlerts);
1015        assertEquals( "Last", lastPage.getTitleText() );
1016
1017        final HtmlElement div1 = lastPage.getHtmlElementById("nextDiv");
1018        assertEquals("previousDiv",
1019            ((HtmlElement) div1.getPreviousSibling()).getAttributeValue("id"));
1020
1021        final List expectedAlerts = Collections.singletonList("previousDiv");
1022        assertEquals( expectedAlerts, collectedAlerts );
1023    }
1024
1025    /**
1026     * Regression test for previousSibling and appendChild
1027     * @throws Exception if the test fails
1028     */

1029    public void testPreviousSibling_AppendChild() throws Exception {
1030        final String lastContent
1031            = "<html><head><title>Last</title><script>"
1032            + "function doTest() {\n"
1033            + " var previousDiv=document.getElementById('previousDiv');\n"
1034            + " var parentDiv=document.getElementById('parentDiv');\n"
1035            + " parentDiv.appendChild(previousDiv);\n"
1036            + " var nextDiv=document.getElementById('nextDiv');\n"
1037            + " parentDiv.appendChild(nextDiv);\n"
1038            + " alert(nextDiv.previousSibling.id);\n"
1039            + "}\n"
1040            + "</script></head><body onload='doTest()'>"
1041            + "<div id='parentDiv'/><div id='junk1'/><div id='previousDiv'/><div id='junk2'/><div id='nextDiv'/>"
1042            + "</body></html>";
1043
1044        final List collectedAlerts = new ArrayList();
1045        final HtmlPage lastPage = loadPage(lastContent, collectedAlerts);
1046        assertEquals( "Last", lastPage.getTitleText() );
1047
1048        final HtmlElement nextDiv = lastPage.getHtmlElementById("nextDiv");
1049        assertEquals("previousDiv",
1050            ((HtmlElement) nextDiv.getPreviousSibling()).getAttributeValue("id"));
1051
1052        final List expectedAlerts = Collections.singletonList("previousDiv");
1053        assertEquals( expectedAlerts, collectedAlerts );
1054    }
1055
1056    /**
1057     * @throws Exception if the test fails
1058     */

1059    public void testAllProperty_KeyByName() throws Exception {
1060        final String firstContent
1061            = "<html><head><title>First</title><script>"
1062            + "function doTest() {\n"
1063            + " alert(document.all['input1'].value);\n"
1064            + " alert(document.all['foo2'].value);\n"
1065            + "}\n"
1066            + "</script></head><body onload='doTest()'><form id='form1'>"
1067            + " <input id='input1' name='foo1' type='text' value='tangerine' />"
1068            + " <input id='input2' name='foo2' type='text' value='ginger' />"
1069            + "</form>"
1070            + "</body></html>";
1071
1072        final List collectedAlerts = new ArrayList();
1073        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1074        assertEquals( "First", firstPage.getTitleText() );
1075
1076        final List expectedAlerts = Arrays.asList( new String[]{
1077            "tangerine", "ginger"} );
1078        assertEquals( expectedAlerts, collectedAlerts );
1079    }
1080
1081    /**
1082     * Regression test for bug 707750
1083     * @throws Exception if the test fails
1084     */

1085    public void testAllProperty_CalledDuringPageLoad() throws Exception {
1086        final String firstContent
1087            = "<html><body>"
1088            + "<div id='ARSMenuDiv1' style='VISIBILITY: hidden; POSITION: absolute; z-index: 1000000'></div>"
1089            + "<script language='Javascript'>"
1090            + " var divObj = document.all['ARSMenuDiv1'];"
1091            + " alert(divObj.tagName);"
1092            + "</script></body></html>";
1093
1094        final List collectedAlerts = new ArrayList();
1095        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1096        assertEquals("", firstPage.getTitleText());
1097
1098        final List expectedAlerts = Collections.singletonList("DIV");
1099        assertEquals( expectedAlerts, collectedAlerts );
1100    }
1101
1102    /**
1103     * @throws Exception if the test fails
1104     */

1105    public void testDocumentWrite() throws Exception {
1106        final String firstContent
1107            = "<html><head><title>First</title></head><body>"
1108            + "<script>document.write(\"<div id='div1'></div>\")</script>"
1109            + "</form></body></html>";
1110
1111        final List collectedAlerts = new ArrayList();
1112        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1113        assertEquals( "First", firstPage.getTitleText() );
1114
1115        try {
1116            firstPage.getHtmlElementById("div1");
1117        }
1118        catch (final ElementNotFoundException e) {
1119            fail("Element not written to page as expected");
1120        }
1121    }
1122
1123    /**
1124     * Regression test for bug 743241
1125     * @throws Exception if the test fails
1126     */

1127    public void testDocumentWrite_LoadScript() throws Exception {
1128        final WebClient webClient = new WebClient();
1129        final MockWebConnection webConnection = new MockWebConnection( webClient );
1130        webClient.setWebConnection( webConnection );
1131
1132        final String firstContent
1133            = "<html><head><title>First</title></head><body>"
1134            + "<script SRC='http://script'></script>"
1135            + "</form></body></html>";
1136        webConnection.setResponse(URL_FIRST, firstContent);
1137
1138        final String scriptContent
1139            = "document.write(\"<div id='div1'></div>\");";
1140        webConnection.setResponse(new URL("http://script"), scriptContent, "text/javascript");
1141
1142        final List collectedAlerts = new ArrayList();
1143        webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
1144
1145        final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
1146        assertEquals( "First", firstPage.getTitleText() );
1147
1148        try {
1149            firstPage.getHtmlElementById("div1");
1150        }
1151        catch (final ElementNotFoundException e) {
1152            fail("Element not written to page as expected");
1153        }
1154    }
1155
1156    /**
1157     * Regression test for bug 715379
1158     * @throws Exception if the test fails
1159     */

1160    public void testDocumentWrite_script() throws Exception {
1161        final WebClient webClient = new WebClient();
1162        final MockWebConnection webConnection = new MockWebConnection( webClient );
1163        webClient.setWebConnection( webConnection );
1164
1165        final String mainContent
1166            = "<html><head><title>Main</title></head><body>"
1167            + "<iframe name='iframe' id='iframe' SRC='http://first'></iframe>"
1168            + "<script type='text/javascript'>"
1169            + "document.write('<script type=\"text/javascript\" SRC=\"http://script\"></' + 'script>');"
1170            + "</script></body></html> ";
1171        webConnection.setResponse(new URL("http://main"), mainContent);
1172
1173        final String firstContent
1174            = "<html><body><h1 id='first'>First</h1></body></html>";
1175        webConnection.setResponse(URL_FIRST, firstContent);
1176
1177        final String secondContent
1178            = "<html><body><h1 id='second'>Second</h1></body></html>";
1179        webConnection.setResponse(URL_SECOND, secondContent);
1180
1181        final String scriptContent
1182            = "document.getElementById('iframe').src = 'http://second';";
1183        webConnection.setResponse(new URL("http://script"), scriptContent, "text/javascript");
1184
1185        final List collectedAlerts = new ArrayList();
1186        webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
1187
1188        final HtmlPage mainPage = ( HtmlPage )webClient.getPage( new URL( "http://main" ) );
1189        assertEquals( "Main", mainPage.getTitleText() );
1190
1191        final HtmlInlineFrame iFrame =
1192            (HtmlInlineFrame) mainPage.getHtmlElementById( "iframe" );
1193
1194        assertEquals( "http://second", iFrame.getSrcAttribute() );
1195
1196        final HtmlPage enclosedPage = (HtmlPage) iFrame.getEnclosedPage();
1197        // This will blow up if the script hasn't been written to the document
1198
// and executed so the second page has been loaded.
1199
enclosedPage.getHtmlElementById( "second" );
1200    }
1201    
1202    /**
1203     * @throws Exception if the test fails
1204     */

1205    public void testDocumentWrite_InDOM() throws Exception {
1206        final String mainContent
1207            = "<html><head><title>First</title></head><body>"
1208            + "<script type='text/javascript'>"
1209            + "document.write('<a id=\"blah\">Hello World</a>');"
1210            + "alert(document.getElementById('blah').tagName);"
1211            + "</script>"
1212            + "</body></html>";
1213
1214        final List collectedAlerts = new ArrayList();
1215        final HtmlPage firstPage = loadPage(mainContent, collectedAlerts);
1216        assertEquals("First", firstPage.getTitleText());
1217
1218        assertEquals(collectedAlerts, Collections.singletonList("A"));
1219    }
1220
1221    /**
1222     * @throws Exception if the test fails
1223     */

1224    public void testGetReferrer() throws Exception {
1225        final WebClient webClient = new WebClient();
1226        final MockWebConnection webConnection = new MockWebConnection( webClient );
1227
1228        final String firstContent
1229            = "<html><head><title>First</title></head><body onload='alert(document.referrer);'>"
1230            + "</form></body></html>";
1231
1232        final List responseHeaders = Collections.singletonList( new KeyValuePair("referrer", "http://ref") );
1233        webConnection.setResponse(
1234            URL_FIRST, firstContent, 200, "OK", "text/html", responseHeaders );
1235        webClient.setWebConnection( webConnection );
1236
1237        final List collectedAlerts = new ArrayList();
1238        webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
1239
1240        final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
1241        assertEquals( "First", firstPage.getTitleText() );
1242
1243        assertEquals( Collections.singletonList("http://ref"), collectedAlerts );
1244    }
1245
1246
1247    /**
1248     * @throws Exception if the test fails
1249     */

1250    public void testGetReferrer_NoneSpecified() throws Exception {
1251        final String firstContent
1252            = "<html><head><title>First</title></head><body onload='alert(document.referrer);'>"
1253            + "</form></body></html>";
1254
1255        final List collectedAlerts = new ArrayList();
1256        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1257        assertEquals( "First", firstPage.getTitleText() );
1258
1259        assertEquals( Collections.singletonList(""), collectedAlerts );
1260    }
1261
1262    /**
1263     * @throws Exception if the test fails
1264     */

1265    public void testGetURL() throws Exception {
1266        final String firstContent
1267            = "<html><head><title>First</title></head><body onload='alert(document.URL);'>"
1268            + "</form></body></html>";
1269
1270        final List collectedAlerts = new ArrayList();
1271        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1272        assertEquals( "First", firstPage.getTitleText() );
1273
1274        assertEquals(Collections.singletonList(URL_GARGOYLE.toExternalForm()), collectedAlerts);
1275    }
1276
1277    /**
1278     * @throws Exception if the test fails
1279     */

1280    public void testGetElementsByTagName() throws Exception {
1281        final String firstContent
1282            = "<html><head><title>First</title><script>"
1283            + "function doTest() {\n"
1284            + " var elements = document.getElementsByTagName('input');\n"
1285            + " for( i=0; i<elements.length; i++ ) {\n"
1286            + " alert(elements[i].type);\n"
1287            + " }\n"
1288            + "}\n"
1289            + "</script></head><body onload='doTest()'>"
1290            + "<form><input type='button' name='button1' value='pushme'></form>"
1291            + "</body></html>";
1292
1293        final List collectedAlerts = new ArrayList();
1294        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1295        assertEquals( "First", firstPage.getTitleText() );
1296
1297        final List expectedAlerts = Collections.singletonList("button");
1298        assertEquals(expectedAlerts, collectedAlerts);
1299    }
1300
1301    /**
1302     * Regression test for bug 740636
1303     * @throws Exception if the test fails
1304     */

1305    public void testGetElementsByTagName_CaseInsensitive() throws Exception {
1306        final String firstContent
1307            = "<html><head><title>First</title><script>"
1308            + "function doTest() {\n"
1309            + " var elements = document.getElementsByTagName('InPuT');\n"
1310            + " for( i=0; i<elements.length; i++ ) {\n"
1311            + " alert(elements[i].type);\n"
1312            + " }\n"
1313            + "}\n"
1314            + "</script></head><body onload='doTest()'>"
1315            + "<form><input type='button' name='button1' value='pushme'></form>"
1316            + "</body></html>";
1317
1318        final List collectedAlerts = new ArrayList();
1319        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1320        assertEquals( "First", firstPage.getTitleText() );
1321
1322        final List expectedAlerts = Collections.singletonList("button");
1323        assertEquals(expectedAlerts, collectedAlerts);
1324    }
1325
1326    /**
1327     * Regression test for bug 740605
1328     * @throws Exception if the test fails
1329     */

1330    public void testGetElementsByTagName_Inline() throws Exception {
1331        final String firstContent
1332            = "<html><body><script type=\"text/javascript\">"
1333            + "alert(document.getElementsByTagName('script').length);"
1334            + "</script></body></html>";
1335
1336        final List collectedAlerts = new ArrayList();
1337        loadPage(firstContent, collectedAlerts);
1338        final List expectedAlerts = Collections.singletonList("1");
1339        assertEquals(expectedAlerts, collectedAlerts);
1340    }
1341
1342    /**
1343     * Regression test for bug 740605
1344     * @throws Exception if the test fails
1345     */

1346    public void testGetElementsByTagName_LoadScript() throws Exception {
1347        final WebClient webClient = new WebClient();
1348        final MockWebConnection webConnection = new MockWebConnection( webClient );
1349        webClient.setWebConnection( webConnection );
1350
1351        final String firstContent
1352            = "<html><body><script SRC=\"http://script\"></script></body></html>";
1353        webConnection.setResponse(URL_FIRST, firstContent);
1354
1355        final String scriptContent
1356            = "alert(document.getElementsByTagName('script').length);";
1357        webConnection.setResponse(new URL("http://script"), scriptContent, "text/javascript");
1358
1359        final List collectedAlerts = new ArrayList();
1360        webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
1361
1362        webClient.getPage( URL_FIRST );
1363
1364        final List expectedAlerts = Collections.singletonList("1");
1365        assertEquals(expectedAlerts, collectedAlerts);
1366    }
1367
1368    /**
1369     * @throws Exception if the test fails
1370     */

1371    public void testDocumentAll_WithParentheses() throws Exception {
1372        final String firstContent
1373            = "<html><head><title>First</title><script>"
1374            + "function doTest() {\n"
1375            + " var length = document.all.length;\n"
1376            + " for( i=0; i< length; i++ ) {\n"
1377            + " alert(document.all(i).tagName);\n"
1378            + " }\n"
1379            + "}\n"
1380            + "</script></head><body onload='doTest()'>"
1381            + "</body></html>";
1382
1383        final List collectedAlerts = new ArrayList();
1384        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1385        assertEquals( "First", firstPage.getTitleText() );
1386
1387        final List expectedAlerts = Arrays.asList( new String[] {
1388            "HTML", "HEAD", "TITLE", "SCRIPT", "BODY"} );
1389        assertEquals( expectedAlerts, collectedAlerts );
1390    }
1391
1392    /**
1393     * @throws Exception if the test fails
1394     */

1395    public void testDocumentAll_IndexByInt() throws Exception {
1396        final String firstContent
1397            = "<html><head><title>First</title><script>"
1398            + "function doTest() {\n"
1399            + " var length = document.all.length;\n"
1400            + " for( i=0; i< length; i++ ) {\n"
1401            + " alert(document.all[i].tagName);\n"
1402            + " }\n"
1403            + "}\n"
1404            + "</script></head><body onload='doTest()'>"
1405            + "</body></html>";
1406
1407        final List collectedAlerts = new ArrayList();
1408        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1409        assertEquals( "First", firstPage.getTitleText() );
1410
1411        final List expectedAlerts = Arrays.asList( new String[] {
1412            "HTML", "HEAD", "TITLE", "SCRIPT", "BODY"} );
1413        assertEquals( expectedAlerts, collectedAlerts );
1414    }
1415
1416    /**
1417     * @throws Exception if the test fails
1418     */

1419    public void testDocumentAll_Item() throws Exception {
1420        final String firstContent
1421            = "<html><head><title>First</title><script>"
1422            + "function doTest() {\n"
1423            + " alert(document.all.item(0).tagName);\n"
1424            + "}\n"
1425            + "</script></head><body onload='doTest()'>"
1426            + "</body></html>";
1427
1428        final List collectedAlerts = new ArrayList();
1429        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1430        assertEquals( "First", firstPage.getTitleText() );
1431
1432        final List expectedAlerts = Arrays.asList( new String[] {"HTML"} );
1433        assertEquals( expectedAlerts, collectedAlerts );
1434    }
1435
1436    /**
1437     * @throws Exception if the test fails
1438     */

1439    public void testDocumentAll_NamedItem() throws Exception {
1440        final String firstContent
1441            = "<html><head><title>First</title><script>\n"
1442            + "function doTest() {\n"
1443            + " alert(document.all.namedItem('form1').name);\n"
1444            + " alert(document.all.namedItem('form2').id);\n"
1445            + " alert(document.all.namedItem('form3').length);\n"
1446            + "}\n"
1447            + "</script></head><body onload='doTest()'>\n"
1448            + "<form name='form1'></form>\n"
1449            + "<form id='form2'></form>\n"
1450            + "<form name='form3'></form>\n"
1451            + "<form name='form3'></form>\n"
1452            + "</body></html>";
1453
1454        final List collectedAlerts = new ArrayList();
1455        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1456        assertEquals( "First", firstPage.getTitleText() );
1457
1458        final List expectedAlerts = Arrays.asList( new String[] {"form1", "form2", "2"} );
1459        assertEquals( expectedAlerts, collectedAlerts );
1460    }
1461
1462    /**
1463     * @throws Exception if the test fails
1464     */

1465    public void testDocumentAll_tags() throws Exception {
1466        final String firstContent
1467            = "<html><head><title>First</title><script>"
1468            + "function doTest() {\n"
1469            + " var inputs = document.all.tags('input');\n"
1470            + " var inputCount = inputs.length;\n"
1471            + " for( i=0; i< inputCount; i++ ) {\n"
1472            + " alert(inputs[i].name);\n"
1473            + " }\n"
1474            + " // Make sure tags() returns an element array that you can call item() on.\n"
1475            + " alert(document.all.tags('input').item(0).name);\n"
1476            + " alert(document.all.tags('input').item(1).name);\n"
1477            + " // Make sure tags() returns an empty element array if there are no matches.\n"
1478            + " alert(document.all.tags('xxx').length);\n"
1479            + "}\n"
1480            + "</script></head><body onload='doTest()'>"
1481            + "<input type='text' name='a' value='1'>"
1482            + "<input type='text' name='b' value='1'>"
1483            + "</body></html>";
1484
1485        final List collectedAlerts = new ArrayList();
1486        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1487        assertEquals( "First", firstPage.getTitleText() );
1488
1489        final List expectedAlerts = Arrays.asList( new String[] {
1490            "a", "b", "a", "b", "0"} );
1491        assertEquals( expectedAlerts, collectedAlerts );
1492    }
1493
1494    /**
1495     * Makes sure that the document.all collection contents are not cached if the
1496     * collection is accessed before the page has finished loading.
1497     * @throws Exception If the test fails.
1498     */

1499    public void testDocumentAll_Caching() throws Exception {
1500        final String firstContent
1501            = "<html><head><title>Test</title></head>"
1502            + "<body onload='alert(document.all.b.value)'>"
1503            + "<input type='text' name='a' value='1'>"
1504            + "<script>alert(document.all.a.value)</script>"
1505            + "<input type='text' name='b' value='2'>"
1506            + "</body></html>";
1507
1508        final List collectedAlerts = new ArrayList();
1509        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1510        assertEquals( "Test", firstPage.getTitleText() );
1511
1512        final List expectedAlerts = Arrays.asList( new String[]{"1", "2"} );
1513        assertEquals( expectedAlerts, collectedAlerts );
1514    }
1515
1516    /**
1517     * @throws Exception if the test fails
1518     */

1519    public void testCookie_read() throws Exception {
1520        final WebClient webClient = new WebClient();
1521        final MockWebConnection webConnection = new MockWebConnection( webClient );
1522
1523        final String firstContent
1524            = "<html><head><title>First</title><script>"
1525            + "function doTest() {\n"
1526            + " var cookieSet = document.cookie.split(';');\n"
1527            + " var setSize = cookieSet.length;\n"
1528            + " var crumbs;\n"
1529            + " var x=0;\n"
1530            + " for (x=0;((x<setSize)); x++) {\n"
1531            + " crumbs = cookieSet[x].split('=');\n"
1532            + " alert ( crumbs[0] );\n"
1533            + " alert ( crumbs[1] );\n"
1534            + " } \n"
1535            + "}\n"
1536            + "</script></head><body onload='doTest()'>"
1537            + "</body></html>";
1538
1539        final URL url = URL_FIRST;
1540        webConnection.setResponse(url, firstContent);
1541        webClient.setWebConnection( webConnection );
1542
1543        final HttpState state = webConnection.getStateForUrl(url);
1544        state.addCookie( new Cookie("first", "one", "two") );
1545        state.addCookie( new Cookie("first", "three", "four") );
1546
1547        final List collectedAlerts = new ArrayList();
1548        webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
1549
1550        final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
1551        assertEquals( "First", firstPage.getTitleText() );
1552
1553        final List expectedAlerts = Arrays.asList( new String[] {
1554            "one", "two", "three", "four"} );
1555        assertEquals( expectedAlerts, collectedAlerts );
1556    }
1557
1558    /**
1559     * @throws Exception if the test fails
1560     */

1561    public void testGetElementsByName() throws Exception {
1562        final String firstContent
1563            = "<html><head><title>First</title><script>"
1564            + "function doTest() {\n"
1565            + " var elements = document.getElementsByName('name1');\n"
1566            + " for( i=0; i<elements.length; i++ ) {\n"
1567            + " alert(elements[i].value);\n"
1568            + " }\n"
1569            + "}\n"
1570            + "</script></head><body onload='doTest()'>"
1571            + "<form>"
1572            + "<input type='radio' name='name1' value='value1'>"
1573            + "<input type='radio' name='name1' value='value2'>"
1574            + "<input type='button' name='name2' value='value3'>"
1575            + "</form>"
1576            + "</body></html>";
1577
1578        final List collectedAlerts = new ArrayList();
1579        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1580        assertEquals( "First", firstPage.getTitleText() );
1581
1582        final List expectedAlerts = Arrays.asList(new String[] { "value1", "value2" });
1583        assertEquals(expectedAlerts, collectedAlerts);
1584    }
1585    
1586    /**
1587     * @throws Exception if the test fails
1588     */

1589    public void testDocumentBody_read() throws Exception {
1590
1591        final String firstContent = "<html><head><title>First</title></head>"
1592            + "<body id='IAmTheBody' onload='alert(document.body.id)'>"
1593            + "</body></html>";
1594
1595        final List collectedAlerts = new ArrayList();
1596
1597        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1598        assertEquals("First", firstPage.getTitleText());
1599
1600        final List expectedAlerts = Collections.singletonList("IAmTheBody");
1601        assertEquals(expectedAlerts, collectedAlerts);
1602    }
1603    
1604    /**
1605     * Test the access to the images value. This should return the 2 images in the document
1606     * @throws Exception if the test fails
1607     */

1608    public void testGetImages() throws Exception {
1609        final String firstContent
1610            = "<html><head><title>First</title><script>"
1611            + "function doTest() {\n"
1612            + " alert(document.images.length);\n"
1613            + " alert(allImages.length);\n"
1614            + " alert(document.images == allImages);\n"
1615            + "}\n"
1616            + "</script></head><body onload='doTest()'>"
1617            + "<img SRC='firstImage'>"
1618            + "<script>"
1619            + "var allImages = document.images;\n"
1620            + "alert(allImages.length);\n"
1621            + "</script>"
1622            + "<form>"
1623            + "<img SRC='2ndImage'>"
1624            + "</form>"
1625            + "</body></html>";
1626
1627        final List collectedAlerts = new ArrayList();
1628        final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
1629        assertEquals( "First", firstPage.getTitleText() );
1630
1631        final List expectedAlerts = Arrays.asList(new String[] { "1", "2", "2", "true" });
1632        createTestPageForRealBrowserIfNeeded(firstContent, expectedAlerts);
1633        assertEquals(expectedAlerts, collectedAlerts);
1634    }
1635    
1636    /**
1637     * Test setting and reading the title for an existing title
1638     *
1639     * @throws Exception if the test fails
1640     */

1641    public void testSettingTitle() throws Exception {
1642        final String content
1643            = "<html><head><title>Bad Title</title></head>\n"
1644            + "<body>\n"
1645            + "<script>\n"
1646            + " document.title = 'correct title';\n"
1647            + " alert(document.title)\n"
1648            + "</script>\n"
1649            + "</body></html>";
1650 
1651        final List collectedAlerts = new ArrayList();
1652        final HtmlPage page = loadPage(content, collectedAlerts);
1653        assertEquals("correct title", page.getTitleText());
1654
1655        final List expectedAlerts = Arrays.asList( new String[]{
1656            "correct title"
1657        } );
1658
1659        assertEquals("Test the alert", expectedAlerts, collectedAlerts );
1660    }
1661
1662    /**
1663     * Test setting and reading the title for when the is not in
1664     * the page to begin
1665     *
1666     * @throws Exception if the test fails
1667     */

1668    public void testSettingMissingTitle() throws Exception {
1669        final String content = "<html><head></head>\n"
1670            + "<body>\n"
1671            + "<script>\n"
1672            + " document.title = 'correct title';\n"
1673            + " alert(document.title)\n"
1674            + "</script>\n"
1675            + "</body></html>";
1676
1677        final List collectedAlerts = new ArrayList();
1678        loadPage(content, collectedAlerts);
1679        final List expectedAlerts = Arrays.asList( new String[]{ "correct title" } );
1680
1681        assertEquals("Test the alert", expectedAlerts, collectedAlerts );
1682    }
1683    
1684    /**
1685     * Test setting and reading the title for when the is not in
1686     * the page to begin
1687     *
1688     * @throws Exception if the test fails
1689     */

1690    public void testSettingBlankTitle() throws Exception {
1691        final String content = "<html><head><title></title></head>\n"
1692            + "<body>\n"
1693            + "<script>\n"
1694            + " document.title = 'correct title';\n"
1695            + " alert(document.title)\n"
1696            + "</script>\n"
1697            + "</body></html>";
1698 
1699        final List collectedAlerts = new ArrayList();
1700        final HtmlPage page = loadPage(content, collectedAlerts);
1701        assertEquals("correct title", page.getTitleText());
1702
1703        final List expectedAlerts = Arrays.asList( new String[]{
1704            "correct title"
1705        } );
1706
1707        assertEquals("Test the alert", expectedAlerts, collectedAlerts );
1708    }
1709
1710    /**
1711     * @throws Exception if the test fails
1712     */

1713    public void testTitle() throws Exception {
1714        final String content = "<html><head><title>foo</title><script>\n"
1715            + "function doTest(){\n"
1716            + " alert(document.title)\n"
1717            + "}\n"
1718            + "</script></head>\n"
1719            + "<body onload='doTest()'>\n"
1720            + "</body></html>";
1721
1722        final List collectedAlerts = new ArrayList();
1723        final HtmlPage page = loadPage(content, collectedAlerts);
1724        assertEquals("foo", page.getTitleText());
1725
1726        final List expectedAlerts = Arrays.asList( new String[]{
1727            "foo"
1728        } );
1729
1730        assertEquals( expectedAlerts, collectedAlerts );
1731    }
1732    
1733    /**
1734     * Test the ReadyState. This should only work in IE.
1735     * Currently locked out since the browser type of code is not working.
1736     *
1737     * @throws Exception if the test fails
1738     */

1739    public void testReadyStateNonIE() throws Exception {
1740        if (true) {
1741            notImplemented();
1742            return;
1743        }
1744        final String content = "<html><head>\n"
1745            + "<script>\n"
1746            + "function testIt() {\n"
1747            + " alert(document.readyState);\n"
1748            + "}\n"
1749            + "alert(document.readyState);\n"
1750            + "</script>\n"
1751            + "</head>\n"
1752            + "<body onLoad='testIt()'></body></html>\n";
1753
1754        final List collectedAlerts = new ArrayList();
1755        loadPage(content, collectedAlerts);
1756
1757        final List expectedAlerts = Arrays.asList( new String[]{
1758            "undefined", "undefined"
1759        } );
1760
1761        assertEquals( expectedAlerts, collectedAlerts );
1762    }
1763    
1764    /**
1765     * Test the ReadyState. This should only work in IE
1766     *
1767     * @throws Exception if the test fails
1768     */

1769    public void testReadyStateIE() throws Exception {
1770        final WebClient client = new WebClient(BrowserVersion.INTERNET_EXPLORER_6_0);
1771        final MockWebConnection webConnection = new MockWebConnection( client );
1772        final String content = "<html><head>\n"
1773            + "<script>\n"
1774            + "function testIt() {\n"
1775            + " alert(document.readyState);\n"
1776            + "}\n"
1777            + "alert(document.readyState);\n"
1778            + "</script>\n"
1779            + "</head>\n"
1780            + "<body onLoad='testIt()'></body></html>\n";
1781        webConnection.setResponse(URL_FIRST, content);
1782        client.setWebConnection( webConnection );
1783
1784        final List collectedAlerts = new ArrayList();
1785        client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
1786        client.getPage(URL_FIRST);
1787
1788        final List expectedAlerts = Arrays.asList( new String[]{
1789            "loading", "complete"
1790        } );
1791
1792        assertEquals( expectedAlerts, collectedAlerts );
1793    }
1794    
1795    /**
1796     * Calling document.body before the page is fully loaded used to cause an exception.
1797     * @throws Exception if the test fails
1798     */

1799    public void testDocumentWithNoBody() throws Exception {
1800        final String content
1801            = "<html><head><title>foo</title><script>\n"
1802            + "alert(document.body)\n"
1803            + "</script></head><body></body></html>";
1804
1805        final List collectedAlerts = new ArrayList();
1806        loadPage(content, collectedAlerts);
1807
1808        final List expectedAlerts = Collections.singletonList("undefined");
1809        assertEquals( expectedAlerts, collectedAlerts );
1810    }
1811
1812    /**
1813     * IE has a bug which returns the element by name if it can not find it by ID.
1814     * @throws Exception if the test fails
1815     */

1816    public void testGetElementByIdForIE() throws Exception {
1817        final String content
1818            = "<html><head><title>foo</title></head>"
1819            + "<body>\n"
1820            + "<input type='text' name='findMe'>\n"
1821            + "<input type='text' id='findMe2' name='byId'>\n"
1822            + "<script>\n"
1823            + "alert(document.getElementById('findMe').name)\n"
1824            + "alert(document.getElementById('findMe2').name)\n"
1825            + "</script></body></html>";
1826
1827        final List collectedAlerts = new ArrayList();
1828        loadPage(content, collectedAlerts);
1829
1830        final List expectedAlerts = Arrays.asList( new String[]{
1831            "findMe", "byId"
1832        } );
1833        assertEquals( expectedAlerts, collectedAlerts );
1834    }
1835    
1836    /**
1837     * IE has a bug which returns the element by name if it can not find it by ID.
1838     * @throws Exception if the test fails
1839     */

1840    public void testGetElementByIdForNetscape() throws Exception {
1841        final WebClient client = new WebClient(BrowserVersion.NETSCAPE_6_2_3);
1842        final MockWebConnection webConnection = new MockWebConnection( client );
1843        final String content
1844            = "<html><head><title>foo</title></head>"
1845            + "<body>\n"
1846            + "<input type='text' name='findMe'>\n"
1847            + "<input type='text' id='findMe2' name='byId'>\n"
1848            + "<script>\n"
1849            + "alert(document.getElementById('findMe'))\n"
1850            + "alert(document.getElementById('findMe2').name)\n"
1851            + "</script></body></html>";
1852
1853        webConnection.setResponse(URL_FIRST, content);
1854        client.setWebConnection( webConnection );
1855        final List collectedAlerts = new ArrayList();
1856        client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
1857
1858        client.getPage(URL_FIRST);
1859
1860        final List expectedAlerts = Arrays.asList( new String[]{
1861            "null", "byId"
1862        } );
1863        assertEquals( expectedAlerts, collectedAlerts );
1864    }
1865    
1866    /**
1867     * @throws Exception if the test fails
1868     */

1869    public void testBuildCookie() throws Exception {
1870        checkCookie(Document.buildCookie("toto=foo", URL_FIRST), "toto", "foo", "", "first", false);
1871        checkCookie(Document.buildCookie("toto=", URL_FIRST), "toto", "", "", "first", false);
1872        checkCookie(Document.buildCookie("toto=foo;secure", URL_FIRST), "toto", "foo", "", "first", true);
1873        checkCookie(Document.buildCookie("toto=foo;path=/myPath;secure", URL_FIRST),
1874                "toto", "foo", "/myPath", "first", true);
1875    }
1876    
1877    private void checkCookie(final Cookie cookie, final String name, final String value,
1878            final String path, final String domain, final boolean secure) {
1879        assertEquals(name, cookie.getName());
1880        assertEquals(value, cookie.getValue());
1881        assertNull(cookie.getComment());
1882        assertEquals(path, cookie.getPath());
1883        assertEquals(domain, cookie.getDomain());
1884        assertEquals(secure, cookie.getSecure());
1885    }
1886
1887    /**
1888     * Test that img and form can be retrieved directly by name but not
1889     * a, input or button
1890     * @throws Exception if the test fails
1891     */

1892    public void testDirectAccessByName() throws Exception {
1893        final String content = "<html><head><title>foo</title><script>\n"
1894            + "function doTest(){\n"
1895            + " alert(document.myImage.id);\n"
1896            + " alert(document.myImage2.length);\n"
1897            + " alert(document.myForm.tagName);\n"
1898            + " alert(document.myAnchor);\n"
1899            + " alert(document.myInput);\n"
1900            + " alert(document.myInputImage);\n"
1901            + " alert(document.myButton);\n"
1902            + "}\n"
1903            + "</script></head>\n"
1904            + "<body onload='doTest()'>\n"
1905            + "<img SRC='foo' name='myImage' id='myImageId'>\n"
1906            + "<img SRC='foo' name='myImage2'>\n"
1907            + "<img SRC='foo' name='myImage2'>\n"
1908            + "<a name='myAnchor'/>\n"
1909            + "<form name='myForm'>\n"
1910            + "<input name='myInput' type='text'>\n"
1911            + "<input name='myInputImage' type='image' SRC='foo'>\n"
1912            + "<button name='myButton'>foo</button>\n"
1913            + "</form>\n"
1914            + "</body></html>";
1915
1916        final List expectedAlerts = Arrays.asList( new String[]{
1917            "myImageId", "2", "FORM", "undefined", "undefined", "undefined", "undefined"
1918        } );
1919        createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
1920
1921        final List collectedAlerts = new ArrayList();
1922        final HtmlPage page = loadPage(content, collectedAlerts);
1923        assertEquals("foo", page.getTitleText());
1924        assertEquals( expectedAlerts, collectedAlerts );
1925    }
1926
1927    /**
1928     * @throws Exception if the test fails
1929     */

1930    public void testWriteInManyTimes() throws Exception {
1931        final String content = "<html><head><title>foo</title><script>\n"
1932            + "function doTest(){\n"
1933            + " alert(document.getElementById('inner').parentNode.id);\n"
1934            + "}\n"
1935            + "</script></head>\n"
1936            + "<body onload='doTest()'>\n"
1937            + "<script>\n"
1938            + "document.write('<div id=\"outer\">');"
1939            + "document.write('<div id=\"inner\"/>');"
1940            + "document.write('</div>');"
1941            + "</script>\n"
1942            + "</body></html>";
1943
1944        final List expectedAlerts = Arrays.asList( new String[]{
1945            "outer"
1946        } );
1947        createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
1948
1949        final List collectedAlerts = new ArrayList();
1950        loadPage(content, collectedAlerts);
1951        assertEquals( expectedAlerts, collectedAlerts );
1952    }
1953
1954    /**
1955     * @throws Exception if the test fails
1956     */

1957    public void testWriteScriptInManyTimes() throws Exception {
1958        final String content = "<html><head><title>foo</title>\n"
1959            + "<script>\n"
1960            + "document.write('<script SRC=\"script.js\">');"
1961            + "document.write('<' + '/script>');"
1962            + "</script>\n"
1963            + "</head>\n"
1964            + "<body>\n"
1965            + "</body></html>";
1966
1967        final List expectedAlerts = Arrays.asList( new String[]{
1968            "foo"
1969        } );
1970        createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
1971
1972        final URL scriptUrl = new URL(URL_FIRST.toExternalForm() + "/script.js");
1973        final WebClient client = new WebClient();
1974        final MockWebConnection webConnection = new MockWebConnection(client);
1975        client.setWebConnection(webConnection);
1976        webConnection.setDefaultResponse(content);
1977        webConnection.setResponse(scriptUrl, "alert('foo');", "text/javascript");
1978
1979        final List collectedAlerts = new ArrayList();
1980        client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
1981        client.getPage(URL_FIRST);
1982
1983        assertEquals( expectedAlerts, collectedAlerts );
1984    }
1985
1986    /**
1987     * @throws Exception if the test fails
1988     */

1989    public void testDomain() throws Exception {
1990        final String content = "<html><head><title>foo</title><script>\n"
1991            + "function doTest(){\n"
1992            + " alert(document.domain);\n"
1993            + " document.domain = 'gargoylesoftware.com';\n"
1994            + " alert(document.domain);\n"
1995            + "}\n"
1996            + "</script></head>\n"
1997            + "<body onload='doTest()'>\n"
1998            + "</body></html>";
1999
2000        final List expectedAlerts = Arrays.asList( new String[]{
2001            "www.gargoylesoftware.com", "gargoylesoftware.com"
2002        } );
2003
2004        final List collectedAlerts = new ArrayList();
2005        loadPage(content, collectedAlerts);
2006        assertEquals( expectedAlerts, collectedAlerts );
2007    }
2008
2009  /**
2010    * @throws Exception if the test fails
2011    */

2012    public void testDomainMixedCaseNetscape() throws Exception {
2013
2014        final URL urlGargoyleUpperCase = new URL("http://WWW.GARGOYLESOFTWARE.COM");
2015
2016        final WebClient client = new WebClient(BrowserVersion.NETSCAPE_6_2_3);
2017        final MockWebConnection webConnection = new MockWebConnection( client );
2018
2019        final String content = "<html><head><title>foo</title><script>\n"
2020            + "function doTest(){\n"
2021            + " alert(document.domain);\n"
2022            + " document.domain = 'GaRgOyLeSoFtWaRe.CoM';\n"
2023            + " alert(document.domain);\n"
2024            + "}\n"
2025            + "</script></head>\n"
2026            + "<body onload='doTest()'>\n"
2027            + "</body></html>";
2028
2029
2030        webConnection.setResponse(urlGargoyleUpperCase, content);
2031        client.setWebConnection( webConnection );
2032        final List collectedAlerts = new ArrayList();
2033        client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
2034
2035        client.getPage(urlGargoyleUpperCase);
2036
2037
2038
2039        final List expectedAlerts = Arrays.asList(new String[] {
2040            "www.gargoylesoftware.com", "gargoylesoftware.com"
2041        });
2042        assertEquals( expectedAlerts, collectedAlerts );
2043    }
2044
2045  /**
2046    * @throws Exception if the test fails
2047    */

2048    public void testDomainMixedCase() throws Exception {
2049        final String content = "<html><head><title>foo</title><script>\n"
2050            + "function doTest(){\n"
2051            + " alert(document.domain);\n"
2052            + " document.domain = 'GaRgOyLeSoFtWaRe.CoM';\n"
2053            + " alert(document.domain);\n"
2054            + "}\n"
2055            + "</script></head>\n"
2056            + "<body onload='doTest()'>\n"
2057            + "</body></html>";
2058
2059        final List collectedAlerts = new ArrayList();
2060        loadPage(content, collectedAlerts);
2061        final List expectedAlerts = Arrays.asList(new String[] {
2062            "www.gargoylesoftware.com", "GaRgOyLeSoFtWaRe.CoM"
2063        });
2064        assertEquals( expectedAlerts, collectedAlerts );
2065    }
2066
2067
2068      /**
2069     * @throws Exception if the test fails
2070     */

2071    public void testDomainLong() throws Exception {
2072        final String content = "<html><head><title>foo</title><script>\n"
2073            + "function doTest(){\n"
2074            + " alert(document.domain);\n"
2075            + " document.domain = 'd4.d3.d2.d1.gargoylesoftware.com';\n"
2076            + " alert(document.domain);\n"
2077            + " document.domain = 'd1.gargoylesoftware.com';\n"
2078            + " alert(document.domain);\n"
2079            + "}\n"
2080            + "</script></head>\n"
2081            + "<body onload='doTest()'>\n"
2082            + "</body></html>";
2083
2084        final List expectedAlerts = Arrays.asList( new String[]{
2085            "d4.d3.d2.d1.gargoylesoftware.com", "d4.d3.d2.d1.gargoylesoftware.com","d1.gargoylesoftware.com"
2086        } );
2087
2088        final List collectedAlerts = new ArrayList();
2089        loadPage(content, collectedAlerts,new URL("http://d4.d3.d2.d1.gargoylesoftware.com"));
2090        assertEquals( expectedAlerts, collectedAlerts );
2091    }
2092
2093    /**
2094     * @throws Exception if the test fails
2095     */

2096    public void testDomainSetSelf() throws Exception {
2097        final String content = "<html><head><title>foo</title><script>\n"
2098            + "function doTest(){\n"
2099            + " alert(document.domain);\n"
2100            + " document.domain = 'localhost';\n"
2101            + " alert(document.domain);\n"
2102            + "}\n"
2103            + "</script></head>\n"
2104            + "<body onload='doTest()'>\n"
2105            + "</body></html>";
2106
2107        final List expectedAlerts = Arrays.asList( new String[]{
2108            "localhost", "localhost"
2109        } );
2110
2111        final List collectedAlerts = new ArrayList();
2112        loadPage(content, collectedAlerts,new URL("http://localhost"));
2113        assertEquals( expectedAlerts, collectedAlerts );
2114    }
2115
2116    /**
2117     * @throws Exception if the test fails
2118     */

2119    public void testDomainTooShort() throws Exception {
2120        final String content = "<html><head><title>foo</title><script>\n"
2121            + "function doTest(){\n"
2122            + " alert(document.domain);\n"
2123            + " document.domain = 'com';\n"
2124            + " alert(document.domain);\n"
2125            + "}\n"
2126            + "</script></head>\n"
2127            + "<body onload='doTest()'>\n"
2128            + "</body></html>";
2129
2130        final List collectedAlerts = new ArrayList();
2131        try {
2132            loadPage(content, collectedAlerts);
2133        }
2134        catch (final ScriptException ex) {
2135            return;
2136        }
2137        fail();
2138    }
2139    
2140    /**
2141     * @throws Exception if the test fails
2142     */

2143    public void testDocumentWriteFrameRelativeURLMultipleFrameset() throws Exception {
2144        final String framesetContent = "<html><head><title>frameset</title></head>\n"
2145            + "<script>\n"
2146            + " document.write('<frameset><frame SRC=\"frame.html\"/></frameset>');\n"
2147            + "</script>\n"
2148            + "<frameset><frame SRC='blank.html'/></frameset>"
2149            + "</html>";
2150         
2151        final URL baseURL = new URL("http://base/subdir/");
2152        final URL framesetURL = new URL(baseURL.toExternalForm() + "test.html");
2153        final URL frameURL = new URL(baseURL.toExternalForm() + "frame.html");
2154        final URL blankURL = new URL(baseURL.toExternalForm() + "blank.html");
2155
2156        final WebClient client = new WebClient();
2157        final MockWebConnection webConnection = new MockWebConnection( client );
2158        webConnection.setResponse(framesetURL, framesetContent);
2159        webConnection.setResponseAsGenericHtml(frameURL, "frame");
2160        webConnection.setResponseAsGenericHtml(blankURL, "blank");
2161        client.setWebConnection( webConnection );
2162         
2163        final HtmlPage framesetPage = (HtmlPage) client.getPage(framesetURL);
2164        final FrameWindow frame = (FrameWindow) framesetPage.getFrames().get(0);
2165         
2166        assertNotNull(frame);
2167        assertEquals(frameURL.toExternalForm(), frame.getEnclosedPage().getWebResponse().getUrl().toExternalForm());
2168        assertEquals("frame", ((HtmlPage)frame.getEnclosedPage()).getTitleText());
2169    }
2170     
2171     /**
2172     * @throws Exception if the test fails
2173     */

2174    public void testScriptsArray() throws Exception {
2175        final String htmlContent = "<html><head><script lang='JavaScript'>"
2176            + " function doTest(){"
2177            + " alert(document.scripts.length);" // This line used to blow up
2178
+ "}"
2179            + "</script></head><body onload='doTest();'>"
2180            + "<script>var scriptTwo = 1;</script>"
2181            + "</body></html> ";
2182
2183        final List collectedAlerts = new ArrayList();
2184        loadPage(htmlContent, collectedAlerts);
2185        assertEquals(Collections.singletonList("2"), collectedAlerts);
2186    }
2187    
2188    /**
2189     * document.XXX should first look at elements named XXX before using standard functions.
2190     * @throws Exception if the test fails
2191     */

2192    public void testPrecedence() throws Exception {
2193        final String htmlContent = "<html><head></head>"
2194            + "<body>"
2195            + "<form name='writeln'>foo</form>"
2196            + "<script>alert(document.writeln.tagName);</script>"
2197            + "</body></html>";
2198        final List expectedAlerts = Arrays.asList(new String[] {"FORM"});
2199        createTestPageForRealBrowserIfNeeded(htmlContent, expectedAlerts);
2200
2201        final List collectedAlerts = new ArrayList();
2202        loadPage(htmlContent, collectedAlerts);
2203        assertEquals(expectedAlerts, collectedAlerts);
2204    }
2205}
2206
Popular Tags