KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.List;
44
45 import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
46 import com.gargoylesoftware.htmlunit.KeyValuePair;
47 import com.gargoylesoftware.htmlunit.MockWebConnection;
48 import com.gargoylesoftware.htmlunit.WebClient;
49 import com.gargoylesoftware.htmlunit.WebTestCase;
50 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
51 import com.gargoylesoftware.htmlunit.html.HtmlButton;
52 import com.gargoylesoftware.htmlunit.html.HtmlForm;
53 import com.gargoylesoftware.htmlunit.html.HtmlPage;
54 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
55
56 /**
57  * Tests for Form
58  *
59  * @version $Revision: 1.23 $
60  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
61  * @author David K. Taylor
62  * @author Marc Guillemot
63  * @author Chris Erskine
64  */

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

70     public FormTest( final String name ) {
71         super(name);
72     }
73
74
75     /**
76      * @throws Exception if the test fails
77      */

78     public void testElementsAccessor() throws Exception {
79         final String content
80             = "<html><head><title>foo</title><script>\n"
81             + "function doTest(){\n"
82             + " alert(document.form1.length)\n"
83             + " for( var i=0; i< document.form1.length; i++) {\n"
84             + " var element = document.form1.elements[i];"
85             + " if( element.type != 'radio' && element != document.form1[element.name] ) {\n"
86             + " alert('name index not working for '+element.name);\n"
87             + " }\n"
88             + " alert( element.name )\n"
89             + " }\n"
90             + "}\n"
91             + "</script></head><body onload='doTest()'>\n"
92             + "<p>hello world</p>"
93             + "<form name='form1'>"
94             + " <input type='button' name='button1' />"
95             + " <button type='button' name='button2' />"
96             + " <input type='checkbox' name='checkbox1' />"
97             + " <input type='file' name='fileupload1' />"
98             + " <input type='hidden' name='hidden1' />"
99             + " <input type='radio' name='radio1' value='1' />"
100             + " <input type='radio' name='radio1' value='2' />"
101             + " <select name='select1'>"
102             + " <option>foo</option>"
103             + " </select>"
104             + " <select multiple='multiple' name='select2'>"
105             + " <option>foo</option>"
106             + " </select>"
107             + " <input type='password' name='password1' />"
108             + " <input type='reset' name='reset1' />"
109             + " <button type='reset' name='reset2' />"
110             + " <input type='submit' name='submit1' />"
111             + " <button type='submit' name='submit2' />"
112             + " <input type='text' name='textInput1' />"
113             + " <textarea name='textarea1'>foo</textarea>"
114             + "</form>"
115             + "</body></html>";
116
117         final List collectedAlerts = new ArrayList();
118         final HtmlPage page = loadPage(content, collectedAlerts);
119         assertEquals("foo", page.getTitleText());
120
121         final List expectedAlerts = Arrays.asList( new String[]{
122             "16", "button1", "button2", "checkbox1", "fileupload1", "hidden1",
123             "radio1", "radio1",
124             "select1", "select2", "password1", "reset1",
125             "reset2", "submit1", "submit2", "textInput1", "textarea1"
126         } );
127
128         assertEquals( expectedAlerts, collectedAlerts );
129     }
130
131
132     /**
133      * @throws Exception if the test fails
134      */

135     public void testRadioButtonArray() throws Exception {
136         final String content
137             = "<html><head><title>foo</title><script>\n"
138             + "function doTest(){\n"
139             + " var radioArray = document.form1['radio1'];"
140             + " alert(radioArray.length)\n"
141             + " for( var i=0; i< radioArray.length; i++) {\n"
142             + " var element = radioArray[i];"
143             + " alert( element.value )\n"
144             + " }\n"
145             + "}\n"
146             + "</script></head><body onload='doTest()'>\n"
147             + "<p>hello world</p>"
148             + "<form name='form1'>"
149             + " <input type='radio' name='radio1' value='1'/>"
150             + " <input type='radio' name='radio1' value='2'/>"
151             + " <input type='radio' name='radio1' value='3'/>"
152             + "</form>"
153             + "</body></html>";
154
155         final List collectedAlerts = new ArrayList();
156
157         final List expectedAlerts = Arrays.asList( new String[]{
158             "3", "1", "2", "3"
159         } );
160
161         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
162         final HtmlPage page = loadPage(content, collectedAlerts);
163
164         assertEquals("foo", page.getTitleText());
165         assertEquals( expectedAlerts, collectedAlerts );
166     }
167
168
169     /**
170      * If there is only one radio button with a specified name then that radio
171      * button will be returned for the name, not an array of radio buttons. Test
172      * this.
173      * @throws Exception if the test fails
174      */

175     public void testRadioButton_OnlyOne() throws Exception {
176         final String content
177             = "<html><head><title>foo</title><script>\n"
178             + "function doTest(){\n"
179             + " alert(document.form1['radio1'].value);"
180             + "}\n"
181             + "</script></head><body onload='doTest()'>\n"
182             + "<p>hello world</p>"
183             + "<form name='form1'>"
184             + " <input type='radio' name='radio1' value='1'/>"
185             + "</form>"
186             + "</body></html>";
187
188         final List collectedAlerts = new ArrayList();
189         final HtmlPage page = loadPage(content, collectedAlerts);
190         assertEquals("foo", page.getTitleText());
191
192         final List expectedAlerts = Arrays.asList( new String[]{
193             "1"
194         } );
195
196         assertEquals( expectedAlerts, collectedAlerts );
197     }
198
199
200     /**
201      * @throws Exception if the test fails
202      */

203     public void testActionProperty() throws Exception {
204         final String jsProperty = "action";
205         final String htmlProperty = "action";
206         final String oldValue = "http://foo.com";
207         final String newValue = "mailto:me@bar.com";
208
209         final HtmlForm form = doTestProperty( jsProperty, htmlProperty, oldValue, newValue );
210         assertEquals( newValue, form.getActionAttribute() );
211     }
212
213
214     /**
215      * @throws Exception if the test fails
216      */

217     public void testEncodingProperty() throws Exception {
218         final String jsProperty = "encoding";
219         final String htmlProperty = "enctype";
220         final String oldValue = "myEncoding";
221         final String newValue = "newEncoding";
222
223         final HtmlForm form = doTestProperty( jsProperty, htmlProperty, oldValue, newValue );
224         assertEquals( newValue, form.getEnctypeAttribute() );
225     }
226
227
228     /**
229      * @throws Exception if the test fails
230      */

231     public void testMethodProperty() throws Exception {
232         final String jsProperty = "method";
233         final String htmlProperty = "method";
234         final String oldValue = "get";
235         final String newValue = "post";
236
237         final HtmlForm form = doTestProperty( jsProperty, htmlProperty, oldValue, newValue );
238         assertEquals( newValue, form.getMethodAttribute() );
239     }
240
241
242     /**
243      * @throws Exception if the test fails
244      */

245     public void testTargetProperty() throws Exception {
246         final String jsProperty = "target";
247         final String htmlProperty = "target";
248         final String oldValue = "_top";
249         final String newValue = "_parent";
250
251         final HtmlForm form = doTestProperty( jsProperty, htmlProperty, oldValue, newValue );
252         assertEquals( newValue, form.getTargetAttribute() );
253     }
254
255
256     private HtmlForm doTestProperty(
257             final String jsProperty,
258             final String htmlProperty,
259             final String oldValue,
260             final String newValue )
261         throws
262             Exception {
263
264         final String content
265             = "<html><head><title>foo</title><script>\n"
266             + "function doTest(){\n"
267             + " alert(document.form1."+jsProperty+");\n"
268             + " document.form1."+jsProperty+"='"+newValue+"'\n"
269             + " alert(document.form1."+jsProperty+");\n"
270             + "}\n"
271             + "</script></head><body onload='doTest()'>\n"
272             + "<p>hello world</p>"
273             + "<form name='form1' "+htmlProperty+"='"+oldValue+"'>"
274             + " <input type='button' name='button1' />"
275             + "</form>"
276             + "</body></html>";
277
278         final List collectedAlerts = new ArrayList();
279         final HtmlPage page = loadPage(content, collectedAlerts);
280
281         final List expectedAlerts = Arrays.asList( new String[]{
282             oldValue, newValue
283         } );
284
285         assertEquals( expectedAlerts, collectedAlerts );
286         return page.getFormByName("form1");
287     }
288
289
290     /**
291      * @throws Exception if the test fails
292      */

293     public void testFormSubmit() throws Exception {
294         final WebClient client = new WebClient();
295         final MockWebConnection webConnection = new MockWebConnection( client );
296
297         final String firstContent
298             = "<html><head><title>first</title></head><body>\n"
299             + "<p>hello world</p>"
300             + "<form name='form1' method='get' action='http://second'>"
301             + " <input type='button' name='button1' />"
302             + " <input type='button' name='button2' />"
303             + "</form>"
304             + "</body></html>";
305         final String secondContent
306             = "<html><head><title>second</title></head><body>\n"
307             + "<p>hello world</p>"
308             + "</body></html>";
309
310         webConnection.setResponse(
311             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
312         webConnection.setResponse(
313             URL_SECOND, secondContent, 200, "OK", "text/html",
314             Collections.EMPTY_LIST );
315         client.setWebConnection( webConnection );
316
317         final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST);
318         assertEquals( "first", page.getTitleText() );
319
320         final HtmlPage secondPage
321             = (HtmlPage)page.executeJavaScriptIfPossible("document.form1.submit()", "test", true, null).getNewPage();
322         assertEquals( "second", secondPage.getTitleText() );
323     }
324
325
326     /**
327      * @throws Exception if the test fails
328      */

329     public void testFormSubmit_target() throws Exception {
330         final WebClient client = new WebClient();
331         final MockWebConnection webConnection = new MockWebConnection( client );
332
333         final String firstContent
334             = "<html><head><title>first</title></head><body>\n"
335             + "<p>hello world</p>"
336             + "<form name='form1' method='get' action='http://second' target='MyNewWindow'>"
337             + " <input type='button' name='button1' />"
338             + "</form>"
339             + "</body></html>";
340         final String secondContent
341             = "<html><head><title>second</title></head><body>\n"
342             + "<p>hello world</p>"
343             + "</body></html>";
344
345         webConnection.setResponse(
346             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
347         webConnection.setResponse(
348             URL_SECOND, secondContent, 200, "OK", "text/html",
349             Collections.EMPTY_LIST );
350         client.setWebConnection( webConnection );
351
352         final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST);
353         assertEquals( "first", page.getTitleText() );
354
355         final HtmlPage secondPage
356             = (HtmlPage)page.executeJavaScriptIfPossible("document.form1.submit()", "test", true, null).getNewPage();
357         assertEquals( "second", secondPage.getTitleText() );
358         assertEquals( "MyNewWindow", secondPage.getEnclosingWindow().getName() );
359     }
360
361     
362     /**
363      * @throws Exception if the test fails
364      */

365     public void testFormSubmitDoesntCallOnSubmit() throws Exception {
366         final WebClient client = new WebClient();
367         final MockWebConnection webConnection = new MockWebConnection( client );
368
369         final String firstContent
370             = "<html><head><title>first</title></head><body>\n"
371             + "<form name='form1' method='get' action='http://second' onsubmit=\"alert('onsubmit called')\">"
372             + " <input type='submit' />"
373             + "</form>"
374             + "<a HREF='javascript:document.form1.submit()' id='link1'>Click me</a>"
375             + "</body></html>";
376         final String secondContent
377             = "<html><head><title>second</title></head><body>\n"
378             + "<p>hello world</p>"
379             + "</body></html>";
380
381         webConnection.setResponse(
382             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
383         webConnection.setResponse(
384             URL_SECOND, secondContent, 200, "OK", "text/html",
385             Collections.EMPTY_LIST );
386         client.setWebConnection( webConnection );
387
388         final List collectedAlerts = new ArrayList();
389         client.setAlertHandler( new CollectingAlertHandler( collectedAlerts ) );
390
391         final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST);
392         final HtmlAnchor link = (HtmlAnchor)page.getHtmlElementById("link1");
393         link.click();
394
395         assertEquals( Collections.EMPTY_LIST, collectedAlerts );
396     }
397     
398
399     /**
400      * @throws Exception if the test fails
401      */

402     public void testInputNamedId() throws Exception {
403         doTestInputWithName("id");
404     }
405
406
407     /**
408      * @throws Exception if the test fails
409      */

410     public void testInputNamedAction() throws Exception {
411         doTestInputWithName("action");
412     }
413
414
415     private void doTestInputWithName( final String name ) throws Exception {
416         final String content
417             = "<html><head><title>foo</title><script>"
418             + "function go() {\n"
419             + " alert(document.simple_form."+name+".value);\n"
420             + " document.simple_form."+name+".value='foo';\n"
421             + " alert(document.simple_form."+name+".value);\n"
422             + "}</script></head>"
423             + "<body onload='go()'>"
424             + "<p>hello world</p>"
425             + "<form action='login.jsp' name='simple_form'>"
426             + " <input name='"+name+"' type='hidden' value='"+name+"2'>"
427             + "</form>"
428             + "</body></html>";
429
430         final List collectedAlerts = new ArrayList();
431         final HtmlPage page = loadPage(content, collectedAlerts);
432         assertEquals("foo", page.getTitleText());
433         final List expectedAlerts = Arrays.asList( new String[]{
434             name+"2", "foo"
435         } );
436         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
437         assertEquals( expectedAlerts, collectedAlerts );
438     }
439
440
441     /**
442      * Regression test that used to blow up on page load
443      * @throws Exception if the test fails
444      */

445     public void testAccessingRadioButtonArrayByName_Regression() throws Exception {
446         final WebClient client = new WebClient();
447         final MockWebConnection webConnection = new MockWebConnection( client );
448
449         final List collectedAlerts = new ArrayList();
450         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
451
452         final String firstContent
453             = "<html><head><title>Button Test</title></head><body><form name='whatsnew'>"
454             + "<input type='radio' name='second' value='1'>"
455             + "<input type='radio' name='second' value='2' checked>"
456             + "</form><script>clickAction();\n"
457             + "function clickAction(){\n"
458             + " var value = -1;\n"
459             + " radios = document.forms['whatsnew'].elements['second'];\n"
460             + " for (var i=0; i < radios.length; i++){\n"
461             + " if (radios[i].checked == true) {\n"
462             + " value = radios[i].value;\n"
463             + " break;\n"
464             + " }\n"
465             + " }\n"
466             + " alert('value = ' + value);\n"
467             + "}\n"
468             + "</script></body></html>";
469
470         webConnection.setResponse(
471             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
472         client.setWebConnection( webConnection );
473
474         final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST);
475         assertEquals("Button Test", page.getTitleText());
476
477         final List expectedAlerts = Arrays.asList( new String[]{"value = 2"} );
478         assertEquals( expectedAlerts, collectedAlerts );
479     }
480
481
482     /**
483      * Test for a bug that appeared when visiting mail.yahoo.com. Setting the value of one input
484      * seems to blow away the other input. Also tests that the form input collection does not get
485      * cached before the document is finished loading.
486      * @throws Exception if the test fails
487      */

488     public void testFindInputWithoutTypeDefined() throws Exception {
489         final String htmlContent
490             = "<html><head><title>foo</title></head>"
491             + "<body onload='alert(document.simple_form.login.value);'>"
492             + "<p>hello world</p><table><tr><td>"
493             + "<form action='login.jsp' name='simple_form'>"
494             + " <input name='msg' type='hidden' value='0'>"
495             + " <script>document.simple_form.msg.value=1</script>"
496             + " <input name='login' size='17' value='foo'>"
497             + "</form></td></tr></table>"
498             + "</body></html>";
499         final List collectedAlerts = new ArrayList();
500
501         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
502
503         assertEquals("foo", page.getTitleText());
504         final List expectedAlerts = Arrays.asList( new String[]{
505             "foo"
506         } );
507         assertEquals( expectedAlerts, collectedAlerts );
508     }
509     
510     /**
511      * @throws Exception if the test fails
512      */

513     public void testFormSubmit_MultipleButtons() throws Exception {
514         final WebClient client = new WebClient();
515         final MockWebConnection webConnection = new MockWebConnection( client );
516
517         final String firstContent
518             = "<html><head><title>first</title></head><body>\n"
519             + "<p>hello world</p>"
520             + "<form name='form1' method='get' action='http://second'>"
521             + " <button type='submit' name='button1' id='button1'/>"
522             + " <button type='submit' name='button2' />"
523             + "</form>"
524             + "</body></html>";
525         final String secondContent
526             = "<html><head><title>second</title></head><body>\n"
527             + "<p>hello world</p>"
528             + "</body></html>";
529
530         webConnection.setResponse(
531             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
532         webConnection.setResponse(
533             URL_SECOND, secondContent, 200, "OK", "text/html",
534             Collections.EMPTY_LIST );
535         client.setWebConnection( webConnection );
536
537         final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST);
538         assertEquals( "first", page.getTitleText() );
539
540         final HtmlButton button = (HtmlButton)page.getHtmlElementById("button1");
541         final HtmlPage secondPage = (HtmlPage)button.click();
542         assertEquals( "second", secondPage.getTitleText() );
543         
544         final List expectedParameters = Arrays.asList( new Object[]{ new KeyValuePair("button1","")} );
545         assertEquals( expectedParameters, webConnection.getLastParameters());
546     }
547
548     /**
549      * Test form.length - This method does not count the type=image
550      * input tags.
551      * @throws Exception if the test fails
552      */

553     public void testLength() throws Exception {
554         final String content
555             = "<html><head><title>foo</title><script>\n"
556             + "function doTest(){\n"
557             + " alert(document.form1.length);"
558             + "}\n"
559             + "</script></head><body onload='doTest()'>\n"
560             + "<form name='form1'>"
561             + " <input type='radio' name='radio1' value='1'/>"
562             + " <input type='image' SRC='foo' value='1'/>"
563             + " <input type='submit' name='submit1' value='1'/>"
564             + "</form>"
565             + "</body></html>";
566
567         final List collectedAlerts = new ArrayList();
568         loadPage(content, collectedAlerts);
569
570         final List expectedAlerts = Arrays.asList( new String[]{
571             "2"
572         } );
573
574         assertEquals( expectedAlerts, collectedAlerts );
575     }
576
577     /**
578      * @throws Exception if the test fails
579      */

580     public void testGet() throws Exception {
581         final String content
582             = "<html><head><title>foo</title><script>\n"
583             + "function doTest(){\n"
584             + " alert(document.form1[0].name)\n"
585             + "}\n"
586             + "</script></head><body onload='doTest()'>\n"
587             + "<p>hello world</p>"
588             + "<form name='form1'>"
589             + " <input type='button' name='button1' />"
590             + "</form>"
591             + "</body></html>";
592
593         final List collectedAlerts = new ArrayList();
594         loadPage(content, collectedAlerts);
595
596         final List expectedAlerts = Arrays.asList( new String[]{ "button1" } );
597
598         assertEquals( expectedAlerts, collectedAlerts );
599     }
600
601
602     /**
603     * @throws Exception if the test fails
604     */

605     public void testLostFunction() throws Exception {
606
607         final String content
608             = "<html><head><title>foo</title><script>"
609             + " function onSubmit() { alert('hi!'); return false; }"
610             + "</script></head><body>"
611             + "<form onsubmit='return onSubmit();'>"
612             + " <input type='submit' id='clickMe' />"
613             + "</form>"
614             + "</body></html>";
615     
616         final List collectedAlerts = new ArrayList();
617         final HtmlPage page = loadPage(content, collectedAlerts);
618         final HtmlSubmitInput button = (HtmlSubmitInput)
619             page.getHtmlElementById("clickMe");
620         button.click();
621         final List expectedAlerts = Collections.singletonList("hi!");
622         assertEquals(expectedAlerts, collectedAlerts);
623     }
624  
625     /**
626      * Test that the elements collection is live
627     * @throws Exception if the test fails
628     */

629     public void testElementsLive() throws Exception {
630
631         final String content = "<html>"
632             + "<body>"
633             + "<form name='myForm'>"
634             + "<script>"
635             + "var oElements = document.myForm.elements;"
636             + "alert(oElements.length);"
637             + "</script>"
638             + "<input type='text' name='foo'/>"
639             + "<script>"
640             + "alert(oElements.length);"
641             + "alert(document.myForm.elements.length);"
642             + "alert(oElements == document.myForm.elements);"
643             + "</script>"
644             + "</form>"
645             + "</body>"
646             + "</html>";
647
648         final List collectedAlerts = new ArrayList();
649         loadPage(content, collectedAlerts);
650
651         final List expectedAlerts = Arrays.asList( new String[]{"0", "1", "1", "true"});
652         assertEquals(expectedAlerts, collectedAlerts);
653     }
654
655     /**
656      * @throws Exception if the test fails
657      */

658     public void testGetFormFromFormsById() throws Exception {
659         final String content =
660             "<html>"
661             + "<head></head>"
662             + "<body onload=\"alert(document.forms['myForm'].action)\">"
663             + "<form id='myForm' action='foo.html'>"
664             + "</form>"
665             + "</body>"
666             + "</html>";
667         
668         
669         final List collectedAlerts = new ArrayList();
670         final List expectedAlerts = Arrays.asList( new String[]{
671             "foo.html"
672         } );
673         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
674         loadPage(content, collectedAlerts);
675
676         assertEquals(expectedAlerts, collectedAlerts);
677     }
678
679     /**
680      * @throws Exception if the test fails
681      */

682     public void testGetFieldNamedLikeForm() throws Exception {
683         final String content =
684             "<html>"
685             + "<head></head>"
686             + "<body onload='alert(document.login.login.type)'>"
687             + "<form name='login' action='foo.html'>"
688             + "<input name='login' type='text'>"
689             + "</form>"
690             + "</body>"
691             + "</html>";
692         
693         final List collectedAlerts = new ArrayList();
694         final List expectedAlerts = Arrays.asList( new String[]{ "text" } );
695         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
696         loadPage(content, collectedAlerts);
697
698         assertEquals(expectedAlerts, collectedAlerts);
699     }
700
701     /**
702      * document.myForm.submit returns a field name submit or the submit function
703      * depending on the sort of the field named submit
704      * @throws Exception if the test fails
705      */

706     public void testFieldNamedSubmit() throws Exception {
707         testFieldNamedSubmit("<input type='text' name='submit'>", "INPUT");
708         testFieldNamedSubmit("<input type='password' name='submit'>", "INPUT");
709         testFieldNamedSubmit("<input type='submit' name='submit'>", "INPUT");
710         testFieldNamedSubmit("<input type='radio' name='submit'>", "INPUT");
711         testFieldNamedSubmit("<input type='checkbox' name='submit'>", "INPUT");
712         testFieldNamedSubmit("<input type='button' name='submit'>", "INPUT");
713         testFieldNamedSubmit("<button type='submit' name='submit'>", "BUTTON");
714         testFieldNamedSubmit("<textarea name='submit'></textarea>", "TEXTAREA");
715         testFieldNamedSubmit("<select name='submit'></select>", "SELECT");
716         testFieldNamedSubmit("<input type='image' name='submit'>", "function");
717         testFieldNamedSubmit("<input type='IMAGE' name='submit'>", "function");
718     }
719
720     /**
721      * @param htmlSnippet The html to embed in the test
722      * @param expected The expected alert
723      * @throws Exception if the test fails
724      */

725     private void testFieldNamedSubmit(final String htmlSnippet, final String expected) throws Exception {
726         final String content =
727             "<html>"
728             + "<head>"
729             + "<script>"
730             + "function test()"
731             + "{"
732             + " if (document.login.submit.tagName)"
733             + " alert(document.login.submit.tagName);"
734             + " else"
735             + " alert('function');"
736             + "}"
737             + "</script>"
738             + "</head>"
739             + "<body onload='test()'>"
740             + "<form name='login' action='foo.html'>"
741             + htmlSnippet
742             + "</form>"
743             + "</body>"
744             + "</html>";
745
746         final List collectedAlerts = new ArrayList();
747         final List expectedAlerts = Arrays.asList( new String[]{ expected } );
748         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
749         loadPage(content, collectedAlerts);
750
751         assertEquals(expectedAlerts, collectedAlerts);
752     }
753
754     /**
755      * @throws Exception if the test fails
756      */

757     public void testFieldFoundWithID() throws Exception {
758         final String content = "<html><head>"
759             + "<script>"
760             + "function test()"
761             + "{"
762             + " alert(IRForm.IRText.value);"
763             + " alert(IRForm.myField.length);"
764             + "}"
765             + "</script>"
766             + "</head>"
767             + "<body onload='test()'>"
768             + " <form name='IRForm' action='#'>"
769             + " <input type='text' id='IRText' value='before'/>"
770             + " <input type='image' name='myField' SRC='foo.gif'/>"
771             + " <input type='image' id='myField' SRC='foo.gif'/>"
772             + " <input type='text' name='myField'/>"
773             + " <input type='text' id='myField'/>"
774             + " </form>"
775             + "</body>"
776             + "</html>";
777         final List expectedAlerts = Arrays.asList( new String[]{ "before", "2" } );
778         final List collectedAlerts = new ArrayList();
779         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
780         loadPage(content, collectedAlerts);
781
782         assertEquals(expectedAlerts, collectedAlerts);
783     }
784 }
785
Popular Tags