View Javadoc

1   /*
2    * Copyright (c) 2002-2011 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package com.gargoylesoftware.htmlunit.html;
16  
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertSame;
19  
20  import java.io.File;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.junit.Rule;
28  import org.junit.Test;
29  import org.junit.rules.TemporaryFolder;
30  import org.junit.runner.RunWith;
31  
32  import com.gargoylesoftware.htmlunit.BrowserRunner;
33  import com.gargoylesoftware.htmlunit.HttpMethod;
34  import com.gargoylesoftware.htmlunit.MockWebConnection;
35  import com.gargoylesoftware.htmlunit.Page;
36  import com.gargoylesoftware.htmlunit.WebTestCase;
37  
38  /**
39   * Tests for {@link HtmlSelect}.
40   *
41   * @version $Revision: 6469 $
42   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
43   * @author Mike Williams
44   * @author Marc Guillemot
45   * @author Ahmed Ashour
46   */
47  @RunWith(BrowserRunner.class)
48  public class HtmlSelectTest extends WebTestCase {
49      /** JUnit rule must be public fields :-(. */
50      @Rule
51      public TemporaryFolder tmpFolderProvider_ = new TemporaryFolder();
52  
53      /**
54       * Test the good path of submitting a select.
55       * @exception Exception If the test fails
56       */
57      @Test
58      public void testSelect() throws Exception {
59          final String htmlContent = "<html><head><title>foo</title></head><body>\n"
60              + "<form id='form1'><select name='select1'>\n"
61              + "<option value='option1'>Option1</option>\n"
62              + "<option value='option2' selected='selected'>Option2</option>\n"
63              + "<option value='option3'>Option3</option>\n"
64              + "</select>\n"
65              + "<input type='submit' name='button' value='foo'/>\n"
66              + "</form></body></html>";
67          final HtmlPage page = loadPage(htmlContent);
68          final MockWebConnection webConnection = getMockConnection(page);
69  
70          final HtmlForm form = page.getHtmlElementById("form1");
71  
72          final HtmlSelect select = form.getSelectsByName("select1").get(0);
73          final HtmlSubmitInput button = form.getInputByName("button");
74  
75          // Test that the select is being correctly identified as a submittable element
76          assertEquals(Arrays.asList(new Object[] {select, button}), form.getSubmittableElements(button));
77  
78          // Test that the correct value is being passed back up to the server
79          final HtmlPage secondPage = button.click();
80  
81          assertEquals("url", getDefaultUrl() + "?select1=option2&button=foo",
82                  secondPage.getWebResponse().getWebRequest().getUrl());
83          assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
84          assertNotNull(secondPage);
85      }
86  
87      /**
88       * Tests submitting the select with no options selected.
89       * @exception Exception If the test fails
90       */
91      @Test
92      public void testSelect_MultipleSelectNoneSelected() throws Exception {
93          final String htmlContent = "<html><head><title>foo</title></head><body>\n"
94              + "<form id='form1'><select name='select1' multiple>\n"
95              + "<option value='option1'>Option1</option>\n"
96              + "<option value='option2'>Option2</option>\n"
97              + "<option value='option3'>Option3</option>\n"
98              + "</select>\n"
99              + "<input type='submit' name='button' value='foo'/>\n"
100             + "</form></body></html>";
101         final HtmlPage page = loadPage(htmlContent);
102         final MockWebConnection webConnection = getMockConnection(page);
103 
104         final HtmlForm form = page.getHtmlElementById("form1");
105 
106         final HtmlSelect select = form.getSelectsByName("select1").get(0);
107         assertNotNull(select);
108 
109         final HtmlSubmitInput button = form.getInputByName("button");
110 
111         // Test that the correct value is being passed back up to the server
112         final HtmlPage secondPage = button.click();
113 
114         assertEquals("url", getDefaultUrl() + "?button=foo", secondPage.getWebResponse().getWebRequest().getUrl());
115         assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
116         assertNotNull(secondPage);
117     }
118 
119     /**
120      * Tests changing the selected option.
121      * @exception Exception If the test fails
122      */
123     @Test
124     public void testSelect_ChangeSelectedOption_SingleSelect() throws Exception {
125         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
126             + "<form id='form1'><select name='select1'>\n"
127             + "<option value='option1' selected='selected'>Option1</option>\n"
128             + "<option value='option2'>Option2</option>\n"
129             + "<option value='option3'>Option3</option>\n"
130             + "</select>\n"
131             + "<input type='submit' name='button' value='foo'/>\n"
132             + "</form></body></html>";
133         final HtmlPage page = loadPage(htmlContent);
134         final MockWebConnection webConnection = getMockConnection(page);
135 
136         final HtmlForm form = page.getHtmlElementById("form1");
137 
138         final HtmlSelect select = form.getSelectsByName("select1").get(0);
139         final HtmlSubmitInput button = form.getInputByName("button");
140 
141         // Change the value
142         select.setSelectedAttribute("option3", true);
143 
144         // Test that the correct value is being passed back up to the server
145         final HtmlPage secondPage = (HtmlPage) button.click();
146 
147         assertEquals("url", getDefaultUrl() + "?select1=option3&button=foo",
148                 secondPage.getWebResponse().getWebRequest().getUrl());
149         assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
150         assertNotNull(secondPage);
151     }
152 
153     /**
154      * Tests changing the selected option.
155      * @exception Exception If the test fails
156      */
157     @Test
158     public void testSelect_ChangeSelectedOption_MultipleSelect() throws Exception {
159         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
160             + "<form id='form1'><select name='select1' multiple='multiple'>\n"
161             + "<option value='option1' selected='selected'>Option1</option>\n"
162             + "<option value='option2'>Option2</option>\n"
163             + "<option value='option3'>Option3</option>\n"
164             + "</select>\n"
165             + "<input type='submit' name='button' value='foo'/>\n"
166             + "</form></body></html>";
167         final HtmlPage page = loadPage(htmlContent);
168         final MockWebConnection webConnection = getMockConnection(page);
169 
170         final HtmlForm form = page.getHtmlElementById("form1");
171 
172         final HtmlSelect select = form.getSelectsByName("select1").get(0);
173         final HtmlSubmitInput button = form.getInputByName("button");
174 
175         // Change the value
176         select.setSelectedAttribute("option3", true);
177         select.setSelectedAttribute("option2", true);
178 
179         // Test that the correct value is being passed back up to the server
180         final HtmlPage secondPage = button.click();
181 
182         assertEquals("url", getDefaultUrl() + "?select1=option1&select1=option2&select1=option3&button=foo",
183                 secondPage.getWebResponse().getWebRequest().getUrl());
184         assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
185         assertNotNull(secondPage);
186     }
187 
188     /**
189      * Tests multiple selected options on multiple select lists.
190      * @exception Exception If the test fails
191      */
192     @Test
193     public void testSelect_MultipleSelectMultipleSelected() throws Exception {
194         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
195             + "<form id='form1'><select name='select1' multiple>\n"
196             + "<option value='option1' selected='selected'>Option1</option>\n"
197             + "<option value='option2'>Option2</option>\n"
198             + "<option value='option3' selected='selected'>Option3</option>\n"
199             + "</select>\n"
200             + "<input type='submit' name='button' value='foo'/>\n"
201             + "</form></body></html>";
202         final HtmlPage page = loadPage(htmlContent);
203 
204         final HtmlForm form = page.getHtmlElementById("form1");
205 
206         final HtmlSelect select = form.getSelectsByName("select1").get(0);
207         final List<HtmlOption> expected = new ArrayList<HtmlOption>();
208         expected.add(select.getOptionByValue("option1"));
209         expected.add(select.getOptionByValue("option3"));
210 
211         assertEquals(expected, select.getSelectedOptions());
212     }
213 
214     /**
215      * Test multiple selected options on single select lists. This is erroneous HTML, but
216      * browsers simply use the last option.
217      *
218      * @exception Exception If the test fails
219      */
220     @Test
221     public void testSelect_SingleSelectMultipleSelected() throws Exception {
222         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
223             + "<form id='form1'><select name='select1'>\n"
224             + "<option value='option1' selected='selected'>Option1</option>\n"
225             + "<option value='option2'>Option2</option>\n"
226             + "<option value='option3' selected='selected'>Option3</option>\n"
227             + "</select>\n"
228             + "<input type='submit' name='button' value='foo'/>\n"
229             + "</form></body></html>";
230         final HtmlPage page = loadPage(htmlContent);
231 
232         final HtmlForm form = page.getHtmlElementById("form1");
233 
234         final HtmlSelect select = form.getSelectsByName("select1").get(0);
235         final List<HtmlOption> expected = new ArrayList<HtmlOption>();
236         expected.add(select.getOptionByValue("option3"));
237 
238         assertEquals(expected, select.getSelectedOptions());
239     }
240 
241     /**
242      * Test no selected options on single select lists. This is erroneous HTML, but
243      * browsers simply assume the first one to be selected
244      *
245      * @exception Exception If the test fails
246      */
247     @Test
248     public void testSelect_SingleSelectNoneSelected() throws Exception {
249         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
250             + "<form id='form1'><select name='select1'>\n"
251             + "<option value='option1'>Option1</option>\n"
252             + "<option value='option2'>Option2</option>\n"
253             + "<option value='option3'>Option3</option>\n"
254             + "</select>\n"
255             + "<input type='submit' name='button' value='foo'/>\n"
256             + "</form></body></html>";
257         final HtmlPage page = loadPage(htmlContent);
258 
259         final HtmlForm form = page.getHtmlElementById("form1");
260 
261         final HtmlSelect select = form.getSelectsByName("select1").get(0);
262         final List<HtmlOption> expected = new ArrayList<HtmlOption>();
263         expected.add(select.getOptionByValue("option1"));
264 
265         assertEquals(expected, select.getSelectedOptions());
266     }
267 
268     /**
269      * Tests no selected options on single select lists with a size > 1.
270      * @exception Exception If the test fails
271      */
272     @Test
273     public void testSelect_SingleSelectNoneSelectedButSizeGreaterThanOne() throws Exception {
274         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
275             + "<form>\n"
276             + "<select name='select1' size='2' id='mySelect'>\n"
277             + "<option value='option1'>Option1</option>\n"
278             + "<option value='option2'>Option2</option>\n"
279             + "<option value='option3'>Option3</option>\n"
280             + "</select>\n"
281             + "</form></body></html>";
282 
283         final HtmlPage page = loadPage(htmlContent);
284 
285         final HtmlSelect select = page.getHtmlElementById("mySelect");
286 
287         assertEquals(Collections.EMPTY_LIST, select.getSelectedOptions());
288     }
289 
290     /**
291      * Tests changing the selected option.
292      * @exception Exception If the test fails
293      */
294     @Test
295     public void testSetSelected_IllegalValue() throws Exception {
296         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
297             + "<form id='form1'><select name='select1'>\n"
298             + "<option value='option1' selected='selected'>Option1</option>\n"
299             + "<option value='option2'>Option2</option>\n"
300             + "<option value='option3'>Option3</option>\n"
301             + "</select>\n"
302             + "<select name='select2'>\n"
303             + "</select>\n"
304             + "<input type='submit' name='button' value='foo'/>\n"
305             + "</form></body></html>";
306 
307         final HtmlPage page = loadPage(htmlContent);
308         final HtmlForm form = page.getHtmlElementById("form1");
309         final HtmlSelect select = form.getSelectByName("select1");
310 
311         select.setSelectedAttribute("missingOption", true);
312     }
313 
314     /**
315      * @throws Exception if the test fails
316      */
317     @Test
318     public void testGetOptions() throws Exception {
319         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
320             + "<form id='form1'><select name='select1'>\n"
321             + "<option value='option1' selected='selected'>Option1</option>\n"
322             + "<option value='option2'>Option2</option>\n"
323             + "<optgroup label='group1'>\n"
324             + "    <option value='option3'>Option3</option>\n"
325             + "</optgroup>\n"
326             + "</select>\n"
327             + "<input type='submit' name='button' value='foo'/>\n"
328             + "</form></body></html>";
329         final HtmlPage page = loadPage(htmlContent);
330 
331         final HtmlForm form = page.getHtmlElementById("form1");
332 
333         final HtmlSelect select = form.getSelectsByName("select1").get(0);
334 
335         final List<HtmlOption> expectedOptions = new ArrayList<HtmlOption>();
336         expectedOptions.add(select.getOptionByValue("option1"));
337         expectedOptions.add(select.getOptionByValue("option2"));
338         expectedOptions.add(select.getOptionByValue("option3"));
339 
340         assertEquals(expectedOptions, select.getOptions());
341     }
342 
343     /**
344      * @throws Exception if the test fails
345      */
346     @Test
347     public void testSelect_OptionMultiple_NoValueOnAttribute() throws Exception {
348         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
349             + "<form id='form1'><select name='select1' id='select1' multiple>\n"
350             + "<option value='option1'>Option1</option>\n"
351             + "<option value='option2' >Option2</option>\n"
352             + "<option value='option3'>Option3</option>\n"
353             + "</select>\n"
354             + "<input type='submit' name='button' value='foo'/>\n"
355             + "</form></body></html>";
356         final HtmlPage page = loadPage(htmlContent);
357 
358         final HtmlSelect select = page.getHtmlElementById("select1");
359         assertTrue(select.isMultipleSelectEnabled());
360     }
361 
362     /**
363      * @throws Exception if the test fails
364      */
365     @Test
366     public void testGetOptionByValue() throws Exception {
367         final String htmlContent = "<html><head><title>foo</title></head><body><form id='form1'>\n"
368             + "<select name='select1'>\n"
369             + "    <option value='option1'>s1o1</option>\n"
370             + "    <option value='option2'>s1o2</option>\n"
371             + "</select>\n"
372             + "<select name='select2'>\n"
373             + "    <option value='option1'>s2o1</option>\n"
374             + "    <option value='option2'>s2o2</option>\n"
375             + "    <option>s2o3</option>\n"
376             + "</select>\n"
377             + "<input type='submit' name='button' value='foo'/>\n"
378             + "</form></body></html>";
379         final HtmlPage page = loadPage(htmlContent);
380 
381         final HtmlForm form = page.getHtmlElementById("form1");
382 
383         final HtmlSelect select = form.getSelectsByName("select2").get(0);
384         assertEquals("s2o2", select.getOptionByValue("option2").asText());
385 
386         assertEquals(select.getOption(2), select.getOptionByValue("s2o3"));
387     }
388 
389     /**
390      * @throws Exception if the test fails
391      */
392     @Test
393     public void testSelect_SetSelected_OnChangeHandler() throws Exception {
394         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
395             + "<form id='form1'><select name='select1' onChange='alert(\"changing\")'>\n"
396             + "<option value='option1' selected='selected'>Option1</option>\n"
397             + "<option value='option2'>Option2</option>\n"
398             + "<option value='option3'>Option3</option>\n"
399             + "</select>\n"
400             + "<input type='submit' name='button' value='foo'/>\n"
401             + "</form></body></html>";
402         final List<String> collectedAlerts = new ArrayList<String>();
403         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
404 
405         final HtmlForm form = page.getHtmlElementById("form1");
406 
407         final HtmlSelect select = form.getSelectsByName("select1").get(0);
408 
409         // Change the value
410         select.setSelectedAttribute("option3", true);
411 
412         final String[] expectedAlerts = {"changing"};
413         assertEquals(expectedAlerts, collectedAlerts);
414     }
415 
416     /**
417      * @throws Exception if the test fails
418      */
419     @Test
420     public void testSetSelectionOnOptionWithNoName() throws Exception {
421         final String htmlContent = "<html><body><form name='form' method='GET' action='action.html'>\n"
422             + "<select name='select' multiple size='5'>\n"
423             + "<option value='1'>111</option>\n"
424             + "<option id='option2'>222</option>\n"
425             + "</select>\n"
426             + "</form></body></html>";
427         final List<String> collectedAlerts = new ArrayList<String>();
428         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
429 
430         final HtmlOption option = page.getHtmlElementById("option2");
431         option.setSelected(true);
432     }
433 
434     private void checkOptions(final HtmlSelect select) {
435         final List<HtmlOption> options = select.getOptions();
436         if (options.isEmpty()) {
437             assertNull(select.getFirstChild());
438             assertNull(select.getLastChild());
439         }
440         else {
441             assertEquals(options.get(0), select.getFirstChild());
442             assertEquals(options.get(options.size() - 1), select.getLastChild());
443         }
444     }
445 
446     /** @throws Exception if the test fails */
447     @Test
448     public void testRemoveOptionsFromSelect() throws Exception {
449         final String htmlContent = "<html><body><form name='form' method='GET' action='action.html'>\n"
450             + "<select name='select' id='theSelect'>"
451             + "<option value='a'>111</option>"
452             + "<option value='b'>222</option>"
453             + "<option value='c'>333</option>"
454             + "<option value='d'>444</option>"
455             + "</select>\n"
456             + "</form></body></html>";
457         final HtmlPage page = loadPage(htmlContent);
458 
459         final HtmlSelect theSelect = page.getHtmlElementById("theSelect");
460         assertNotNull(theSelect);
461 
462         assertEquals(4, theSelect.getOptions().size());
463         assertEquals("a", theSelect.getOption(0).getValueAttribute());
464         assertEquals("b", theSelect.getOption(1).getValueAttribute());
465         assertEquals("c", theSelect.getOption(2).getValueAttribute());
466         assertEquals("d", theSelect.getOption(3).getValueAttribute());
467 
468         // remove from the middle
469         theSelect.getOption(1).remove();
470         checkOptions(theSelect);
471         assertEquals(3, theSelect.getOptions().size());
472         assertEquals("a", theSelect.getOption(0).getValueAttribute());
473         assertEquals("c", theSelect.getOption(1).getValueAttribute());
474         assertEquals("d", theSelect.getOption(2).getValueAttribute());
475 
476         // remove from the end
477         theSelect.getOption(2).remove();
478         checkOptions(theSelect);
479         assertEquals(2, theSelect.getOptions().size());
480         assertEquals("a", theSelect.getOption(0).getValueAttribute());
481         assertEquals("c", theSelect.getOption(1).getValueAttribute());
482 
483         // remove from the front
484         theSelect.getOption(0).remove();
485         checkOptions(theSelect);
486         assertEquals(1, theSelect.getOptions().size());
487         assertEquals("c", theSelect.getOption(0).getValueAttribute());
488 
489         // remove from the last one
490         theSelect.getOption(0).remove();
491         checkOptions(theSelect);
492         assertEquals(0, theSelect.getOptions().size());
493     }
494 
495     /** @throws Exception if the test fails */
496     @Test
497     public void testEditOptions() throws Exception {
498         final String htmlContent = "<html><body><form name='form' method='GET' action='action.html'>\n"
499             + "<select name='select' id='theSelect'>\n"
500             + "<option value='a'>111</option>\n"
501             + "<option value='b'>222</option>\n"
502             + "<option value='c'>333</option>\n"
503             + "</select>\n"
504             + "</form></body></html>";
505         final HtmlPage page = loadPage(htmlContent);
506 
507         final HtmlSelect theSelect = page.getHtmlElementById("theSelect");
508 
509         assertNotNull(theSelect);
510         assertEquals(3, theSelect.getOptions().size());
511 
512         appendOption(theSelect, "d");
513         assertEquals(4, theSelect.getOptions().size());
514         assertEquals("d", theSelect.getOption(3).getValueAttribute());
515 
516         theSelect.setOptionSize(1);
517         assertEquals(1, theSelect.getOptions().size());
518         assertEquals("a", theSelect.getOption(0).getValueAttribute());
519 
520         appendOption(theSelect, "x");
521         assertEquals(2, theSelect.getOptions().size());
522         assertEquals("x", theSelect.getOption(1).getValueAttribute());
523     }
524 
525     void appendOption(final HtmlSelect select, final String value) {
526         final HtmlOption option = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME).createElement(
527                 select.getPage(), HtmlOption.TAG_NAME, null);
528         option.setValueAttribute(value);
529         option.setLabelAttribute(value);
530         select.appendOption(option);
531     }
532 
533     /**
534      * Test that asText() returns a blank string if nothing is selected.
535      *
536      * @exception Exception If the test fails
537      */
538     @Test
539     public void asTextWhenNothingSelected() throws Exception {
540         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
541             + "<form>\n"
542             + "<select name='select1' size='1' id='mySelect'>\n"
543             + "</select>\n"
544             + "</form></body></html>";
545 
546         final HtmlPage page = loadPage(htmlContent);
547 
548         final HtmlSelect select = page.getHtmlElementById("mySelect");
549 
550         assertEquals("", select.asText());
551     }
552 
553     /**
554      * Verifies that asText() returns all options when multiple options are selectable, instead of just
555      * the selected ones.
556      * @throws Exception if an error occurs
557      */
558     @Test
559     public void asTextWithMultipleSelect() throws Exception {
560         final String html = "<html><body><form>\n"
561             + "<select name='a' multiple>\n"
562             + "<option value='1'>foo</option>\n"
563             + "<option value='2'>bar</option>\n"
564             + "<option value='3'>baz</option>\n"
565             + "</select>\n"
566             + "</form></body></html>";
567         final HtmlPage page = loadPage(html);
568         final HtmlSelect select = (HtmlSelect) page.getDocumentElement().getHtmlElementsByTagName("select").get(0);
569         assertEquals("foo\nbar\nbaz", select.asText());
570     }
571 
572     /**
573      * Test that setSelectedAttribute returns the right page.
574      *
575      * @exception Exception If the test fails
576      */
577     @Test
578     public void testSetSelectedAttributeReturnedPage() throws Exception {
579         final String content = "<html><head><title>foo</title>\n"
580             + "<script>\n"
581             + "function test() {\n"
582             + "  document.getElementById('iframe').src = 'about:blank';\n"
583             + "}\n"
584             + "</script>\n"
585             + "</head><body>\n"
586             + "<form>\n"
587             + "<select name='select1' size='1' id='mySelect' onchange='test()'>\n"
588             + "<option value='option1'>option 1</option>\n"
589             + "<option value='option2'>option 2</option>\n"
590             + "</select>\n"
591             + "</form>\n"
592             + "<iframe id='iframe' src='about:blank'></iframe>\n"
593             + "</body></html>";
594 
595         final HtmlPage page = loadPage(content);
596 
597         final HtmlSelect select = page.getHtmlElementById("mySelect");
598         final HtmlOption option = select.getOptionByValue("option2");
599         final Page page2 = select.setSelectedAttribute(option, true);
600         assertEquals(page, page2);
601     }
602 
603     /**
604      * @throws Exception if the test fails
605      */
606     @Test
607     public void testOnChangeResultPage() throws Exception {
608         final String htmlContent
609             = "<html><head><title>foo</title></head><body>\n"
610             + "<form id='form1'>\n"
611             + "<select name='select1' id='select1' onchange='location=\"about:blank\"'>\n"
612             + "     <option id='option1'>Option1</option>\n"
613             + "     <option id='option2' selected>Number Two</option>\n"
614             + "</select>\n"
615             + "</form></body></html>";
616 
617         final HtmlPage page = loadPage(htmlContent);
618 
619         final HtmlOption option1 = page.getHtmlElementById("option1");
620         final HtmlPage page2 = option1.click();
621         assertEquals("about:blank", page2.getWebResponse().getWebRequest().getUrl());
622     }
623 
624     /**
625      * @throws Exception if the test fails
626      */
627     @Test
628     public void onChange_resultPage_newCurrentWindow() throws Exception {
629         final String htmlContent
630             = "<html><head><title>foo</title></head><body>\n"
631             + "<form id='form1'>\n"
632             + "<select name='select1' id='select1' onchange='open(\"about:blank\", \"_blank\")'>\n"
633             + "     <option id='option1'>Option1</option>\n"
634             + "     <option id='option2' selected>Number Two</option>\n"
635             + "</select>\n"
636             + "</form></body></html>";
637 
638         final HtmlPage page = loadPage(htmlContent);
639 
640         final HtmlSelect select = page.getHtmlElementById("select1");
641         final HtmlOption option1 = page.getHtmlElementById("option1");
642         final HtmlPage page2 = select.setSelectedAttribute(option1, true);
643         assertEquals("about:blank", page2.getUrl());
644     }
645 
646     /**
647      * @throws Exception if the test fails
648      */
649     @Test
650     public void asXml_size() throws Exception {
651         final String content = "<html><head><title>foo</title></head>\n"
652             + "<body>\n"
653             + "<select/>\n"
654             + "</body></html>";
655 
656         final HtmlPage page = loadPage(content);
657         assertEquals(-1, page.asXml().indexOf("size"));
658     }
659 
660     /**
661      * @throws Exception if the test fails
662      */
663     @Test
664     public void select_focus() throws Exception {
665         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
666             + "<form id='form1'>\n"
667             + "<select name='select1' id='select1' multiple onfocus='alert(\"focus\")'>\n"
668             + "<option value='option1'>Option1</option>\n"
669             + "<option value='option2'>Option2</option>\n"
670             + "<option value='option3' selected>Option3</option>\n"
671             + "</select>\n"
672             + "<input type='submit' name='button' value='foo'/>\n"
673             + "</form></body></html>";
674 
675         final List<String> collectedAlerts = new ArrayList<String>();
676         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
677         assertEquals(Collections.emptyList(), collectedAlerts);
678 
679         final HtmlSelect select = page.getHtmlElementById("select1");
680         assertNull(page.getFocusedElement());
681         select.getOption(0).setSelected(true);
682         assertSame(select, page.getFocusedElement());
683 
684         final String[] expectedAlerts = {"focus"};
685         assertEquals(expectedAlerts, collectedAlerts);
686     }
687 
688     /**
689      * @throws Exception if the test fails
690      */
691     @Test
692     public void testGetOptionByText() throws Exception {
693         final String html = "<html><head><title>foo</title></head><body><form id='form1'>\n"
694             + "<select name='select1'>\n"
695             + "    <option value='option1'>s1o1</option>\n"
696             + "    <option value='option2'>s1o2</option>\n"
697             + "</select>\n"
698             + "<select name='select2'>\n"
699             + "    <option value='option1'>s2o1</option>\n"
700             + "    <option value='option2'>s2o2</option>\n"
701             + "    <option>s2o3</option>\n"
702             + "</select>\n"
703             + "<input type='submit' name='button' value='foo'/>\n"
704             + "</form></body></html>";
705         final HtmlPage page = loadPage(html);
706 
707         final HtmlForm form = page.getHtmlElementById("form1");
708 
709         final HtmlSelect select = form.getSelectsByName("select2").get(0);
710         assertEquals("s2o2", select.getOptionByText("s2o2").asText());
711 
712         assertEquals(select.getOption(2), select.getOptionByText("s2o3"));
713     }
714 
715     /**
716      * @throws Exception if the test fails
717      */
718     @Test
719     public void savePageSavesSelectedOption() throws Exception {
720         final String content = "<html><body>\n"
721             + "<form action=''>\n"
722             + "  <select id='main'>\n"
723             + "    <option value='1'>option 1</option>\n"
724             + "    <option value='2'>option 2</option>\n"
725             + "    <option value='3' selected>option 3</option>\n"
726             + "  </select>\n"
727             + "</form>\n"
728             + "<script>\n"
729             + "var oSelect = document.getElementById('main');\n"
730             + "oSelect.options[1].selected = true;\n"
731             + "alert(oSelect.options[1].getAttribute('selected'));\n"
732             + "</script>\n"
733             + "</body></html>";
734 
735         final HtmlPage page = loadPage(content);
736         final HtmlSelect select = (HtmlSelect) page.getElementById("main");
737         assertEquals("option 2", select.getSelectedOptions().get(0).getText());
738 
739         // save the file and reload it
740         final File file = new File(tmpFolderProvider_.newFolder("tmp"), "test.html");
741         page.save(file);
742         final String html2 = FileUtils.readFileToString(file, "UTF-8");
743         final HtmlPage page2 = loadPage(html2);
744         final HtmlSelect select2 = (HtmlSelect) page2.getElementById("main");
745         assertEquals("option 2", select2.getSelectedOptions().get(0).getText());
746     }
747 }