Source Code Cross Referenced for HtmlFormTest.java in  » Testing » htmlunit » com » gargoylesoftware » htmlunit » html » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » htmlunit » com.gargoylesoftware.htmlunit.html 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
0003:         *
0004:         * Redistribution and use in source and binary forms, with or without
0005:         * modification, are permitted provided that the following conditions are met:
0006:         *
0007:         * 1. Redistributions of source code must retain the above copyright notice,
0008:         *    this list of conditions and the following disclaimer.
0009:         * 2. Redistributions in binary form must reproduce the above copyright notice,
0010:         *    this list of conditions and the following disclaimer in the documentation
0011:         *    and/or other materials provided with the distribution.
0012:         * 3. The end-user documentation included with the redistribution, if any, must
0013:         *    include the following acknowledgment:
0014:         *
0015:         *       "This product includes software developed by Gargoyle Software Inc.
0016:         *        (http://www.GargoyleSoftware.com/)."
0017:         *
0018:         *    Alternately, this acknowledgment may appear in the software itself, if
0019:         *    and wherever such third-party acknowledgments normally appear.
0020:         * 4. The name "Gargoyle Software" must not be used to endorse or promote
0021:         *    products derived from this software without prior written permission.
0022:         *    For written permission, please contact info@GargoyleSoftware.com.
0023:         * 5. Products derived from this software may not be called "HtmlUnit", nor may
0024:         *    "HtmlUnit" appear in their name, without prior written permission of
0025:         *    Gargoyle Software Inc.
0026:         *
0027:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
0028:         * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
0029:         * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
0030:         * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0031:         * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0032:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
0033:         * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0034:         * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0035:         * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
0036:         * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0037:         */
0038:        package com.gargoylesoftware.htmlunit.html;
0039:
0040:        import java.net.URL;
0041:        import java.util.ArrayList;
0042:        import java.util.Arrays;
0043:        import java.util.Collections;
0044:        import java.util.List;
0045:        import java.util.ListIterator;
0046:        import java.util.Map;
0047:
0048:        import org.apache.commons.lang.StringUtils;
0049:
0050:        import com.gargoylesoftware.htmlunit.BrowserVersion;
0051:        import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
0052:        import com.gargoylesoftware.htmlunit.ElementNotFoundException;
0053:        import com.gargoylesoftware.htmlunit.KeyValuePair;
0054:        import com.gargoylesoftware.htmlunit.MockWebConnection;
0055:        import com.gargoylesoftware.htmlunit.Page;
0056:        import com.gargoylesoftware.htmlunit.SubmitMethod;
0057:        import com.gargoylesoftware.htmlunit.WebClient;
0058:        import com.gargoylesoftware.htmlunit.WebRequestSettings;
0059:        import com.gargoylesoftware.htmlunit.WebTestCase;
0060:        import com.gargoylesoftware.htmlunit.WebWindow;
0061:
0062:        /**
0063:         * Tests for {@link HtmlForm}.
0064:         *
0065:         * @version $Revision: 2132 $
0066:         * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
0067:         * @author <a href="mailto:chen_jun@users.sourceforge.net">Jun Chen</a>
0068:         * @author George Murnock
0069:         * @author Marc Guillemot
0070:         * @author Ahmed Ashour
0071:         * @author Philip Graf
0072:         */
0073:        public class HtmlFormTest extends WebTestCase {
0074:            /**
0075:             * Create an instance
0076:             *
0077:             * @param name The name of the test
0078:             */
0079:            public HtmlFormTest(final String name) {
0080:                super (name);
0081:            }
0082:
0083:            /**
0084:             * Test the good case for setCheckedRadioButton()
0085:             *
0086:             * @exception Exception If the test fails
0087:             */
0088:            public void testSetSelectedRadioButton_ValueExists()
0089:                    throws Exception {
0090:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0091:                        + "<form id='form1'>\n"
0092:                        + "<input type='radio' name='foo' value='1' selected='selected' id='input1'/>\n"
0093:                        + "<input type='radio' name='foo' value='2' id='input2'/>\n"
0094:                        + "<input type='radio' name='foo' value='3'/>\n"
0095:                        + "<input type='submit' name='button' value='foo'/>\n"
0096:                        + "</form></body></html>";
0097:                final HtmlPage page = loadPage(htmlContent);
0098:                final MockWebConnection webConnection = getMockConnection(page);
0099:
0100:                final HtmlForm form = (HtmlForm) page
0101:                        .getHtmlElementById("form1");
0102:
0103:                final HtmlSubmitInput pushButton = (HtmlSubmitInput) form
0104:                        .getInputByName("button");
0105:
0106:                ((HtmlRadioButtonInput) form
0107:                        .getFirstByXPath("//input[@type='radio' and @name='foo' and @value='2']"))
0108:                        .setChecked(true);
0109:
0110:                assertFalse(((HtmlRadioButtonInput) page
0111:                        .getHtmlElementById("input1")).isChecked());
0112:                assertTrue(((HtmlRadioButtonInput) page
0113:                        .getHtmlElementById("input2")).isChecked());
0114:
0115:                // Test that only one value for the radio button is being passed back to the server
0116:                final HtmlPage secondPage = (HtmlPage) pushButton.click();
0117:
0118:                assertEquals("url", URL_GARGOYLE.toExternalForm()
0119:                        + "?foo=2&button=foo", secondPage.getWebResponse()
0120:                        .getUrl());
0121:                assertEquals("method", SubmitMethod.GET, webConnection
0122:                        .getLastMethod());
0123:            }
0124:
0125:            /**
0126:             *  Test setCheckedRadioButton() with a value that doesn't exist
0127:             *
0128:             * @exception Exception If the test fails
0129:             */
0130:            public void testSetSelectedRadioButton_ValueDoesNotExist_DoNotForceSelection()
0131:                    throws Exception {
0132:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0133:                        + "<form id='form1'>\n"
0134:                        + "<input type='radio' name='foo' value='1' selected='selected'/>\n"
0135:                        + "<input type='radio' name='foo' value='2'/>\n"
0136:                        + "<input type='radio' name='foo' value='3'/>\n"
0137:                        + "<input type='submit' name='button' value='foo'/>\n"
0138:                        + "</form></body></html>";
0139:                final HtmlPage page = loadPage(htmlContent);
0140:
0141:                final HtmlForm form = (HtmlForm) page
0142:                        .getHtmlElementById("form1");
0143:
0144:                final HtmlInput pushButton = form.getInputByName("button");
0145:                assertNotNull(pushButton);
0146:
0147:                assertNull(form
0148:                        .getFirstByXPath("//input[@type='radio' and @name='foo' and @value='4']"));
0149:            }
0150:
0151:            /**
0152:             *  Test setCheckedRadioButton() with a value that doesn't exist
0153:             *
0154:             * @exception Exception If the test fails
0155:             * @deprecated after 1.11
0156:             */
0157:            public void testSetSelectedRadioButton_ValueDoesNotExist_ForceSelection()
0158:                    throws Exception {
0159:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0160:                        + "<form id='form1'>\n"
0161:                        + "<input type='radio' name='foo' value='1' selected='selected'/>\n"
0162:                        + "<input type='radio' name='foo' value='2'/>\n"
0163:                        + "<input type='radio' name='foo' value='3'/>\n"
0164:                        + "<input type='submit' name='button' value='foo'/>\n"
0165:                        + "</form></body></html>";
0166:                final HtmlPage page = loadPage(htmlContent);
0167:                final MockWebConnection webConnection = getMockConnection(page);
0168:
0169:                final HtmlForm form = (HtmlForm) page
0170:                        .getHtmlElementById("form1");
0171:
0172:                final HtmlSubmitInput pushButton = (HtmlSubmitInput) form
0173:                        .getInputByName("button");
0174:
0175:                form.fakeCheckedRadioButton("foo", "4");
0176:
0177:                // Test that only one value for the radio button is being passed back to the server
0178:                final HtmlPage secondPage = (HtmlPage) pushButton.click();
0179:
0180:                assertEquals("url", URL_GARGOYLE.toExternalForm()
0181:                        + "?button=foo&foo=4", secondPage.getWebResponse()
0182:                        .getUrl());
0183:                assertEquals("method", SubmitMethod.GET, webConnection
0184:                        .getLastMethod());
0185:            }
0186:
0187:            /**
0188:             * @throws Exception if the test fails
0189:             */
0190:            public void testSubmit_String() throws Exception {
0191:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0192:                        + "<form id='form1'>\n"
0193:                        + "<input id='submitButton' type='submit' name='button' value='foo'/>\n"
0194:                        + "</form></body></html>";
0195:                final HtmlPage page = loadPage(htmlContent);
0196:
0197:                final HtmlForm form = (HtmlForm) page
0198:                        .getHtmlElementById("form1");
0199:
0200:                // Regression test: this used to blow up
0201:                form.submit((HtmlSubmitInput) page
0202:                        .getHtmlElementById("submitButton"));
0203:            }
0204:
0205:            /**
0206:             * @throws Exception if the test fails
0207:             */
0208:            public void testSubmit_ExtraParameters() throws Exception {
0209:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0210:                        + "<form id='form1' method='post'>\n"
0211:                        + "    <input type='text' name='textfield' value='*'/>\n"
0212:                        + "    <input type='submit' name='button' value='foo'/>\n"
0213:                        + "</form></body></html>";
0214:                final HtmlPage page = loadPage(htmlContent);
0215:                final MockWebConnection webConnection = getMockConnection(page);
0216:
0217:                final HtmlForm form = (HtmlForm) page
0218:                        .getHtmlElementById("form1");
0219:
0220:                final HtmlSubmitInput button = (HtmlSubmitInput) form
0221:                        .getInputByName("button");
0222:                button.click();
0223:
0224:                final List expectedParameters = Arrays.asList(new Object[] {
0225:                        new KeyValuePair("textfield", "*"),
0226:                        new KeyValuePair("button", "foo") });
0227:                final List collectedParameters = webConnection
0228:                        .getLastParameters();
0229:
0230:                assertEquals(expectedParameters, collectedParameters);
0231:            }
0232:
0233:            /**
0234:             * @throws Exception if the test fails
0235:             */
0236:            public void testSubmit_BadSubmitMethod() throws Exception {
0237:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0238:                        + "<form id='form1' method='put'>\n"
0239:                        + "    <input type='text' name='textfield' value='*'/>\n"
0240:                        + "    <input type='submit' name='button' id='button' value='foo'/>\n"
0241:                        + "</form></body></html>";
0242:                final HtmlPage page = loadPage(htmlContent);
0243:                final MockWebConnection webConnection = getMockConnection(page);
0244:
0245:                ((ClickableElement) page.getHtmlElementById("button")).click();
0246:
0247:                assertEquals(SubmitMethod.GET, webConnection.getLastMethod());
0248:            }
0249:
0250:            /**
0251:             * @throws Exception if the test fails
0252:             */
0253:            public void testSubmit_onSubmitHandler() throws Exception {
0254:                final String firstContent = "<html><head><title>First</title></head><body>\n"
0255:                        + "<form method='get' action='"
0256:                        + URL_SECOND
0257:                        + "' onSubmit='alert(\"clicked\")'>\n"
0258:                        + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
0259:                        + "</body></html>";
0260:                final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
0261:
0262:                final WebClient client = new WebClient();
0263:                final List collectedAlerts = new ArrayList();
0264:                client.setAlertHandler(new CollectingAlertHandler(
0265:                        collectedAlerts));
0266:
0267:                final MockWebConnection webConnection = new MockWebConnection(
0268:                        client);
0269:                webConnection.setResponse(URL_FIRST, firstContent);
0270:                webConnection.setDefaultResponse(secondContent);
0271:
0272:                client.setWebConnection(webConnection);
0273:
0274:                final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
0275:                final HtmlSubmitInput button = (HtmlSubmitInput) firstPage
0276:                        .getHtmlElementById("button");
0277:
0278:                assertEquals(Collections.EMPTY_LIST, collectedAlerts);
0279:                final HtmlPage secondPage = (HtmlPage) button.click();
0280:                assertEquals("Second", secondPage.getTitleText());
0281:
0282:                assertEquals(new String[] { "clicked" }, collectedAlerts);
0283:            }
0284:
0285:            /**
0286:             * @throws Exception if the test fails
0287:             */
0288:            public void testSubmit_onSubmitHandler_returnFalse()
0289:                    throws Exception {
0290:                final String firstContent = "<html><head><title>First</title></head><body>\n"
0291:                        + "<form method='get' action='"
0292:                        + URL_SECOND
0293:                        + "' "
0294:                        + "onSubmit='alert(\"clicked\");return false;'>\n"
0295:                        + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
0296:                        + "</body></html>";
0297:                final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
0298:
0299:                final WebClient client = new WebClient();
0300:                final List collectedAlerts = new ArrayList();
0301:                client.setAlertHandler(new CollectingAlertHandler(
0302:                        collectedAlerts));
0303:
0304:                final MockWebConnection webConnection = new MockWebConnection(
0305:                        client);
0306:                webConnection.setResponse(URL_FIRST, firstContent);
0307:                webConnection.setResponse(URL_SECOND, secondContent);
0308:
0309:                client.setWebConnection(webConnection);
0310:
0311:                final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
0312:                final HtmlSubmitInput button = (HtmlSubmitInput) firstPage
0313:                        .getHtmlElementById("button");
0314:
0315:                assertEquals(Collections.EMPTY_LIST, collectedAlerts);
0316:                final HtmlPage secondPage = (HtmlPage) button.click();
0317:                assertEquals(firstPage.getTitleText(), secondPage
0318:                        .getTitleText());
0319:
0320:                assertEquals(new String[] { "clicked" }, collectedAlerts);
0321:            }
0322:
0323:            /**
0324:             * Regression test for bug 1628521
0325:             * NullPointerException when submitting forms
0326:             * @throws Exception if the test fails
0327:             */
0328:            public void testSubmit_onSubmitHandler_fails() throws Exception {
0329:                final String firstContent = "<html><head><title>First</title></head><body>\n"
0330:                        + "<form method='get' action='"
0331:                        + URL_SECOND
0332:                        + "' onSubmit='return null'>\n"
0333:                        + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
0334:                        + "</body></html>";
0335:                final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
0336:
0337:                final WebClient client = new WebClient();
0338:                final MockWebConnection webConnection = new MockWebConnection(
0339:                        client);
0340:                webConnection.setResponse(URL_FIRST, firstContent);
0341:                webConnection.setDefaultResponse(secondContent);
0342:
0343:                client.setWebConnection(webConnection);
0344:
0345:                final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
0346:                final HtmlSubmitInput button = (HtmlSubmitInput) firstPage
0347:                        .getHtmlElementById("button");
0348:                final HtmlPage secondPage = (HtmlPage) button.click();
0349:                assertEquals("Second", secondPage.getTitleText());
0350:            }
0351:
0352:            /**
0353:             * @throws Exception if the test fails
0354:             */
0355:            public void testSubmit_onSubmitHandler_javascriptDisabled()
0356:                    throws Exception {
0357:                final String firstContent = "<html><head><title>First</title></head><body>\n"
0358:                        + "<form method='get' action='"
0359:                        + URL_SECOND
0360:                        + "' onSubmit='alert(\"clicked\")'>\n"
0361:                        + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
0362:                        + "</body></html>";
0363:                final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
0364:
0365:                final WebClient client = new WebClient();
0366:                client.setJavaScriptEnabled(false);
0367:
0368:                final List collectedAlerts = new ArrayList();
0369:                client.setAlertHandler(new CollectingAlertHandler(
0370:                        collectedAlerts));
0371:
0372:                final MockWebConnection webConnection = new MockWebConnection(
0373:                        client);
0374:                webConnection.setResponse(URL_FIRST, firstContent);
0375:                webConnection.setDefaultResponse(secondContent);
0376:
0377:                client.setWebConnection(webConnection);
0378:
0379:                final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
0380:                final HtmlSubmitInput button = (HtmlSubmitInput) firstPage
0381:                        .getHtmlElementById("button");
0382:
0383:                assertEquals(Collections.EMPTY_LIST, collectedAlerts);
0384:                final HtmlPage secondPage = (HtmlPage) button.click();
0385:                assertEquals("First", firstPage.getTitleText());
0386:                assertEquals("Second", secondPage.getTitleText());
0387:
0388:                assertEquals(Collections.EMPTY_LIST, collectedAlerts);
0389:            }
0390:
0391:            /**
0392:             * @throws Exception if the test fails
0393:             */
0394:            public void testSubmit_javascriptAction() throws Exception {
0395:                final String firstContent = "<html><head><title>First</title></head><body>\n"
0396:                        + "<form method='get' action='javascript:alert(\"clicked\")'>\n"
0397:                        + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
0398:                        + "</body></html>";
0399:                final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
0400:
0401:                final WebClient client = new WebClient();
0402:                final List collectedAlerts = new ArrayList();
0403:                client.setAlertHandler(new CollectingAlertHandler(
0404:                        collectedAlerts));
0405:
0406:                final MockWebConnection webConnection = new MockWebConnection(
0407:                        client);
0408:                webConnection.setResponse(URL_FIRST, firstContent);
0409:                webConnection.setResponse(URL_SECOND, secondContent);
0410:
0411:                client.setWebConnection(webConnection);
0412:
0413:                final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
0414:                final HtmlSubmitInput button = (HtmlSubmitInput) firstPage
0415:                        .getHtmlElementById("button");
0416:
0417:                assertEquals(Collections.EMPTY_LIST, collectedAlerts);
0418:                final HtmlPage secondPage = (HtmlPage) button.click();
0419:                assertEquals(firstPage.getTitleText(), secondPage
0420:                        .getTitleText());
0421:
0422:                assertEquals(new String[] { "clicked" }, collectedAlerts);
0423:            }
0424:
0425:            /**
0426:             * @throws Exception if the test fails
0427:             */
0428:            public void testSubmit_javascriptAction_javascriptDisabled()
0429:                    throws Exception {
0430:                final String firstContent = "<html><head><title>First</title></head><body>\n"
0431:                        + "<form method='get' action='javascript:alert(\"clicked\")'>\n"
0432:                        + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
0433:                        + "</body></html>";
0434:
0435:                final WebClient client = new WebClient();
0436:                client.setJavaScriptEnabled(false);
0437:
0438:                final List collectedAlerts = new ArrayList();
0439:                client.setAlertHandler(new CollectingAlertHandler(
0440:                        collectedAlerts));
0441:
0442:                final MockWebConnection webConnection = new MockWebConnection(
0443:                        client);
0444:                webConnection.setResponse(URL_FIRST, firstContent);
0445:
0446:                client.setWebConnection(webConnection);
0447:
0448:                final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
0449:                final HtmlSubmitInput button = (HtmlSubmitInput) firstPage
0450:                        .getHtmlElementById("button");
0451:
0452:                assertEquals(Collections.EMPTY_LIST, collectedAlerts);
0453:                final HtmlPage secondPage = (HtmlPage) button.click();
0454:                assertSame(firstPage, secondPage);
0455:            }
0456:
0457:            /**
0458:             * Regression test for a bug that caused a NullPointer exception to be thrown during submit.
0459:             * @throws Exception if the test fails
0460:             */
0461:            public void testSubmitRadioButton() throws Exception {
0462:                final String htmlContent = "<html><body><form method='POST' action='http://first'>\n"
0463:                        + "<table><tr> <td ><input type='radio' name='name1' value='foo'> "
0464:                        + "Option 1</td> </tr>\n"
0465:                        + "<tr> <td ><input type='radio' name='name1' value='bar' checked >\n"
0466:                        + "Option 2</td> </tr>\n"
0467:                        + "<tr> <td ><input type='radio' name='name1' value='baz'> Option 3</td> </tr>\n"
0468:                        + "</table><input type='submit' value='Login' name='loginButton1'></form>\n"
0469:                        + "</body></html>";
0470:
0471:                final HtmlPage page = loadPage(htmlContent);
0472:
0473:                final HtmlSubmitInput loginButton = (HtmlSubmitInput) page
0474:                        .getDocumentHtmlElement().getOneHtmlElementByAttribute(
0475:                                "input", "value", "Login");
0476:                loginButton.click();
0477:            }
0478:
0479:            /**
0480:             * @throws Exception if the test fails
0481:             */
0482:            public void testReset_onResetHandler() throws Exception {
0483:                final String html = "<html><head><title>First</title></head><body>\n"
0484:                        + "<form method='get' action='"
0485:                        + URL_SECOND
0486:                        + "' "
0487:                        + "onReset='alert(\"clicked\");alert(event.type)'>\n"
0488:                        + "<input name='button' type='reset' value='PushMe' id='button'/></form>\n"
0489:                        + "</body></html>";
0490:
0491:                final List collectedAlerts = new ArrayList();
0492:
0493:                final HtmlPage firstPage = loadPage(html, collectedAlerts);
0494:                final HtmlResetInput button = (HtmlResetInput) firstPage
0495:                        .getHtmlElementById("button");
0496:
0497:                assertEquals(Collections.EMPTY_LIST, collectedAlerts);
0498:                final HtmlPage secondPage = (HtmlPage) button.click();
0499:                assertSame(firstPage, secondPage);
0500:
0501:                final String[] expectedAlerts = { "clicked", "reset" };
0502:                assertEquals(expectedAlerts, collectedAlerts);
0503:            }
0504:
0505:            /**
0506:             * <p>Simulate a bug report where an anchor contained javascript that caused a form submit.
0507:             * According to the bug report, the form would be submitted even though the onsubmit
0508:             * handler would return false.  This wasn't reproducible but I added a test for it anyway.</p>
0509:             *
0510:             * <p>UPDATE: If the form submit is triggered by javascript then the onsubmit handler is not
0511:             * supposed to be called so it doesn't matter what value it returns.</p>
0512:             * @throws Exception if the test fails
0513:             */
0514:            public void testSubmit_AnchorCausesSubmit_onSubmitHandler_returnFalse()
0515:                    throws Exception {
0516:                final String firstContent = "<html><head><title>First</title></head>\n"
0517:                        + "<script>function doalert(message){alert(message);}</script>\n"
0518:                        + "<body><form name='form1' method='get' action='"
0519:                        + URL_SECOND
0520:                        + "' "
0521:                        + "onSubmit='doalert(\"clicked\");return false;'>\n"
0522:                        + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
0523:                        + "<a id='link1' href='javascript:document.form1.submit()'>Click me</a>\n"
0524:                        + "</body></html>";
0525:                final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
0526:
0527:                final WebClient client = new WebClient();
0528:                final List collectedAlerts = new ArrayList();
0529:                client.setAlertHandler(new CollectingAlertHandler(
0530:                        collectedAlerts));
0531:
0532:                final MockWebConnection webConnection = new MockWebConnection(
0533:                        client);
0534:                webConnection.setResponse(URL_FIRST, firstContent);
0535:                webConnection.setDefaultResponse(secondContent);
0536:
0537:                client.setWebConnection(webConnection);
0538:
0539:                final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
0540:                final HtmlAnchor anchor = (HtmlAnchor) firstPage
0541:                        .getHtmlElementById("link1");
0542:
0543:                assertEquals(Collections.EMPTY_LIST, collectedAlerts);
0544:                final HtmlPage secondPage = (HtmlPage) anchor.click();
0545:                assertEquals("Second", secondPage.getTitleText());
0546:
0547:                assertEquals(Collections.EMPTY_LIST, collectedAlerts);
0548:            }
0549:
0550:            /**
0551:             * @throws Exception if the test fails
0552:             */
0553:            public void testSubmit_NoDefaultValue() throws Exception {
0554:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0555:                        + "<form id='form1' method='post'>\n"
0556:                        + "    <input type='text' name='textfield'/>\n"
0557:                        + "    <input type='submit' name='button' value='foo'/>\n"
0558:                        + "</form></body></html>";
0559:                final HtmlPage page = loadPage(htmlContent);
0560:                final MockWebConnection webConnection = getMockConnection(page);
0561:
0562:                final HtmlForm form = (HtmlForm) page
0563:                        .getHtmlElementById("form1");
0564:
0565:                final HtmlSubmitInput button = (HtmlSubmitInput) form
0566:                        .getInputByName("button");
0567:                button.click();
0568:
0569:                final List expectedParameters = Arrays.asList(new Object[] {
0570:                        new KeyValuePair("textfield", ""),
0571:                        new KeyValuePair("button", "foo") });
0572:                final List collectedParameters = webConnection
0573:                        .getLastParameters();
0574:
0575:                assertEquals(expectedParameters, collectedParameters);
0576:            }
0577:
0578:            /**
0579:             * @throws Exception if the test fails
0580:             */
0581:            public void testSubmit_NoNameOnControl() throws Exception {
0582:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0583:                        + "<form id='form1' method='post'>\n"
0584:                        + "    <input type='text' id='textfield' value='blah'/>\n"
0585:                        + "    <input type='submit' name='button' value='foo'/>\n"
0586:                        + "</form></body></html>";
0587:                final HtmlPage page = loadPage(htmlContent);
0588:                final MockWebConnection webConnection = getMockConnection(page);
0589:
0590:                final HtmlForm form = (HtmlForm) page
0591:                        .getHtmlElementById("form1");
0592:
0593:                final HtmlSubmitInput button = (HtmlSubmitInput) form
0594:                        .getInputByName("button");
0595:                button.click();
0596:
0597:                final List expectedParameters = Arrays
0598:                        .asList(new Object[] { new KeyValuePair("button", "foo") });
0599:                final List collectedParameters = webConnection
0600:                        .getLastParameters();
0601:
0602:                assertEquals(expectedParameters, collectedParameters);
0603:            }
0604:
0605:            /**
0606:             * @throws Exception if the test fails
0607:             */
0608:            public void testSubmit_NoNameOnButton() throws Exception {
0609:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0610:                        + "<form id='form1' method='post'>\n"
0611:                        + "    <input type='text' id='textfield' value='blah' name='textfield' />\n"
0612:                        + "    <button type='submit' id='button' value='Go'>Go</button>\n"
0613:                        + "</form></body></html>";
0614:                final HtmlPage page = loadPage(htmlContent);
0615:                final MockWebConnection webConnection = getMockConnection(page);
0616:
0617:                final HtmlButton button = (HtmlButton) page
0618:                        .getHtmlElementById("button");
0619:                button.click();
0620:
0621:                final List expectedParameters = Arrays
0622:                        .asList(new Object[] { new KeyValuePair("textfield",
0623:                                "blah") });
0624:                final List collectedParameters = webConnection
0625:                        .getLastParameters();
0626:
0627:                assertEquals(expectedParameters, collectedParameters);
0628:            }
0629:
0630:            /**
0631:             * @throws Exception if the test fails
0632:             */
0633:            public void testSubmit_NestedInput() throws Exception {
0634:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0635:                        + "<form id='form1' method='post'>\n"
0636:                        + "    <table><tr><td>\n"
0637:                        + "        <input type='text' name='textfield' value='blah'/>\n"
0638:                        + "        </td><td>\n"
0639:                        + "        <input type='submit' name='button' value='foo'/>\n"
0640:                        + "        </td></tr>\n"
0641:                        + "     </table>\n"
0642:                        + "</form></body></html>";
0643:                final HtmlPage page = loadPage(htmlContent);
0644:                final MockWebConnection webConnection = getMockConnection(page);
0645:
0646:                final HtmlForm form = (HtmlForm) page
0647:                        .getHtmlElementById("form1");
0648:
0649:                final HtmlSubmitInput button = (HtmlSubmitInput) form
0650:                        .getInputByName("button");
0651:                button.click();
0652:
0653:                final List expectedParameters = Arrays.asList(new Object[] {
0654:                        new KeyValuePair("textfield", "blah"),
0655:                        new KeyValuePair("button", "foo") });
0656:                final List collectedParameters = webConnection
0657:                        .getLastParameters();
0658:
0659:                assertEquals(expectedParameters, collectedParameters);
0660:            }
0661:
0662:            /**
0663:             * @throws Exception if the test fails
0664:             */
0665:            public void testSubmit_IgnoresDisabledControls() throws Exception {
0666:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0667:                        + "<form id='form1' method='post'>\n"
0668:                        + "    <input type='text' name='textfield' value='blah' disabled />\n"
0669:                        + "    <input type='submit' name='button' value='foo'/>\n"
0670:                        + "</form></body></html>";
0671:                final HtmlPage page = loadPage(htmlContent);
0672:                final MockWebConnection webConnection = getMockConnection(page);
0673:
0674:                final HtmlForm form = (HtmlForm) page
0675:                        .getHtmlElementById("form1");
0676:
0677:                final HtmlSubmitInput button = (HtmlSubmitInput) form
0678:                        .getInputByName("button");
0679:                button.click();
0680:
0681:                final List expectedParameters = Arrays
0682:                        .asList(new Object[] { new KeyValuePair("button", "foo") });
0683:                final List collectedParameters = webConnection
0684:                        .getLastParameters();
0685:
0686:                assertEquals(expectedParameters, collectedParameters);
0687:            }
0688:
0689:            /**
0690:             * Reset buttons should not be successful controls.
0691:             * @see <a href="http://www.w3.org/TR/html4/interact/forms.html#h-17.13.2">Spec</a>
0692:             * @throws Exception if the test fails
0693:             */
0694:            public void testSubmit_IgnoresResetControls() throws Exception {
0695:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0696:                        + "<form id='form1' method='post'>\n"
0697:                        + "    <button type='reset' name='buttonreset' value='buttonreset'/>\n"
0698:                        + "    <input type='reset' name='reset' value='reset'/>\n"
0699:                        + "    <input type='submit' name='submit' value='submit'/>\n"
0700:                        + "</form></body></html>";
0701:                final HtmlPage page = loadPage(htmlContent);
0702:                final MockWebConnection webConnection = getMockConnection(page);
0703:
0704:                final HtmlForm form = (HtmlForm) page
0705:                        .getHtmlElementById("form1");
0706:
0707:                final HtmlSubmitInput button = (HtmlSubmitInput) form
0708:                        .getInputByName("submit");
0709:                button.click();
0710:
0711:                final List expectedParameters = Arrays
0712:                        .asList(new Object[] { new KeyValuePair("submit",
0713:                                "submit") });
0714:                final List collectedParameters = webConnection
0715:                        .getLastParameters();
0716:
0717:                assertEquals(expectedParameters, collectedParameters);
0718:            }
0719:
0720:            /**
0721:             * @throws Exception if the test fails
0722:             */
0723:            public void testSubmit_CheckboxClicked() throws Exception {
0724:                final String htmlContent = "<html><head><title>foo</title>\n"
0725:                        + "<script language='javascript'>\n"
0726:                        + "function setFormat()\n"
0727:                        + "{\n"
0728:                        + "    if (document.form1.Format.checked) {\n"
0729:                        + "        document.form1.Format.value='html';\n"
0730:                        + "    } else {\n"
0731:                        + "        document.form1.Format.value='plain';\n"
0732:                        + "    }\n"
0733:                        + "}\n"
0734:                        + "</script>\n"
0735:                        + "</head><body>\n"
0736:                        + "<form name='form1' id='form1' method='post'>\n"
0737:                        + "    <input type=checkbox name=Format value='' onclick='setFormat()'>\n"
0738:                        + "    <input type='submit' name='button' value='foo'/>\n"
0739:                        + "</form></body></html>";
0740:
0741:                final HtmlPage page1 = loadPage(htmlContent);
0742:                final MockWebConnection webConnection1 = getMockConnection(page1);
0743:                final HtmlForm form1 = (HtmlForm) page1
0744:                        .getHtmlElementById("form1");
0745:                final HtmlSubmitInput button1 = (HtmlSubmitInput) form1
0746:                        .getInputByName("button");
0747:
0748:                final HtmlPage page2 = (HtmlPage) button1.click();
0749:                final List collectedParameters1 = webConnection1
0750:                        .getLastParameters();
0751:                final List expectedParameters1 = Arrays
0752:                        .asList(new Object[] { new KeyValuePair("button", "foo") });
0753:
0754:                final MockWebConnection webConnection2 = getMockConnection(page2);
0755:                final HtmlForm form2 = (HtmlForm) page2
0756:                        .getHtmlElementById("form1");
0757:                final HtmlCheckBoxInput checkBox2 = (HtmlCheckBoxInput) form2
0758:                        .getInputByName("Format");
0759:                final HtmlSubmitInput button2 = (HtmlSubmitInput) form2
0760:                        .getInputByName("button");
0761:
0762:                checkBox2.click();
0763:                button2.click();
0764:                final List collectedParameters2 = webConnection2
0765:                        .getLastParameters();
0766:                final List expectedParameters2 = Arrays.asList(new Object[] {
0767:                        new KeyValuePair("Format", "html"),
0768:                        new KeyValuePair("button", "foo") });
0769:
0770:                assertEquals(expectedParameters1, collectedParameters1);
0771:                assertEquals(expectedParameters2, collectedParameters2);
0772:            }
0773:
0774:            /**
0775:             * @throws Exception if the test fails
0776:             */
0777:            public void testGetInputByValue() throws Exception {
0778:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0779:                        + "<form id='form1'>\n"
0780:                        + "    <input type='submit' name='button' value='xxx'/>\n"
0781:                        + "    <input type='text' name='textfield' value='foo'/>\n"
0782:                        + "    <input type='submit' name='button1' value='foo'/>\n"
0783:                        + "    <input type='reset' name='button2' value='foo'/>\n"
0784:                        + "    <input type='submit' name='button' value='bar'/>\n"
0785:                        + "</form></body></html>";
0786:                final HtmlPage page = loadPage(htmlContent);
0787:
0788:                final HtmlForm form = (HtmlForm) page
0789:                        .getHtmlElementById("form1");
0790:
0791:                final List allInputsByValue = form.getInputsByValue("foo");
0792:                final ListIterator iterator = allInputsByValue.listIterator();
0793:                while (iterator.hasNext()) {
0794:                    final HtmlInput input = (HtmlInput) iterator.next();
0795:                    iterator.set(input.getNameAttribute());
0796:                }
0797:
0798:                final String[] expectedInputs = { "textfield", "button1",
0799:                        "button2" };
0800:                assertEquals("Get all", expectedInputs, allInputsByValue);
0801:                assertEquals(Collections.EMPTY_LIST, form
0802:                        .getInputsByValue("none-matching"));
0803:
0804:                assertEquals("Get first", "button", form.getInputByValue("bar")
0805:                        .getNameAttribute());
0806:                try {
0807:                    form.getInputByValue("none-matching");
0808:                    fail("Expected ElementNotFoundException");
0809:                } catch (final ElementNotFoundException e) {
0810:                    // Expected path.
0811:                }
0812:            }
0813:
0814:            /**
0815:             * Test that {@link HtmlForm#getTextAreaByName(String)} returns
0816:             * the first textarea with the given name.
0817:             *
0818:             * @throws Exception If the test page can't be loaded.
0819:             */
0820:            public void testGetTextAreaByName() throws Exception {
0821:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0822:                        + "<form id='form1'>\n"
0823:                        + "    <textarea id='ta1_1' name='ta1'>hello</textarea>\n"
0824:                        + "    <textarea id='ta1_2' name='ta1'>world</textarea>\n"
0825:                        + "    <textarea id='ta2_1' name='ta2'>!</textarea>\n"
0826:                        + "</form></body></html>";
0827:                final HtmlPage page = loadPage(htmlContent);
0828:
0829:                final HtmlForm form = (HtmlForm) page
0830:                        .getHtmlElementById("form1");
0831:
0832:                assertEquals("First textarea with name 'ta1'", form
0833:                        .getHtmlElementById("ta1_1"), form
0834:                        .getTextAreaByName("ta1"));
0835:                assertEquals("First textarea with name 'ta2'", form
0836:                        .getHtmlElementById("ta2_1"), form
0837:                        .getTextAreaByName("ta2"));
0838:
0839:                try {
0840:                    form.getTextAreaByName("ta3");
0841:                    fail("Expected ElementNotFoundException as there is no textarea with name 'ta3'");
0842:                } catch (final ElementNotFoundException e) {
0843:                    // pass: exception is expected
0844:                }
0845:            }
0846:
0847:            /**
0848:             * Test that {@link HtmlForm#getButtonByName(String)} returns
0849:             * the first button with the given name.
0850:             *
0851:             * @throws Exception If the test page can't be loaded.
0852:             */
0853:            public void testGetButtonByName() throws Exception {
0854:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0855:                        + "<form id='form1'>\n"
0856:                        + "    <button id='b1_1' name='b1' value='hello' type='button'/>\n"
0857:                        + "    <button id='b1_2' name='b1' value='world' type='button'/>\n"
0858:                        + "    <button id='b2_1' name='b2' value='!' type='button'/>\n"
0859:                        + "</form></body></html>";
0860:                final HtmlPage page = loadPage(htmlContent);
0861:
0862:                final HtmlForm form = (HtmlForm) page
0863:                        .getHtmlElementById("form1");
0864:
0865:                assertEquals("First button with name 'b1'", form
0866:                        .getHtmlElementById("b1_1"), form.getButtonByName("b1"));
0867:                assertEquals("First button with name 'b2'", form
0868:                        .getHtmlElementById("b2_1"), form.getButtonByName("b2"));
0869:
0870:                try {
0871:                    form.getTextAreaByName("b3");
0872:                    fail("Expected ElementNotFoundException as there is no button with name 'b3'");
0873:                } catch (final ElementNotFoundException e) {
0874:                    // pass: exception is expected
0875:                }
0876:            }
0877:
0878:            /**
0879:             * Test that the result of the form will get loaded into the window
0880:             * specified by "target"
0881:             * @throws Exception If the test fails.
0882:             */
0883:            public void testSubmitToTargetWindow() throws Exception {
0884:                final String firstContent = "<html><head><title>first</title></head><body>\n"
0885:                        + "<form id='form1' target='window2' action='"
0886:                        + URL_SECOND
0887:                        + "' method='post'>\n"
0888:                        + "    <input type='submit' name='button' value='push me'/>\n"
0889:                        + "</form></body></html>";
0890:                final WebClient client = new WebClient();
0891:
0892:                final MockWebConnection webConnection = new MockWebConnection(
0893:                        client);
0894:                webConnection.setResponse(URL_FIRST, firstContent);
0895:                webConnection.setResponseAsGenericHtml(URL_SECOND, "second");
0896:                client.setWebConnection(webConnection);
0897:
0898:                final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
0899:                final HtmlForm form = (HtmlForm) page
0900:                        .getHtmlElementById("form1");
0901:
0902:                final HtmlSubmitInput button = (HtmlSubmitInput) form
0903:                        .getInputByName("button");
0904:                final HtmlPage secondPage = (HtmlPage) button.click();
0905:                assertEquals("window2", secondPage.getEnclosingWindow()
0906:                        .getName());
0907:
0908:                final WebWindow firstWindow = client.getCurrentWindow();
0909:                assertEquals("first window name", "", firstWindow.getName());
0910:                assertSame(page, firstWindow.getEnclosedPage());
0911:            }
0912:
0913:            /**
0914:             * @throws Exception if the test fails
0915:             */
0916:            public void testSubmit_SelectHasNoOptions() throws Exception {
0917:                final String htmlContent = "<html><body><form name='form' method='GET' action='action.html'>\n"
0918:                        + "<select name='select'>\n"
0919:                        + "</select>\n"
0920:                        + "</form></body></html>";
0921:                final HtmlPage page = loadPage(htmlContent);
0922:                final MockWebConnection webConnection = getMockConnection(page);
0923:
0924:                final HtmlPage secondPage = (HtmlPage) page.getFormByName(
0925:                        "form").submit((SubmittableElement) null);
0926:
0927:                assertNotNull(secondPage);
0928:                assertEquals("parameters", Collections.EMPTY_LIST,
0929:                        webConnection.getLastParameters());
0930:            }
0931:
0932:            /**
0933:             * @throws Exception if the test fails
0934:             */
0935:            public void testSubmit_SelectOptionWithoutValueAttribute()
0936:                    throws Exception {
0937:                final String htmlContent = "<html><body><form name='form' action='action.html'>\n"
0938:                        + "<select name='select'>\n"
0939:                        + "     <option>first value</option>\n"
0940:                        + "     <option selected>second value</option>\n"
0941:                        + "</select>\n" + "</form></body></html>";
0942:                final HtmlPage page = loadPage(htmlContent);
0943:                final HtmlPage secondPage = (HtmlPage) page.getFormByName(
0944:                        "form").submit((SubmittableElement) null);
0945:
0946:                assertNotNull(secondPage);
0947:                assertEquals(page.getWebResponse().getUrl().toExternalForm()
0948:                        + "action.html?select=second+value", secondPage
0949:                        .getWebResponse().getUrl());
0950:            }
0951:
0952:            /**
0953:             * At one point this test was failing because deeply nested inputs weren't getting picked up.
0954:             * @throws Exception if the test fails
0955:             */
0956:            public void testSubmit_DeepInputs() throws Exception {
0957:                final String htmlContent = "<html><form method='post' action=''>\n"
0958:                        + "<table><tr><td>\n"
0959:                        + "<input value='NOT_SUBMITTED' name='data' type='text'/>\n"
0960:                        + "</td></tr></table>\n"
0961:                        + "<input id='submitButton' name='submit' type='submit'/>\n"
0962:                        + "</form></html>";
0963:                final HtmlPage page = loadPage(htmlContent);
0964:                final MockWebConnection webConnection = getMockConnection(page);
0965:
0966:                final HtmlInput submitButton = (HtmlInput) page
0967:                        .getHtmlElementById("submitButton");
0968:                submitButton.click();
0969:
0970:                final List collectedParameters = webConnection
0971:                        .getLastParameters();
0972:                final List expectedParameters = Arrays.asList(new Object[] {
0973:                        new KeyValuePair("data", "NOT_SUBMITTED"),
0974:                        new KeyValuePair("submit", "Submit Query") });
0975:                assertEquals(expectedParameters, collectedParameters);
0976:            }
0977:
0978:            /**
0979:             * Test order of submitted parameters matches order of elements in form.
0980:             * @throws Exception if the test fails
0981:             */
0982:            public void testSubmit_FormElementOrder() throws Exception {
0983:                final String htmlContent = "<html><head></head><body><form method='post' action=''>\n"
0984:                        + "<input type='submit' name='dispatch' value='Save' id='submitButton'>\n"
0985:                        + "<input type='hidden' name='dispatch' value='TAB'>\n"
0986:                        + "</form></body></html>";
0987:                final WebClient client = new WebClient();
0988:
0989:                final MockWebConnection webConnection = new MockWebConnection(
0990:                        client);
0991:                webConnection.setDefaultResponse(htmlContent);
0992:                client.setWebConnection(webConnection);
0993:
0994:                final WebRequestSettings settings = new WebRequestSettings(
0995:                        URL_GARGOYLE, SubmitMethod.POST);
0996:
0997:                final HtmlPage page = (HtmlPage) client.getPage(settings);
0998:                final HtmlInput submitButton = (HtmlInput) page
0999:                        .getHtmlElementById("submitButton");
1000:                submitButton.click();
1001:
1002:                final List collectedParameters = webConnection
1003:                        .getLastParameters();
1004:                final List expectedParameters = Arrays.asList(new Object[] {
1005:                        new KeyValuePair("dispatch", "Save"),
1006:                        new KeyValuePair("dispatch", "TAB"), });
1007:                assertCollectionsEqual(expectedParameters, collectedParameters);
1008:            }
1009:
1010:            /**
1011:             * Test the 'Referer' HTTP header
1012:             * @throws Exception on test failure
1013:             */
1014:            public void testSubmit_refererHeader() throws Exception {
1015:                final String firstContent = "<html><head><title>First</title></head><body>\n"
1016:                        + "<form method='post' action='"
1017:                        + URL_SECOND
1018:                        + "'>\n"
1019:                        + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
1020:                        + "</body></html>";
1021:                final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
1022:
1023:                final WebClient client = new WebClient();
1024:
1025:                final MockWebConnection webConnection = new MockWebConnection(
1026:                        client);
1027:                webConnection.setResponse(URL_FIRST, firstContent);
1028:                webConnection.setResponse(URL_SECOND, secondContent);
1029:
1030:                client.setWebConnection(webConnection);
1031:
1032:                final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
1033:                final HtmlSubmitInput button = (HtmlSubmitInput) firstPage
1034:                        .getHtmlElementById("button");
1035:
1036:                button.click();
1037:
1038:                final Map lastAdditionalHeaders = webConnection
1039:                        .getLastAdditionalHeaders();
1040:                assertEquals(URL_FIRST.toString(), lastAdditionalHeaders
1041:                        .get("Referer"));
1042:            }
1043:
1044:            /**
1045:             * Simulate a bug report where using JavaScript to submit a form that contains a
1046:             * JavaScript action causes a an "IllegalArgumentException: javascript urls can only
1047:             * be used to load content into frames and iframes."
1048:             *
1049:             * @throws Exception if the test fails
1050:             */
1051:            public void testJSSubmit_JavaScriptAction() throws Exception {
1052:                final String htmlContent = "<html><head><title>First</title></head>\n"
1053:                        + "<body onload='document.getElementById(\"aForm\").submit()'>\n"
1054:                        + "<form id='aForm' action='javascript:alert(\"clicked\")'"
1055:                        + "</form>\n" + "</body></html>";
1056:
1057:                final String[] expectedAlerts = { "clicked" };
1058:                createTestPageForRealBrowserIfNeeded(htmlContent,
1059:                        expectedAlerts);
1060:
1061:                final List collectedAlerts = new ArrayList();
1062:                loadPage(htmlContent, collectedAlerts);
1063:
1064:                assertEquals(expectedAlerts, collectedAlerts);
1065:            }
1066:
1067:            /**
1068:             * @throws Exception if the test fails
1069:             */
1070:            public void testUrlAfterSubmit() throws Exception {
1071:                testUrlAfterSubmit("get", "foo",
1072:                        "foo?textField=foo&nonAscii=Flo%DFfahrt&button=foo");
1073:                // for a get submit, query parameters in action are lost in browsers
1074:                testUrlAfterSubmit("get", "foo?foo=12",
1075:                        "foo?textField=foo&nonAscii=Flo%DFfahrt&button=foo");
1076:                testUrlAfterSubmit("post", "foo", "foo");
1077:                testUrlAfterSubmit("post", "foo?foo=12", "foo?foo=12");
1078:                testUrlAfterSubmit("post", "", "");
1079:                testUrlAfterSubmit("post", "?a=1&b=2", "?a=1&b=2");
1080:                testUrlAfterSubmit(new URL("http://first?a=1&b=2"), "post", "",
1081:                        "");
1082:
1083:            }
1084:
1085:            /**
1086:             * @throws Exception if the test fails
1087:             */
1088:            public void testUrlAfterSubmitWithAnchor() throws Exception {
1089:                testUrlAfterSubmit("get", "foo#anchor",
1090:                        "foo?textField=foo&nonAscii=Flo%DFfahrt&button=foo#anchor");
1091:                testUrlAfterSubmit("get", "foo?foo=12#anchor",
1092:                        "foo?textField=foo&nonAscii=Flo%DFfahrt&button=foo#anchor");
1093:                testUrlAfterSubmit("post", "foo#anchor", "foo#anchor");
1094:                testUrlAfterSubmit("post", "foo?foo=12#anchor",
1095:                        "foo?foo=12#anchor");
1096:            }
1097:
1098:            /**
1099:             * Utility for {@link #testUrlAfterSubmit()}
1100:             * @param method The form method to use
1101:             * @param action The form action to use
1102:             * @param expectedUrlEnd The expected url
1103:             * @throws Exception if the test fails
1104:             */
1105:            private void testUrlAfterSubmit(final URL url, final String method,
1106:                    final String action, final String expectedUrlEnd)
1107:                    throws Exception {
1108:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
1109:                        + "<form id='form1' method='"
1110:                        + method
1111:                        + "' action='"
1112:                        + action
1113:                        + "'>\n"
1114:                        + "<input type='text' name='textField' value='foo'/>\n"
1115:                        + "<input type='text' name='nonAscii' value='Flo\u00DFfahrt'/>\n"
1116:                        + "<input id='submitButton' type='submit' name='button' value='foo'/>\n"
1117:                        + "<input type='button' name='inputButton' value='foo'/>\n"
1118:                        + "<button type='button' name='buttonButton' value='foo'/>\n"
1119:                        + "</form></body></html>";
1120:                final HtmlPage page = loadPage(htmlContent, null, url);
1121:                final HtmlForm form = (HtmlForm) page
1122:                        .getHtmlElementById("form1");
1123:                final Page page2 = form.submit((HtmlSubmitInput) page
1124:                        .getHtmlElementById("submitButton"));
1125:
1126:                assertEquals(url.toExternalForm() + expectedUrlEnd, page2
1127:                        .getWebResponse().getUrl());
1128:            }
1129:
1130:            /**
1131:             * Utility for {@link #testUrlAfterSubmit()}. Calls {@link #testUrlAfterSubmit(URL, String, String, String)} with
1132:             * URL_GARGOYLE.
1133:             * @param method The form method to use
1134:             * @param action The form action to use
1135:             * @param expectedUrlEnd The expected url
1136:             * @throws Exception if the test fails
1137:             */
1138:            private void testUrlAfterSubmit(final String method,
1139:                    final String action, final String expectedUrlEnd)
1140:                    throws Exception {
1141:                testUrlAfterSubmit(URL_GARGOYLE, method, action, expectedUrlEnd);
1142:            }
1143:
1144:            /**
1145:             * Utility for {@link #testUrlAfterSubmit()}
1146:             * @throws Exception if the test fails
1147:             */
1148:            public void testSubmitRequestCharset() throws Exception {
1149:                testSubmitRequestCharset("UTF-8", null, null, "UTF-8");
1150:                testSubmitRequestCharset(null, "UTF-8", null, "UTF-8");
1151:                testSubmitRequestCharset("ISO-8859-1", null, "UTF-8", "UTF-8");
1152:                testSubmitRequestCharset("ISO-8859-1", null,
1153:                        "UTF-8, ISO-8859-1", "UTF-8");
1154:                testSubmitRequestCharset("UTF-8", null, "ISO-8859-1 UTF-8",
1155:                        "ISO-8859-1");
1156:                testSubmitRequestCharset("ISO-8859-1", null,
1157:                        "UTF-8, ISO-8859-1", "UTF-8");
1158:            }
1159:
1160:            /**
1161:             * Utility for {@link #testSubmitRequestCharset()}
1162:             * @param headerCharset the charset for the content type header if not null
1163:             * @param metaCharset the charset for the meta http-equiv content type tag if not null
1164:             * @param formCharset the charset for the form's accept-charset attribute if not null
1165:             * @param expectedRequestCharset the charset expected for the form submission
1166:             * @throws Exception if the test fails
1167:             */
1168:            private void testSubmitRequestCharset(final String headerCharset,
1169:                    final String metaCharset, final String formCharset,
1170:                    final String expectedRequestCharset) throws Exception {
1171:
1172:                final String formAcceptCharset;
1173:                if (formCharset == null) {
1174:                    formAcceptCharset = "";
1175:                } else {
1176:                    formAcceptCharset = " accept-charset='" + formCharset + "'";
1177:                }
1178:
1179:                final String metaContentType;
1180:                if (metaCharset == null) {
1181:                    metaContentType = "";
1182:                } else {
1183:                    metaContentType = "<meta http-equiv='Content-Type' content='text/html; charset="
1184:                            + metaCharset + "'>\n";
1185:                }
1186:
1187:                final String content = "<html><head><title>foo</title>\n"
1188:                        + metaContentType
1189:                        + "</head><body>\n"
1190:                        + "<form name='form1' method='post' action='foo'"
1191:                        + formAcceptCharset
1192:                        + ">\n"
1193:                        + "<input type='text' name='textField' value='foo'/>\n"
1194:                        + "<input type='text' name='nonAscii' value='Flo\u00DFfahrt'/>\n"
1195:                        + "<input type='submit' name='button' value='foo'/>\n"
1196:                        + "</form></body></html>";
1197:                final WebClient client = new WebClient();
1198:
1199:                final MockWebConnection webConnection = new MockWebConnection(
1200:                        client);
1201:                client.setWebConnection(webConnection);
1202:
1203:                String contentType = "text/html";
1204:                if (headerCharset != null) {
1205:                    contentType += ";charset=" + headerCharset;
1206:                }
1207:                webConnection.setDefaultResponse(content, 200, "ok",
1208:                        contentType);
1209:                final HtmlPage page = (HtmlPage) client.getPage(URL_GARGOYLE);
1210:
1211:                final String firstPageEncoding = StringUtils.defaultString(
1212:                        metaCharset, headerCharset);
1213:                assertEquals(firstPageEncoding, page.getPageEncoding());
1214:
1215:                final HtmlForm form = page.getFormByName("form1");
1216:                form.getInputByName("button").click();
1217:
1218:                assertEquals(expectedRequestCharset, webConnection
1219:                        .getLastWebRequestSettings().getCharset());
1220:            }
1221:
1222:            /**
1223:             * @throws Exception If the test fails
1224:             */
1225:            public void testSumbit_submitInputValue() throws Exception {
1226:                testSumbit_submitInputValue(BrowserVersion.INTERNET_EXPLORER_6_0);
1227:                //test FF separately as value is not to DEFAULT_VALUE if not specified.
1228:                testSumbit_submitInputValue(BrowserVersion.FIREFOX_2);
1229:            }
1230:
1231:            private void testSumbit_submitInputValue(
1232:                    final BrowserVersion browserVersion) throws Exception {
1233:                final String html = "<html><head>\n" + "</head>\n" + "<body>\n"
1234:                        + "<form action='" + URL_SECOND + "'>\n"
1235:                        + "  <input id='myButton' type='submit' name='Save'>\n"
1236:                        + "</form>\n" + "</body></html>";
1237:
1238:                final HtmlPage firstPage = loadPage(browserVersion, html, null);
1239:                final HtmlSubmitInput submitInput = (HtmlSubmitInput) firstPage
1240:                        .getHtmlElementById("myButton");
1241:                final HtmlPage secondPage = (HtmlPage) (submitInput).click();
1242:                assertEquals(URL_SECOND + "?Save=Submit+Query", secondPage
1243:                        .getWebResponse().getUrl());
1244:            }
1245:
1246:            /**
1247:             * Regression test for
1248:             *   https://sourceforge.net/tracker/index.php?func=detail&aid=1822108&group_id=47038&atid=448266
1249:             *
1250:             * @throws Exception If the test fails
1251:             */
1252:            public void testSubmitWithOnClickThatReturnsFalse()
1253:                    throws Exception {
1254:                if (notYetImplemented()) {
1255:                    return;
1256:                }
1257:                final String firstContent = "<html><head><title>foo</title></haed><body>\n"
1258:                        + "<form action='"
1259:                        + URL_SECOND
1260:                        + "' method='post'>\n"
1261:                        + "  <input type='submit' name='mySubmit' onClick='document.forms[0].submit(); return false;'>\n"
1262:                        + "</form></body></html>";
1263:
1264:                final String secondContent = "<html><head><title>foo</title><script>\n"
1265:                        + "  Number.prototype.gn = false;\n"
1266:                        + "  function test() {\n"
1267:                        + "    var v = 0;\n"
1268:                        + "    alert(typeof v);\n"
1269:                        + "    alert(v.gn);\n"
1270:                        + "  }\n"
1271:                        + "</script></head><body onload='test()'>\n"
1272:                        + "</body></html>";
1273:
1274:                final String[] expectedAlerts = { "number", "false" };
1275:                final List collectedAlerts = new ArrayList();
1276:                final WebClient client = new WebClient();
1277:                client.setAlertHandler(new CollectingAlertHandler(
1278:                        collectedAlerts));
1279:                final MockWebConnection conn = new MockWebConnection(client);
1280:                conn.setResponse(URL_FIRST, firstContent);
1281:                conn.setResponse(URL_SECOND, secondContent);
1282:                client.setWebConnection(conn);
1283:
1284:                final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
1285:                final HtmlForm form = (HtmlForm) page.getForms().get(0);
1286:                final HtmlSubmitInput submit = (HtmlSubmitInput) form
1287:                        .getInputByName("mySubmit");
1288:                submit.click();
1289:                assertEquals(expectedAlerts, collectedAlerts);
1290:            }
1291:
1292:            /**
1293:             * Tests that submitting a form without parameters does not trail the URL with a question mark (IE only).
1294:             *
1295:             * @throws Exception If the test fails
1296:             */
1297:            public void testSubmitURLWithoutParameters() throws Exception {
1298:                testSubmitURLWithoutParameters(
1299:                        BrowserVersion.INTERNET_EXPLORER_7_0, URL_SECOND
1300:                                .toExternalForm());
1301:                testSubmitURLWithoutParameters(
1302:                        BrowserVersion.INTERNET_EXPLORER_6_0, URL_SECOND
1303:                                .toExternalForm() + '?');
1304:                testSubmitURLWithoutParameters(BrowserVersion.FIREFOX_2,
1305:                        URL_SECOND.toExternalForm() + '?');
1306:            }
1307:
1308:            private void testSubmitURLWithoutParameters(
1309:                    final BrowserVersion browserVersion,
1310:                    final String expectedURL) throws Exception {
1311:                final String firstContent = "<html><head><title>foo</title></haed><body>\n"
1312:                        + "<form action='"
1313:                        + URL_SECOND
1314:                        + "'>\n"
1315:                        + "  <input type='submit' name='mySubmit' onClick='document.forms[0].submit(); return false;'>\n"
1316:                        + "</form></body></html>";
1317:
1318:                final String secondContent = "<html><head><title>second</title></head></html>";
1319:
1320:                final List collectedAlerts = new ArrayList();
1321:                final WebClient client = new WebClient(browserVersion);
1322:                client.setAlertHandler(new CollectingAlertHandler(
1323:                        collectedAlerts));
1324:                final MockWebConnection conn = new MockWebConnection(client);
1325:                conn.setResponse(URL_FIRST, firstContent);
1326:                conn.setDefaultResponse(secondContent);
1327:                client.setWebConnection(conn);
1328:
1329:                final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
1330:                final HtmlForm form = (HtmlForm) page.getForms().get(0);
1331:                final HtmlSubmitInput submit = (HtmlSubmitInput) form
1332:                        .getInputByName("mySubmit");
1333:                final HtmlPage secondPage = (HtmlPage) submit.click();
1334:                assertEquals(expectedURL, secondPage.getWebResponse().getUrl());
1335:            }
1336:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.