Source Code Cross Referenced for HtmlSelectTest.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) 


001:        /*
002:         * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003:         *
004:         * Redistribution and use in source and binary forms, with or without
005:         * modification, are permitted provided that the following conditions are met:
006:         *
007:         * 1. Redistributions of source code must retain the above copyright notice,
008:         *    this list of conditions and the following disclaimer.
009:         * 2. Redistributions in binary form must reproduce the above copyright notice,
010:         *    this list of conditions and the following disclaimer in the documentation
011:         *    and/or other materials provided with the distribution.
012:         * 3. The end-user documentation included with the redistribution, if any, must
013:         *    include the following acknowledgment:
014:         *
015:         *       "This product includes software developed by Gargoyle Software Inc.
016:         *        (http://www.GargoyleSoftware.com/)."
017:         *
018:         *    Alternately, this acknowledgment may appear in the software itself, if
019:         *    and wherever such third-party acknowledgments normally appear.
020:         * 4. The name "Gargoyle Software" must not be used to endorse or promote
021:         *    products derived from this software without prior written permission.
022:         *    For written permission, please contact info@GargoyleSoftware.com.
023:         * 5. Products derived from this software may not be called "HtmlUnit", nor may
024:         *    "HtmlUnit" appear in their name, without prior written permission of
025:         *    Gargoyle Software Inc.
026:         *
027:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028:         * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029:         * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030:         * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031:         * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033:         * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034:         * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035:         * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036:         * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037:         */
038:        package com.gargoylesoftware.htmlunit.html;
039:
040:        import java.util.ArrayList;
041:        import java.util.Arrays;
042:        import java.util.Collections;
043:        import java.util.List;
044:
045:        import com.gargoylesoftware.htmlunit.KeyValuePair;
046:        import com.gargoylesoftware.htmlunit.MockWebConnection;
047:        import com.gargoylesoftware.htmlunit.Page;
048:        import com.gargoylesoftware.htmlunit.SubmitMethod;
049:        import com.gargoylesoftware.htmlunit.WebTestCase;
050:
051:        /**
052:         * Tests for {@link HtmlSelect}.
053:         *
054:         * @version $Revision: 2132 $
055:         * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
056:         * @author Mike Williams
057:         * @author Marc Guillemot
058:         * @author Ahmed Ashour
059:         */
060:        public class HtmlSelectTest extends WebTestCase {
061:
062:            /**
063:             * Create an instance
064:             *
065:             * @param name Name of the test
066:             */
067:            public HtmlSelectTest(final String name) {
068:                super (name);
069:            }
070:
071:            /**
072:             * Test the good path of submitting a select
073:             *
074:             * @exception Exception If the test fails
075:             */
076:            public void testSelect() throws Exception {
077:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
078:                        + "<form id='form1'><select name='select1'>\n"
079:                        + "<option value='option1'>Option1</option>\n"
080:                        + "<option value='option2' selected='selected'>Option2</option>\n"
081:                        + "<option value='option3'>Option3</option>\n"
082:                        + "</select>\n"
083:                        + "<input type='submit' name='button' value='foo'/>\n"
084:                        + "</form></body></html>";
085:                final HtmlPage page = loadPage(htmlContent);
086:                final MockWebConnection webConnection = getMockConnection(page);
087:
088:                final HtmlForm form = (HtmlForm) page
089:                        .getHtmlElementById("form1");
090:
091:                final HtmlSelect select = (HtmlSelect) form.getSelectsByName(
092:                        "select1").get(0);
093:                final HtmlSubmitInput button = (HtmlSubmitInput) form
094:                        .getInputByName("button");
095:
096:                // Test that the select is being correctly identified as a submittable element
097:                assertCollectionsEqual(Arrays.asList(new Object[] { select,
098:                        button }), form.getSubmittableElements(button));
099:
100:                // Test that the correct value is being passed back up to the server
101:                final HtmlPage secondPage = (HtmlPage) button.click();
102:
103:                assertEquals("url", URL_GARGOYLE.toExternalForm()
104:                        + "?select1=option2&button=foo", secondPage
105:                        .getWebResponse().getUrl());
106:                assertEquals("method", SubmitMethod.GET, webConnection
107:                        .getLastMethod());
108:                assertNotNull(secondPage);
109:            }
110:
111:            /**
112:             * Test submitting the select with no options selected
113:             *
114:             * @exception Exception If the test fails
115:             */
116:            public void testSelect_MultipleSelectNoneSelected()
117:                    throws Exception {
118:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
119:                        + "<form id='form1'><select name='select1' multiple>\n"
120:                        + "<option value='option1'>Option1</option>\n"
121:                        + "<option value='option2'>Option2</option>\n"
122:                        + "<option value='option3'>Option3</option>\n"
123:                        + "</select>\n"
124:                        + "<input type='submit' name='button' value='foo'/>\n"
125:                        + "</form></body></html>";
126:                final HtmlPage page = loadPage(htmlContent);
127:                final MockWebConnection webConnection = getMockConnection(page);
128:
129:                final HtmlForm form = (HtmlForm) page
130:                        .getHtmlElementById("form1");
131:
132:                final HtmlSelect select = (HtmlSelect) form.getSelectsByName(
133:                        "select1").get(0);
134:                assertNotNull(select);
135:
136:                final HtmlSubmitInput button = (HtmlSubmitInput) form
137:                        .getInputByName("button");
138:
139:                // Test that the correct value is being passed back up to the server
140:                final HtmlPage secondPage = (HtmlPage) button.click();
141:
142:                assertEquals("url", URL_GARGOYLE.toExternalForm()
143:                        + "?button=foo", secondPage.getWebResponse().getUrl());
144:                assertEquals("method", SubmitMethod.GET, webConnection
145:                        .getLastMethod());
146:                assertNotNull(secondPage);
147:            }
148:
149:            /**
150:             * Test changing the selected option
151:             *
152:             * @exception Exception If the test fails
153:             */
154:            public void testSelect_ChangeSelectedOption_SingleSelect()
155:                    throws Exception {
156:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
157:                        + "<form id='form1'><select name='select1'>\n"
158:                        + "<option value='option1' selected='selected'>Option1</option>\n"
159:                        + "<option value='option2'>Option2</option>\n"
160:                        + "<option value='option3'>Option3</option>\n"
161:                        + "</select>\n"
162:                        + "<input type='submit' name='button' value='foo'/>\n"
163:                        + "</form></body></html>";
164:                final HtmlPage page = loadPage(htmlContent);
165:                final MockWebConnection webConnection = getMockConnection(page);
166:
167:                final HtmlForm form = (HtmlForm) page
168:                        .getHtmlElementById("form1");
169:
170:                final HtmlSelect select = (HtmlSelect) form.getSelectsByName(
171:                        "select1").get(0);
172:                final HtmlSubmitInput button = (HtmlSubmitInput) form
173:                        .getInputByName("button");
174:
175:                // Change the value
176:                select.setSelectedAttribute("option3", true);
177:
178:                // Test that the correct value is being passed back up to the server
179:                final HtmlPage secondPage = (HtmlPage) button.click();
180:
181:                assertEquals("url", URL_GARGOYLE.toExternalForm()
182:                        + "?select1=option3&button=foo", secondPage
183:                        .getWebResponse().getUrl());
184:                assertEquals("method", SubmitMethod.GET, webConnection
185:                        .getLastMethod());
186:                assertNotNull(secondPage);
187:            }
188:
189:            /**
190:             * Test changing the selected option
191:             *
192:             * @exception Exception If the test fails
193:             */
194:            public void testSelect_ChangeSelectedOption_MultipleSelect()
195:                    throws Exception {
196:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
197:                        + "<form id='form1'><select name='select1' multiple='multiple'>\n"
198:                        + "<option value='option1' selected='selected'>Option1</option>\n"
199:                        + "<option value='option2'>Option2</option>\n"
200:                        + "<option value='option3'>Option3</option>\n"
201:                        + "</select>\n"
202:                        + "<input type='submit' name='button' value='foo'/>\n"
203:                        + "</form></body></html>";
204:                final HtmlPage page = loadPage(htmlContent);
205:                final MockWebConnection webConnection = getMockConnection(page);
206:
207:                final HtmlForm form = (HtmlForm) page
208:                        .getHtmlElementById("form1");
209:
210:                final HtmlSelect select = (HtmlSelect) form.getSelectsByName(
211:                        "select1").get(0);
212:                final HtmlSubmitInput button = (HtmlSubmitInput) form
213:                        .getInputByName("button");
214:
215:                // Change the value
216:                select.setSelectedAttribute("option3", true);
217:                select.setSelectedAttribute("option2", true);
218:
219:                // Test that the correct value is being passed back up to the server
220:                final HtmlPage secondPage = (HtmlPage) button.click();
221:
222:                assertEquals(
223:                        "url",
224:                        URL_GARGOYLE.toExternalForm()
225:                                + "?select1=option1&select1=option2&select1=option3&button=foo",
226:                        secondPage.getWebResponse().getUrl());
227:                assertEquals("method", SubmitMethod.GET, webConnection
228:                        .getLastMethod());
229:                assertNotNull(secondPage);
230:            }
231:
232:            /**
233:             * Test multiple selected options on multiple select lists
234:             *
235:             * @exception Exception If the test fails
236:             */
237:            public void testSelect_MultipleSelectMultipleSelected()
238:                    throws Exception {
239:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
240:                        + "<form id='form1'><select name='select1' multiple>\n"
241:                        + "<option value='option1' selected='selected'>Option1</option>\n"
242:                        + "<option value='option2'>Option2</option>\n"
243:                        + "<option value='option3' selected='selected'>Option3</option>\n"
244:                        + "</select>\n"
245:                        + "<input type='submit' name='button' value='foo'/>\n"
246:                        + "</form></body></html>";
247:                final HtmlPage page = loadPage(htmlContent);
248:
249:                final HtmlForm form = (HtmlForm) page
250:                        .getHtmlElementById("form1");
251:
252:                final HtmlSelect select = (HtmlSelect) form.getSelectsByName(
253:                        "select1").get(0);
254:                final List expected = new ArrayList();
255:                expected.add(select.getOptionByValue("option1"));
256:                expected.add(select.getOptionByValue("option3"));
257:
258:                assertEquals(expected, select.getSelectedOptions());
259:            }
260:
261:            /**
262:             * Test multiple selected options on single select lists. This is erroneous HTML, but
263:             * browsers simply use the last option.
264:             *
265:             * @exception Exception If the test fails
266:             */
267:            public void testSelect_SingleSelectMultipleSelected()
268:                    throws Exception {
269:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
270:                        + "<form id='form1'><select name='select1'>\n"
271:                        + "<option value='option1' selected='selected'>Option1</option>\n"
272:                        + "<option value='option2'>Option2</option>\n"
273:                        + "<option value='option3' selected='selected'>Option3</option>\n"
274:                        + "</select>\n"
275:                        + "<input type='submit' name='button' value='foo'/>\n"
276:                        + "</form></body></html>";
277:                final HtmlPage page = loadPage(htmlContent);
278:
279:                final HtmlForm form = (HtmlForm) page
280:                        .getHtmlElementById("form1");
281:
282:                final HtmlSelect select = (HtmlSelect) form.getSelectsByName(
283:                        "select1").get(0);
284:                final List expected = new ArrayList();
285:                expected.add(select.getOptionByValue("option3"));
286:
287:                assertEquals(expected, select.getSelectedOptions());
288:            }
289:
290:            /**
291:             * Test no selected options on single select lists. This is erroneous HTML, but
292:             * browsers simply assume the first one to be selected
293:             *
294:             * @exception Exception If the test fails
295:             */
296:            public void testSelect_SingleSelectNoneSelected() throws Exception {
297:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
298:                        + "<form id='form1'><select name='select1'>\n"
299:                        + "<option value='option1'>Option1</option>\n"
300:                        + "<option value='option2'>Option2</option>\n"
301:                        + "<option value='option3'>Option3</option>\n"
302:                        + "</select>\n"
303:                        + "<input type='submit' name='button' value='foo'/>\n"
304:                        + "</form></body></html>";
305:                final HtmlPage page = loadPage(htmlContent);
306:
307:                final HtmlForm form = (HtmlForm) page
308:                        .getHtmlElementById("form1");
309:
310:                final HtmlSelect select = (HtmlSelect) form.getSelectsByName(
311:                        "select1").get(0);
312:                final List expected = new ArrayList();
313:                expected.add(select.getOptionByValue("option1"));
314:
315:                assertEquals(expected, select.getSelectedOptions());
316:            }
317:
318:            /**
319:             * Test no selected options on single select lists with a size > 1
320:             *
321:             * @exception Exception If the test fails
322:             */
323:            public void testSelect_SingleSelectNoneSelectedButSizeGreaterThanOne()
324:                    throws Exception {
325:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
326:                        + "<form>\n"
327:                        + "<select name='select1' size='2' id='mySelect'>\n"
328:                        + "<option value='option1'>Option1</option>\n"
329:                        + "<option value='option2'>Option2</option>\n"
330:                        + "<option value='option3'>Option3</option>\n"
331:                        + "</select>\n" + "</form></body></html>";
332:
333:                final HtmlPage page = loadPage(htmlContent);
334:
335:                final HtmlSelect select = (HtmlSelect) page
336:                        .getHtmlElementById("mySelect");
337:
338:                assertEquals(Collections.EMPTY_LIST, select
339:                        .getSelectedOptions());
340:            }
341:
342:            /**
343:             * Test changing the selected option
344:             * @deprecated Mark test as deprecated to avoid compiler warnings as this tests deprecated methods
345:             * @exception Exception If the test fails
346:             */
347:            public void testSetSelected_IllegalValue() throws Exception {
348:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
349:                        + "<form id='form1'><select name='select1'>\n"
350:                        + "<option value='option1' selected='selected'>Option1</option>\n"
351:                        + "<option value='option2'>Option2</option>\n"
352:                        + "<option value='option3'>Option3</option>\n"
353:                        + "</select>\n"
354:                        + "<select name='select2'>\n"
355:                        + "</select>\n"
356:                        + "<input type='submit' name='button' value='foo'/>\n"
357:                        + "</form></body></html>";
358:                final HtmlPage page = loadPage(htmlContent);
359:
360:                final HtmlForm form = (HtmlForm) page
361:                        .getHtmlElementById("form1");
362:
363:                final HtmlSelect select = form.getSelectByName("select1");
364:                final HtmlSubmitInput button = (HtmlSubmitInput) form
365:                        .getInputByName("button");
366:
367:                // Change the value
368:                try {
369:                    select.setSelectedAttribute("missingOption", true);
370:                    fail("Expected IllegalArgumentException");
371:                } catch (final IllegalArgumentException e) {
372:                    // Expected path
373:                }
374:
375:                select.fakeSelectedAttribute("newOption");
376:
377:                final HtmlSelect select2 = form.getSelectByName("select2");
378:                select2.fakeSelectedAttribute("fakedValue");
379:
380:                // Test that the correct value is being passed back up to the server
381:                final HtmlPage secondPage = (HtmlPage) button.click();
382:
383:                final List expectedParameters = new ArrayList();
384:                expectedParameters
385:                        .add(new KeyValuePair("select1", "newOption"));
386:                expectedParameters.add(new KeyValuePair("button", "foo"));
387:
388:                assertEquals(
389:                        "url",
390:                        "http://www.gargoylesoftware.com/?select1=newOption&select2=fakedValue&button=foo",
391:                        secondPage.getWebResponse().getUrl());
392:                assertNotNull(secondPage);
393:            }
394:
395:            /**
396:             * @throws Exception if the test fails
397:             */
398:            public void testGetOptions() throws Exception {
399:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
400:                        + "<form id='form1'><select name='select1'>\n"
401:                        + "<option value='option1' selected='selected'>Option1</option>\n"
402:                        + "<option value='option2'>Option2</option>\n"
403:                        + "<optgroup label='group1'>\n"
404:                        + "    <option value='option3'>Option3</option>\n"
405:                        + "</optgroup>\n"
406:                        + "</select>\n"
407:                        + "<input type='submit' name='button' value='foo'/>\n"
408:                        + "</form></body></html>";
409:                final HtmlPage page = loadPage(htmlContent);
410:
411:                final HtmlForm form = (HtmlForm) page
412:                        .getHtmlElementById("form1");
413:
414:                final HtmlSelect select = (HtmlSelect) form.getSelectsByName(
415:                        "select1").get(0);
416:
417:                final List expectedOptions = new ArrayList();
418:                expectedOptions.add(select.getOptionByValue("option1"));
419:                expectedOptions.add(select.getOptionByValue("option2"));
420:                expectedOptions.add(select.getOptionByValue("option3"));
421:
422:                assertEquals(expectedOptions, select.getOptions());
423:            }
424:
425:            /**
426:             * @throws Exception if the test fails
427:             */
428:            public void testSelect_OptionMultiple_NoValueOnAttribute()
429:                    throws Exception {
430:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
431:                        + "<form id='form1'><select name='select1' id='select1' multiple>\n"
432:                        + "<option value='option1'>Option1</option>\n"
433:                        + "<option value='option2' >Option2</option>\n"
434:                        + "<option value='option3'>Option3</option>\n"
435:                        + "</select>\n"
436:                        + "<input type='submit' name='button' value='foo'/>\n"
437:                        + "</form></body></html>";
438:                final HtmlPage page = loadPage(htmlContent);
439:
440:                final HtmlSelect select = (HtmlSelect) page
441:                        .getHtmlElementById("select1");
442:                assertTrue(select.isMultipleSelectEnabled());
443:            }
444:
445:            /**
446:             * @throws Exception if the test fails
447:             */
448:            public void testGetOptionByValue() throws Exception {
449:                final String htmlContent = "<html><head><title>foo</title></head><body><form id='form1'>\n"
450:                        + "<select name='select1'>\n"
451:                        + "    <option value='option1'>s1o1</option>\n"
452:                        + "    <option value='option2'>s1o2</option>\n"
453:                        + "</select>\n"
454:                        + "<select name='select2'>\n"
455:                        + "    <option value='option1'>s2o1</option>\n"
456:                        + "    <option value='option2'>s2o2</option>\n"
457:                        + "    <option>s2o3</option>\n"
458:                        + "</select>\n"
459:                        + "<input type='submit' name='button' value='foo'/>\n"
460:                        + "</form></body></html>";
461:                final HtmlPage page = loadPage(htmlContent);
462:
463:                final HtmlForm form = (HtmlForm) page
464:                        .getHtmlElementById("form1");
465:
466:                final HtmlSelect select = (HtmlSelect) form.getSelectsByName(
467:                        "select2").get(0);
468:                assertEquals("s2o2", select.getOptionByValue("option2")
469:                        .asText());
470:
471:                assertEquals(select.getOption(2), select
472:                        .getOptionByValue("s2o3"));
473:            }
474:
475:            /**
476:             * @throws Exception if the test fails
477:             */
478:            public void testSelect_SetSelected_OnChangeHandler()
479:                    throws Exception {
480:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
481:                        + "<form id='form1'><select name='select1' onChange='alert(\"changing\")'>\n"
482:                        + "<option value='option1' selected='selected'>Option1</option>\n"
483:                        + "<option value='option2'>Option2</option>\n"
484:                        + "<option value='option3'>Option3</option>\n"
485:                        + "</select>\n"
486:                        + "<input type='submit' name='button' value='foo'/>\n"
487:                        + "</form></body></html>";
488:                final List collectedAlerts = new ArrayList();
489:                final HtmlPage page = loadPage(htmlContent, collectedAlerts);
490:
491:                final HtmlForm form = (HtmlForm) page
492:                        .getHtmlElementById("form1");
493:
494:                final HtmlSelect select = (HtmlSelect) form.getSelectsByName(
495:                        "select1").get(0);
496:
497:                // Change the value
498:                select.setSelectedAttribute("option3", true);
499:
500:                final String[] expectedAlerts = { "changing" };
501:                assertEquals(expectedAlerts, collectedAlerts);
502:            }
503:
504:            /**
505:             * @throws Exception if the test fails
506:             */
507:            public void testSetSelectionOnOptionWithNoName() throws Exception {
508:                final String htmlContent = "<html><body><form name='form' method='GET' action='action.html'>\n"
509:                        + "<select name='select' multiple size='5'>\n"
510:                        + "<option value='1'>111</option>\n"
511:                        + "<option id='option2'>222</option>\n"
512:                        + "</select>\n"
513:                        + "</form></body></html>";
514:                final List collectedAlerts = new ArrayList();
515:                final HtmlPage page = loadPage(htmlContent, collectedAlerts);
516:
517:                final HtmlOption option = (HtmlOption) page
518:                        .getHtmlElementById("option2");
519:                option.setSelected(true);
520:            }
521:
522:            private void checkOptions(final HtmlSelect select) {
523:                final List options = select.getOptions();
524:                if (options.isEmpty()) {
525:                    assertNull(select.getFirstDomChild());
526:                    assertNull(select.getLastDomChild());
527:                } else {
528:                    assertEquals(options.get(0), select.getFirstDomChild());
529:                    assertEquals(options.get(options.size() - 1), select
530:                            .getLastDomChild());
531:                }
532:            }
533:
534:            /** @throws Exception if the test fails */
535:            public void testRemoveOptionsFromSelect() throws Exception {
536:                final String htmlContent = "<html><body><form name='form' method='GET' action='action.html'>\n"
537:                        + "<select name='select' id='theSelect'>"
538:                        + "<option value='a'>111</option>"
539:                        + "<option value='b'>222</option>"
540:                        + "<option value='c'>333</option>"
541:                        + "<option value='d'>444</option>"
542:                        + "</select>\n"
543:                        + "</form></body></html>";
544:                final HtmlPage page = loadPage(htmlContent);
545:
546:                final HtmlSelect theSelect = (HtmlSelect) page
547:                        .getHtmlElementById("theSelect");
548:                assertNotNull(theSelect);
549:
550:                assertEquals(4, theSelect.getOptions().size());
551:                assertEquals("a", theSelect.getOption(0).getValueAttribute());
552:                assertEquals("b", theSelect.getOption(1).getValueAttribute());
553:                assertEquals("c", theSelect.getOption(2).getValueAttribute());
554:                assertEquals("d", theSelect.getOption(3).getValueAttribute());
555:
556:                // remove from the middle
557:                theSelect.getOption(1).remove();
558:                checkOptions(theSelect);
559:                assertEquals(3, theSelect.getOptions().size());
560:                assertEquals("a", theSelect.getOption(0).getValueAttribute());
561:                assertEquals("c", theSelect.getOption(1).getValueAttribute());
562:                assertEquals("d", theSelect.getOption(2).getValueAttribute());
563:
564:                // remove from the end
565:                theSelect.getOption(2).remove();
566:                checkOptions(theSelect);
567:                assertEquals(2, theSelect.getOptions().size());
568:                assertEquals("a", theSelect.getOption(0).getValueAttribute());
569:                assertEquals("c", theSelect.getOption(1).getValueAttribute());
570:
571:                // remove from the front
572:                theSelect.getOption(0).remove();
573:                checkOptions(theSelect);
574:                assertEquals(1, theSelect.getOptions().size());
575:                assertEquals("c", theSelect.getOption(0).getValueAttribute());
576:
577:                // remove from the last one
578:                theSelect.getOption(0).remove();
579:                checkOptions(theSelect);
580:                assertEquals(0, theSelect.getOptions().size());
581:            }
582:
583:            /** @throws Exception If the test fails */
584:            public void testEditOptions() throws Exception {
585:                final String htmlContent = "<html><body><form name='form' method='GET' action='action.html'>\n"
586:                        + "<select name='select' id='theSelect'>\n"
587:                        + "<option value='a'>111</option>\n"
588:                        + "<option value='b'>222</option>\n"
589:                        + "<option value='c'>333</option>\n"
590:                        + "</select>\n"
591:                        + "</form></body></html>";
592:                final HtmlPage page = loadPage(htmlContent);
593:
594:                final HtmlSelect theSelect = (HtmlSelect) page
595:                        .getHtmlElementById("theSelect");
596:
597:                assertNotNull(theSelect);
598:                assertEquals(3, theSelect.getOptions().size());
599:
600:                appendOption(theSelect, "d");
601:                assertEquals(4, theSelect.getOptions().size());
602:                assertEquals("d", theSelect.getOption(3).getValueAttribute());
603:
604:                theSelect.setOptionSize(1);
605:                assertEquals(1, theSelect.getOptions().size());
606:                assertEquals("a", theSelect.getOption(0).getValueAttribute());
607:
608:                appendOption(theSelect, "x");
609:                assertEquals(2, theSelect.getOptions().size());
610:                assertEquals("x", theSelect.getOption(1).getValueAttribute());
611:
612:            }
613:
614:            void appendOption(final HtmlSelect select, final String value) {
615:                final HtmlOption option = (HtmlOption) HTMLParser.getFactory(
616:                        HtmlOption.TAG_NAME).createElement(select.getPage(),
617:                        HtmlOption.TAG_NAME, null);
618:                option.setValueAttribute(value);
619:                option.setLabelAttribute(value);
620:                select.appendOption(option);
621:            }
622:
623:            /**
624:             * Test that asText() returns a blank string if nothing is selected.
625:             *
626:             * @exception Exception If the test fails
627:             */
628:            public void testAsTextWhenNothingSelected() throws Exception {
629:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
630:                        + "<form>\n"
631:                        + "<select name='select1' size='1' id='mySelect'>\n"
632:                        + "</select>\n" + "</form></body></html>";
633:
634:                final HtmlPage page = loadPage(htmlContent);
635:
636:                final HtmlSelect select = (HtmlSelect) page
637:                        .getHtmlElementById("mySelect");
638:
639:                assertEquals("", select.asText());
640:            }
641:
642:            /**
643:             * Verifies that asText() returns all options when multiple options are selectable, instead of just
644:             * the selected ones.
645:             * @throws Exception If an error occurs.
646:             */
647:            public void testAsTextWithMultipleSelect() throws Exception {
648:                final String html = "<html><body><form>\n"
649:                        + "<select name='a' multiple>\n"
650:                        + "<option value='1'>foo</option>\n"
651:                        + "<option value='2'>bar</option>\n"
652:                        + "<option value='3'>baz</option>\n" + "</select>\n"
653:                        + "</form></body></html>";
654:                final HtmlPage page = loadPage(html);
655:                final HtmlSelect select = (HtmlSelect) page
656:                        .getDocumentHtmlElement().getHtmlElementsByTagName(
657:                                "select").get(0);
658:                assertEquals("foo\nbar\nbaz", select.asText());
659:            }
660:
661:            /**
662:             * Test that setSelectedAttribute returns the right page.
663:             *
664:             * @exception Exception If the test fails
665:             */
666:            public void testSetSelectedAttributeReturnedPage() throws Exception {
667:                final String content = "<html><head><title>foo</title>\n"
668:                        + "<script>\n"
669:                        + "function test()\n"
670:                        + "{\n"
671:                        + "  document.getElementById('iframe').src = 'about:blank';\n"
672:                        + "}\n"
673:                        + "</script>\n"
674:                        + "</head><body>\n"
675:                        + "<form>\n"
676:                        + "<select name='select1' size='1' id='mySelect' onchange='test()'>\n"
677:                        + "<option value='option1'>option 1</option>\n"
678:                        + "<option value='option2'>option 2</option>\n"
679:                        + "</select>\n" + "</form>\n"
680:                        + "<iframe id='iframe' src='about:blank'></iframe>\n"
681:                        + "</body></html>";
682:
683:                final HtmlPage page = loadPage(content);
684:
685:                final HtmlSelect select = (HtmlSelect) page
686:                        .getHtmlElementById("mySelect");
687:                final HtmlOption option = select.getOptionByValue("option2");
688:                final Page page2 = select.setSelectedAttribute(option, true);
689:                assertEquals(page, page2);
690:            }
691:
692:            /**
693:             * @throws Exception if the test fails
694:             */
695:            public void testOnChangeResultPage() throws Exception {
696:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
697:                        + "<form id='form1'>\n"
698:                        + "<select name='select1' id='select1' onchange='location=\"about:blank\"'>\n"
699:                        + "     <option id='option1'>Option1</option>\n"
700:                        + "     <option id='option2' selected>Number Two</option>\n"
701:                        + "</select>\n" + "</form></body></html>";
702:
703:                final HtmlPage page = loadPage(htmlContent);
704:
705:                final HtmlOption option1 = (HtmlOption) page
706:                        .getHtmlElementById("option1");
707:                assertEquals("about:blank", option1.click().getWebResponse()
708:                        .getUrl());
709:            }
710:
711:            /**
712:             * @throws Exception if the test fails
713:             */
714:            public void testSelectedIndex() throws Exception {
715:                final String content = "<html><head><title>foo</title><script>\n"
716:                        + "  function test() {\n"
717:                        + "    var oSelect = document.getElementById('main');\n"
718:                        + "    var oOption = new Option('bla', 1);\n"
719:                        + "    oSelect.options[oSelect.options.length] = oOption;\n"
720:                        + "    oOption.selected = false;\n"
721:                        + "    alert(oSelect.selectedIndex);\n"
722:                        + "  }\n"
723:                        + "</script></head><body onload='test()'>\n"
724:                        + "<form action=''>\n"
725:                        + "  <select id='main'/>\n"
726:                        + "</form>\n" + "</body></html>";
727:                final String[] expectedAlerts = { "0" };
728:                final List collectedAlerts = new ArrayList();
729:                loadPage(content, collectedAlerts);
730:                assertEquals(expectedAlerts, collectedAlerts);
731:            }
732:
733:            /**
734:             * @throws Exception if the test fails
735:             */
736:            public void testAsXml_size() throws Exception {
737:                final String content = "<html><head><title>foo</title></head>\n"
738:                        + "<body>\n" + "<select/>\n" + "</body></html>";
739:
740:                final HtmlPage page = loadPage(content);
741:                assertEquals(-1, page.asXml().indexOf("size"));
742:            }
743:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.