View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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    * https://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 org.htmlunit.javascript.host.html;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.htmlunit.junit.annotation.BuggyWebDriver;
21  import org.htmlunit.junit.annotation.HtmlUnitNYI;
22  import org.junit.Test;
23  import org.junit.runner.RunWith;
24  import org.openqa.selenium.By;
25  import org.openqa.selenium.WebDriver;
26  import org.openqa.selenium.interactions.Actions;
27  
28  /**
29   * Tests for {@link HTMLOptionElement}.
30   *
31   * @author Marc Guillemot
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   * @author Frank Danek
35   */
36  @RunWith(BrowserRunner.class)
37  public class HTMLOptionElement2Test extends WebDriverTestCase {
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts("SELECT")
44      @BuggyWebDriver(CHROME = {},
45              EDGE = {})
46      //https://bugs.chromium.org/p/chromedriver/issues/detail?id=1352
47      public void clickSelect() throws Exception {
48          final String html = DOCTYPE_HTML
49                  + "<html><head>\n"
50                  + "<title>foo</title>\n"
51                  + "<script>\n"
52                  + LOG_TEXTAREA_FUNCTION
53  
54                  + "  function init() {\n"
55                  + "    var s = document.getElementById('s');\n"
56                  + "    s.addEventListener('click', handle, false);\n"
57                  + "  }\n"
58  
59                  + "  function handle(event) {\n"
60                  + "    if (event.target) {\n"
61                  + "      log(event.target.nodeName);\n"
62                  + "    } else {\n"
63                  + "      log(event.srcElement.nodeName);\n"
64                  + "    }\n"
65                  + "  }\n"
66                  + "</script></head>\n"
67  
68                  + "<body onload='init()'>\n"
69                  + "<form>\n"
70                  + LOG_TEXTAREA
71                  + "  <select id='s' size='7'>\n"
72                  + "    <option value='opt-a'>A</option>\n"
73                  + "    <option id='opt-b' value='b'>B</option>\n"
74                  + "    <option value='opt-c'>C</option>\n"
75                  + "  </select>\n"
76                  + "</form>\n"
77                  + "</body></html>";
78  
79          final WebDriver driver = loadPage2(html);
80          driver.findElement(By.id("s")).click();
81  
82          verifyTextArea2(driver, getExpectedAlerts());
83      }
84  
85      /**
86       * @throws Exception if the test fails
87       */
88      @Test
89      @Alerts(DEFAULT = {"opt-a", "opt-b"},
90              CHROME = "opt-b")
91      @BuggyWebDriver({"opt-a", "b"})
92      @HtmlUnitNYI(CHROME = {"opt-a", "b"},
93              EDGE = {"opt-a", "b"},
94              FF = {"opt-a", "b"},
95              FF_ESR = {"opt-a", "b"})
96      // TODO: Needs further investigation of clicking an option without clicking the select
97      // See the first comment in http://code.google.com/p/selenium/issues/detail?id=2131#c1
98      // Additionally, FF and Chrome drivers look buggy as they don't allow to capture
99      // what happens when running the test manually in the browser.
100     public void click2() throws Exception {
101         final String html = DOCTYPE_HTML
102                 + "<html><head>\n"
103                 + "<title>foo</title>\n"
104                 + "<script>\n"
105                 + LOG_TEXTAREA_FUNCTION
106 
107                 + "  function init() {\n"
108                 + "    s = document.getElementById('s');\n"
109                 + "    s.addEventListener('click', handle, false);\n"
110                 + "  }\n"
111 
112                 + "  function handle(event) {\n"
113                 + "    log(s.options[s.selectedIndex].value);\n"
114                 + "  }\n"
115                 + "</script></head>\n"
116 
117                 + "<body onload='init()'>\n"
118                 + "<form>\n"
119                 + LOG_TEXTAREA
120                 + "  <select id='s'>\n"
121                 + "    <option value='opt-a'>A</option>\n"
122                 + "    <option id='opt-b' value='b'>B</option>\n"
123                 + "    <option value='opt-c'>C</option>\n"
124                 + "  </select>\n"
125                 + "</form>\n"
126                 + "</body></html>";
127 
128         final WebDriver driver = loadPage2(html);
129         driver.findElement(By.id("s")).click();
130         driver.findElement(By.id("opt-b")).click();
131 
132         verifyTextArea2(driver, getExpectedAlerts());
133     }
134 
135     /**
136      * Test for the right event sequence when clicking.
137      *
138      * @throws Exception if the test fails
139      */
140     @Test
141     @Alerts({"onchange-select", "onclick-option", "onclick-select"})
142     @BuggyWebDriver(CHROME = {"onchange-select", "onclick-select"},
143             EDGE = {"onchange-select", "onclick-select"},
144             FF = {"onchange-select", "onclick-select"},
145             FF_ESR = {"onchange-select", "onclick-select"})
146     public void clickOptionEventSequence1() throws Exception {
147         final String html = DOCTYPE_HTML
148                 + "<html><head>\n"
149                 + "<script>\n"
150                 + LOG_TEXTAREA_FUNCTION
151                 + "</script></head>\n"
152 
153                 + "<body>\n"
154                 + "<form>\n"
155                 + LOG_TEXTAREA
156                 + "  <select id='s' size='2' onclick=\"log('onclick-select')\""
157                         + " onchange=\"log('onchange-select')\">\n"
158                 + "    <option id='clickId' value='a' onclick=\"log('onclick-option')\""
159                         + " onchange=\"log('onchange-option')\">A</option>\n"
160                 + "  </select>\n"
161                 + "</form>\n"
162 
163                 + "</body></html>";
164 
165         final WebDriver driver = loadPage2(html);
166 
167         driver.findElement(By.id("clickId")).click();
168 
169         verifyTextArea2(driver, getExpectedAlerts());
170     }
171 
172     /**
173      * Test for the right event sequence when clicking.
174      *
175      * @throws Exception if the test fails
176      */
177     @Test
178     @Alerts({"change-SELECT", "click-OPTION", "click-OPTION"})
179     @BuggyWebDriver(CHROME = {"change-SELECT", "click-SELECT"},
180             EDGE = {"change-SELECT", "click-SELECT"},
181             FF = {"change-SELECT", "click-SELECT"},
182             FF_ESR = {"change-SELECT", "click-SELECT"})
183     public void clickOptionEventSequence2() throws Exception {
184         final String html = DOCTYPE_HTML
185                 + "<html><head>\n"
186                 + "<script>\n"
187                 + LOG_TEXTAREA_FUNCTION
188 
189                 + "  function init() {\n"
190                 + "    var s = document.getElementById('s');\n"
191                 + "    var o = document.getElementById('clickId');\n"
192                 + "    s.addEventListener('click', handle, false);\n"
193                 + "    s.addEventListener('change', handle, false);\n"
194                 + "    o.addEventListener('click', handle, false);\n"
195                 + "    o.addEventListener('change', handle, false);\n"
196                 + "  }\n"
197 
198                 + "  function handle(event) {\n"
199                 + "    if (event.target) {\n"
200                 + "      log(event.type + '-' + event.target.nodeName);\n"
201                 + "    } else {\n"
202                 + "      log(event.type + '-' + event.srcElement.nodeName);\n"
203                 + "    }\n"
204                 + "  }\n"
205                 + "</script></head>\n"
206 
207                 + "<body onload='init()'>\n"
208                 + "<form>\n"
209                 + LOG_TEXTAREA
210                 + "  <select id='s' size='2' >\n"
211                 + "    <option id='clickId' value='a' >A</option>\n"
212                 + "  </select>\n"
213                 + "</form>\n"
214 
215                 + "</body></html>";
216 
217         final WebDriver driver = loadPage2(html);
218 
219         driver.findElement(By.id("clickId")).click();
220 
221         verifyTextArea2(driver, getExpectedAlerts());
222     }
223 
224     /**
225      * Test for the right event sequence when clicking.
226      *
227      * @throws Exception if the test fails
228      */
229     @Test
230     @Alerts({"onchange-select", "change-SELECT", "onclick-option", "click-OPTION", "onclick-select", "click-OPTION"})
231     @BuggyWebDriver(CHROME = {"onchange-select", "change-SELECT", "onclick-select", "click-SELECT"},
232             EDGE = {"onchange-select", "change-SELECT", "onclick-select", "click-SELECT"},
233             FF = {"onchange-select", "change-SELECT", "onclick-select", "click-SELECT"},
234             FF_ESR = {"onchange-select", "change-SELECT", "onclick-select", "click-SELECT"})
235     public void clickOptionEventSequence3() throws Exception {
236         final String html = DOCTYPE_HTML
237                 + "<html><head>\n"
238                 + "<script>\n"
239                 + LOG_TEXTAREA_FUNCTION
240 
241                 + "  function init() {\n"
242                 + "    var s = document.getElementById('s');\n"
243                 + "    var o = document.getElementById('clickId');\n"
244                 + "    s.addEventListener('click', handle, false);\n"
245                 + "    s.addEventListener('change', handle, false);\n"
246                 + "    o.addEventListener('click', handle, false);\n"
247                 + "    o.addEventListener('change', handle, false);\n"
248                 + "  }\n"
249 
250                 + "  function handle(event) {\n"
251                 + "    if (event.target) {\n"
252                 + "      log(event.type + '-' + event.target.nodeName);\n"
253                 + "    } else {\n"
254                 + "      log(event.type + '-' + event.srcElement.nodeName);\n"
255                 + "    }\n"
256                 + "  }\n"
257                 + "</script></head>\n"
258 
259                 + "<body onload='init()'>\n"
260                 + "<form>\n"
261                 + LOG_TEXTAREA
262                 + "  <select id='s' size='2' onclick=\"log('onclick-select')\""
263                         + " onchange=\"log('onchange-select')\">\n"
264                 + "    <option id='clickId' value='a' onclick=\"log('onclick-option')\""
265                         + " onchange=\"log('onchange-option')\">A</option>\n"
266                 + "  </select>\n"
267                 + "</form>\n"
268 
269                 + "</body></html>";
270 
271         final WebDriver driver = loadPage2(html);
272 
273         driver.findElement(By.id("clickId")).click();
274 
275         verifyTextArea2(driver, getExpectedAlerts());
276     }
277 
278     /**
279      * Regression test for 3171569: unselecting the selected option should select the first one (FF)
280      * or have no effect (IE).
281      * @throws Exception if the test fails
282      */
283     @Test
284     @Alerts({"1", "option1", "0"})
285     public void unselectResetToFirstOption() throws Exception {
286         final String html = DOCTYPE_HTML
287             + "<html><head><script>\n"
288             + LOG_TITLE_FUNCTION
289             + "function doTest() {\n"
290             + "  var sel = document.form1.select1;\n"
291             + "  log(sel.selectedIndex);\n"
292             + "  sel.options[1].selected = false;\n"
293             + "  log(sel.value);\n"
294             + "  log(sel.selectedIndex);\n"
295             + "}</script></head><body onload='doTest()'>\n"
296             + "<form name='form1'>\n"
297             + "  <select name='select1'>\n"
298             + "    <option value='option1' name='option1'>One</option>\n"
299             + "    <option value='option2' name='option2' selected>Two</option>\n"
300             + "  </select>\n"
301             + "</form>\n"
302             + "</body></html>";
303 
304         loadPageVerifyTitle2(html);
305     }
306 
307     /**
308      * @throws Exception if an error occurs
309      */
310     @Test
311     @Alerts({"1", "", "-1"})
312     public void unselectResetToFirstOption1() throws Exception {
313         final String html = DOCTYPE_HTML
314             + "<html><head><script>\n"
315             + LOG_TITLE_FUNCTION
316             + "function doTest() {\n"
317             + "  var sel = document.form1.select1;\n"
318             + "  log(sel.selectedIndex);\n"
319             + "  sel.options[1].selected = false;\n"
320             + "  log(sel.value);\n"
321             + "  log(sel.selectedIndex);\n"
322             + "}</script></head><body onload='doTest()'>\n"
323             + "<form name='form1'>\n"
324             + "  <select name='select1' size='4'>\n"
325             + "    <option value='option1' name='option1'>One</option>\n"
326             + "    <option value='option2' name='option2' selected>Two</option>\n"
327             + "  </select>\n"
328             + "</form>\n"
329             + "</body></html>";
330 
331         loadPageVerifyTitle2(html);
332     }
333 
334     /**
335      * @throws Exception if the test fails
336      */
337     @Test
338     @Alerts("1")
339     public void selectFromJSTriggersNoFocusEvent() throws Exception {
340         final String html = DOCTYPE_HTML
341             + "<html><head><script>\n"
342             + LOG_TITLE_FUNCTION
343             + "function doTest() {\n"
344             + "  var sel = document.form1.select1;\n"
345             + "  sel.options[1].selected = true;\n"
346             + "  log(sel.selectedIndex);\n"
347             + "}\n"
348             + "</script></head><body onload='doTest()'>\n"
349             + "<form name='form1'>\n"
350             + "  <select name='select1' onfocus='log(\"focus\")'>\n"
351             + "    <option value='option1' name='option1'>One</option>\n"
352             + "    <option value='option2' name='option2'>Two</option>\n"
353             + "  </select>\n"
354             + "</form>\n"
355             + "</body></html>";
356 
357         loadPageVerifyTitle2(html);
358     }
359 
360     /**
361      * @throws Exception if the test fails
362      */
363     @Test
364     @Alerts({"false", "true", "true", "false", "true"})
365     public void disabledAttribute() throws Exception {
366         final String html = DOCTYPE_HTML
367             + "<html>\n"
368             + "  <head>\n"
369             + "    <script>\n"
370             + LOG_TITLE_FUNCTION
371             + "      function test() {\n"
372             + "        var test1 = document.getElementById('test1');\n"
373             + "        log(test1.disabled);\n"
374             + "        test1.disabled = true;\n"
375             + "        log(test1.disabled);\n"
376             + "        test1.disabled = true;\n"
377             + "        log(test1.disabled);\n"
378             + "        test1.disabled = false;\n"
379             + "        log(test1.disabled);\n"
380 
381             + "        var test2 = document.getElementById('test2');\n"
382             + "        log(test2.disabled);\n"
383             + "      }\n"
384             + "    </script>\n"
385             + "  </head>\n"
386             + "  <body onload='test()'>\n"
387             + "    <form name='form1'>\n"
388             + "      <select>\n"
389             + "        <option id='test1' value='option1'>Option1</option>\n"
390             + "        <option id='test2' value='option2' disabled>Option2</option>\n"
391             + "      </select>\n"
392             + "  </form>\n"
393             + "</body></html>";
394         loadPageVerifyTitle2(html);
395     }
396 
397     /**
398      * @throws Exception if the test fails
399      */
400     @Test
401     @Alerts({"some text", "some value", "false", "some other text", "some other value", "true"})
402     public void readPropsBeforeAdding() throws Exception {
403         final String html = DOCTYPE_HTML
404             + "<html><head><script>\n"
405             + LOG_TITLE_FUNCTION
406             + "function doTest() {\n"
407             + "  var oOption = new Option('some text', 'some value');\n"
408             + "  log(oOption.text);\n"
409             + "  log(oOption.value);\n"
410             + "  log(oOption.selected);\n"
411             + "  oOption.text = 'some other text';\n"
412             + "  oOption.value = 'some other value';\n"
413             + "  oOption.selected = true;\n"
414             + "  log(oOption.text);\n"
415             + "  log(oOption.value);\n"
416             + "  log(oOption.selected);\n"
417             + "}</script></head><body onload='doTest()'>\n"
418             + "<p>hello world</p>\n"
419             + "</body></html>";
420 
421         loadPageVerifyTitle2(html);
422     }
423 
424     /**
425      * Regression test for bug 313.
426      * See http://sourceforge.net/p/htmlunit/bugs/313/.
427      * @throws Exception if the test fails
428      */
429     @Test
430     public void selectingOrphanedOptionCreatedByDocument() throws Exception {
431         final String html = DOCTYPE_HTML
432             + "<html>\n"
433             + "<body>\n"
434             + "<form name='myform'/>\n"
435             + "<script>\n"
436             + "var select = document.createElement('select');\n"
437             + "var opt = document.createElement('option');\n"
438             + "opt.value = 'x';\n"
439             + "opt.selected = true;\n"
440             + "select.appendChild(opt);\n"
441             + "document.myform.appendChild(select);\n"
442             + "</script>\n"
443             + "</body></html>";
444 
445         loadPage2(html);
446     }
447 
448     /**
449      * Regression test for 1592728.
450      * @throws Exception if the test fails
451      */
452     @Test
453     @Alerts({"2", "2"})
454     public void setSelected() throws Exception {
455         final String html = DOCTYPE_HTML
456             + "<html><head><script>\n"
457             + LOG_TITLE_FUNCTION
458             + "function doTest() {\n"
459             + "  var sel = document.form1.select1;\n"
460             + "  log(sel.selectedIndex);\n"
461             + "  sel.options[0].selected = false;\n"
462             + "  log(sel.selectedIndex);\n"
463             + "}</script></head><body onload='doTest()'>\n"
464             + "<form name='form1'>\n"
465             + "  <select name='select1' onchange='this.form.submit()'>\n"
466             + "    <option value='option1' name='option1'>One</option>\n"
467             + "    <option value='option2' name='option2'>Two</option>\n"
468             + "    <option value='option3' name='option3' selected>Three</option>\n"
469             + "  </select>\n"
470             + "</form>\n"
471             + "</body></html>";
472 
473         loadPageVerifyTitle2(html);
474     }
475 
476     /**
477      * Regression test for 1672048.
478      * @throws Exception if the test fails
479      */
480     @Test
481     public void setAttribute() throws Exception {
482         final String html = DOCTYPE_HTML
483             + "<html>\n"
484             + "<head><title>foo</title>\n"
485             + "<script>\n"
486             + "  function doTest() {\n"
487             + "    document.getElementById('option1').setAttribute('class', 'bla bla');\n"
488             + "    var o = new Option('some text', 'some value');\n"
489             + "    o.setAttribute('class', 'myClass');\n"
490             + "  }\n"
491             + "</script>\n"
492             + "</head>\n"
493             + "<body onload='doTest()'>\n"
494             + "  <form name='form1'>\n"
495             + "    <select name='select1'>\n"
496             + "      <option value='option1' id='option1' name='option1'>One</option>\n"
497             + "    </select>\n"
498             + "  </form>\n"
499             + "</body></html>";
500 
501         loadPage2(html);
502     }
503 
504     /**
505      * @throws Exception if the test fails
506      */
507     @Test
508     @Alerts({"undefined", "undefined"})
509     public void optionIndexOutOfBound() throws Exception {
510         final String html = DOCTYPE_HTML
511             + "<html>\n"
512             + "<head>\n"
513             + "<script>\n"
514             + LOG_TITLE_FUNCTION
515             + "function doTest() {\n"
516             + "  var options = document.getElementById('testSelect').options;\n"
517             + "  log(options[55]);\n"
518             + "  try {\n"
519             + "    log(options[-55]);\n"
520             + "  } catch(e) { logEx(e); }\n"
521             + "}\n"
522             + "</script>\n"
523             + "</head>\n"
524             + "<body onload='doTest()'>\n"
525             + "  <form name='form1'>\n"
526             + "    <select name='select1' id='testSelect'>\n"
527             + "      <option value='option1' name='option1'>One</option>\n"
528             + "    </select>\n"
529             + "  </form>\n"
530             + "</body></html>";
531 
532         loadPageVerifyTitle2(html);
533     }
534 
535     /**
536      * @throws Exception if the test fails
537      */
538     @Test
539     @Alerts({"o2: text: Option 2, label: Option 2, value: 2, defaultSelected: false, selected: false",
540              "o3: text: Option 3, label: Option 3, value: 3, defaultSelected: true, selected: false",
541              "0", "1"})
542     public void constructor() throws Exception {
543         final String html = DOCTYPE_HTML
544             + "<html><head><script>\n"
545             + LOG_TITLE_FUNCTION
546             + "function dumpOption(_o) {\n"
547             + "  return 'text: ' + _o.text\n"
548             + " + ', label: ' + _o.label\n"
549             + " + ', value: ' + _o.value\n"
550             + " + ', defaultSelected: ' + _o.defaultSelected\n"
551             + " + ', selected: ' + _o.selected;\n"
552             + "}\n"
553             + "function doTest() {\n"
554             + "  var o2 = new Option('Option 2', '2');\n"
555             + "  log('o2: ' + dumpOption(o2));\n"
556             + "  var o3 = new Option('Option 3', '3', true, false);\n"
557             + "  log('o3: ' + dumpOption(o3));\n"
558             + "  document.form1.select1.appendChild(o3);\n"
559             + "  log(document.form1.select1.options.selectedIndex);\n"
560             + "  document.form1.reset();\n"
561             + "  log(document.form1.select1.options.selectedIndex);\n"
562             + "}\n"
563             + "</script></head><body onload='doTest()'>\n"
564             + "<form name='form1'>\n"
565             + "  <select name='select1' id='testSelect'>\n"
566             + "    <option value='option1' name='option1'>One</option>\n"
567             + "  </select>\n"
568             + "</form>\n"
569             + "</body></html>";
570 
571         loadPageVerifyTitle2(html);
572     }
573 
574     /**
575      * @throws Exception if the test fails
576      */
577     @Test
578     @Alerts("0")
579     public void insideBold() throws Exception {
580         final String html = DOCTYPE_HTML
581             + "<html><head><script>\n"
582             + LOG_TITLE_FUNCTION
583             + "function test() {\n"
584             + "  var sel = document.form1.select1;\n"
585             + "  sel.options[0] = null;\n"
586             + "  log(sel.options.length);\n"
587             + "}</script></head><body onload='test()'>\n"
588             + "<form name='form1'>\n"
589             + "  <b>\n"
590             + "    <select name='select1'>\n"
591             + "      <option>One</option>\n"
592             + "    </select>\n"
593             + "  </b>\n"
594             + "</form>\n"
595             + "</body></html>";
596 
597         loadPageVerifyTitle2(html);
598     }
599 
600     /**
601      * @throws Exception if the test fails
602      */
603     @Test
604     @Alerts({"null", "[object Attr]", "null", "null", "null",
605              "null", "null", "null", "null", "null"})
606     public void getAttributeNode() throws Exception {
607         final String html = DOCTYPE_HTML
608             + "<html><head><script>\n"
609             + LOG_TITLE_FUNCTION
610             + "function doTest() {\n"
611             + "  var s = document.getElementById('testSelect');\n"
612             + "  var o1 = s.options[0];\n"
613             + "  log(o1.getAttributeNode('id'));\n"
614             + "  log(o1.getAttributeNode('name'));\n"
615             + "  log(o1.getAttributeNode('value'));\n"
616             + "  log(o1.getAttributeNode('selected'));\n"
617             + "  log(o1.getAttributeNode('foo'));\n"
618             + "  var o2 = s.options[1];\n"
619             + "  log(o2.getAttributeNode('id'));\n"
620             + "  log(o2.getAttributeNode('name'));\n"
621             + "  log(o2.getAttributeNode('value'));\n"
622             + "  log(o2.getAttributeNode('selected'));\n"
623             + "  log(o2.getAttributeNode('foo'));\n"
624             + "}\n"
625             + "</script></head>\n"
626             + "<body onload='doTest()'>\n"
627             + "<form name='form1'>\n"
628             + "  <select name='select1' id='testSelect'>\n"
629             + "    <option name='option1'>One</option>\n"
630             + "    <option>Two</option>\n"
631             + "  </select>\n"
632             + "</form>\n"
633             + "</body></html>";
634 
635         loadPageVerifyTitle2(html);
636     }
637 
638     /**
639      * @throws Exception if the test fails
640      */
641     @Test
642     @Alerts({"false null null", "false null null", "true *selected selected",
643              "true null null", "false null null", "false *selected selected",
644              "false null null", "true null null", "false *selected selected",
645              "true null null", "false null null", "false *selected selected"})
646     public void selectedAttribute() throws Exception {
647         final String html = DOCTYPE_HTML
648             + "<html><head>\n"
649             + "<script>\n"
650             + LOG_TITLE_FUNCTION
651             + "  function info(opt) {\n"
652             + "    var attrNode = opt.getAttributeNode('selected');\n"
653             + "    if (attrNode) { attrNode = '*' + attrNode.value; }\n"
654             + "    log(opt.selected + ' ' + attrNode + ' ' + opt.getAttribute('selected'));\n"
655             + "  }\n"
656 
657             + "  function doTest() {\n"
658             + "    var s = document.getElementById('testSelect');\n"
659 
660             + "    var o1 = s.options[0];\n"
661             + "    var o2 = s.options[1];\n"
662             + "    var o3 = s.options[2];\n"
663 
664             + "    info(o1);info(o2);info(o3);\n"
665 
666             + "    o1.selected = true;\n"
667             + "    info(o1);info(o2);info(o3);\n"
668 
669             + "    o2.selected = true;\n"
670             + "    info(o1);info(o2);info(o3);\n"
671 
672             + "    o2.selected = false;\n"
673             + "    info(o1);info(o2);info(o3);\n"
674 
675             + "}\n"
676             + "</script></head>\n"
677             + "<body onload='doTest()'>\n"
678             + "<form name='form1'>\n"
679             + "  <select name='select1' id='testSelect'>\n"
680             + "    <option>One</option>\n"
681             + "    <option>Two</option>\n"
682             + "    <option selected='selected'>Three</option>\n"
683             + "  </select>\n"
684             + "</form>\n"
685             + "</body></html>";
686 
687         loadPageVerifyTitle2(html);
688     }
689 
690     /**
691      * @throws Exception if the test fails
692      */
693     @Test
694     @Alerts({"false null null", "false null null", "true *selected selected",
695              "true null null", "false null null", "true *selected selected",
696              "true null null", "true null null", "true *selected selected",
697              "true null null", "false null null", "true *selected selected"})
698     public void selectedAttributeMultiple() throws Exception {
699         final String html = DOCTYPE_HTML
700             + "<html><head>\n"
701             + "<script>\n"
702             + LOG_TITLE_FUNCTION
703             + "  function info(opt) {\n"
704             + "    var attrNode = opt.getAttributeNode('selected');\n"
705             + "    if (attrNode) { attrNode = '*' + attrNode.value; }\n"
706             + "    log(opt.selected + ' ' + attrNode + ' ' + opt.getAttribute('selected'));\n"
707             + "  }\n"
708 
709             + "  function doTest() {\n"
710             + "    var s = document.getElementById('testSelect');\n"
711 
712             + "    var o1 = s.options[0];\n"
713             + "    var o2 = s.options[1];\n"
714             + "    var o3 = s.options[2];\n"
715 
716             + "    info(o1);info(o2);info(o3);\n"
717 
718             + "    o1.selected = true;\n"
719             + "    info(o1);info(o2);info(o3);\n"
720 
721             + "    o2.selected = true;\n"
722             + "    info(o1);info(o2);info(o3);\n"
723 
724             + "    o2.selected = false;\n"
725             + "    info(o1);info(o2);info(o3);\n"
726 
727             + "}\n"
728             + "</script></head>\n"
729             + "<body onload='doTest()'>\n"
730             + "<form name='form1'>\n"
731             + "  <select name='select1' id='testSelect' multiple>\n"
732             + "    <option>One</option>\n"
733             + "    <option>Two</option>\n"
734             + "    <option selected='selected'>Three</option>\n"
735             + "  </select>\n"
736             + "</form>\n"
737             + "</body></html>";
738 
739         loadPageVerifyTitle2(html);
740     }
741 
742     /**
743      * @throws Exception if the test fails
744      */
745     @Test
746     @Alerts({"[object HTMLOptionsCollection]", "0", "1"})
747     public void with_new() throws Exception {
748         final String html = DOCTYPE_HTML
749             + "<html><head><script>\n"
750             + LOG_TITLE_FUNCTION
751             + "function doTest() {\n"
752             + "  var s = document.getElementById('testSelect');\n"
753             + "  log(s.options);\n"
754             + "  log(s.length);\n"
755             + "  try {\n"
756             + "    s.options[0] = new Option('one', 'two');\n"
757             + "  } catch(e) { log(e) }\n"
758             + "  log(s.length);\n"
759             + "}\n"
760             + "</script></head>\n"
761             + "<body onload='doTest()'>\n"
762             + "  <select id='testSelect'>\n"
763             + "  </select>\n"
764             + "</form>\n"
765             + "</body></html>";
766 
767         loadPageVerifyTitle2(html);
768     }
769 
770     /**
771      * @throws Exception if the test fails
772      */
773     @Test
774     @Alerts({"[object HTMLOptionsCollection]", "0", "TypeError", "0"})
775     public void without_new() throws Exception {
776         final String html = DOCTYPE_HTML
777             + "<html><head><script>\n"
778             + LOG_TITLE_FUNCTION
779             + "function doTest() {\n"
780             + "  var s = document.getElementById('testSelect');\n"
781             + "  log(s.options);\n"
782             + "  log(s.length);\n"
783             + "  try {\n"
784             + "    s.options[0] = Option('one', 'two');\n"
785             + "  } catch(e) { logEx(e) }\n"
786             + "  log(s.length);\n"
787             + "}\n"
788             + "</script></head>\n"
789             + "<body onload='doTest()'>\n"
790             + "  <select id='testSelect'>\n"
791             + "  </select>\n"
792             + "</form>\n"
793             + "</body></html>";
794 
795         loadPageVerifyTitle2(html);
796     }
797 
798     /**
799      * @throws Exception if the test fails
800      */
801     @Test
802     @Alerts({"text1", "New Text1", "", "New Text2", "text3", "New Text3", "text4", "New Text4"})
803     public void text() throws Exception {
804         final String html = DOCTYPE_HTML
805             + "<html>\n"
806             + "  <head>\n"
807             + "    <script>\n"
808             + LOG_TITLE_FUNCTION
809             + "      function test() {\n"
810             + "        var option = document.getElementsByTagName('option')[0];\n"
811             + "        log(option.text);\n"
812             + "        option.text = 'New Text1';\n"
813             + "        log(option.text);\n"
814 
815             + "        option = document.getElementsByTagName('option')[1];\n"
816             + "        log(option.text);\n"
817             + "        option.text = 'New Text2';\n"
818             + "        log(option.text);\n"
819 
820             + "        option = document.getElementsByTagName('option')[2];\n"
821             + "        log(option.text);\n"
822             + "        option.text = 'New Text3';\n"
823             + "        log(option.text);\n"
824 
825             + "        option = document.getElementsByTagName('option')[3];\n"
826             + "        log(option.text);\n"
827             + "        option.text = 'New Text4';\n"
828             + "        log(option.text);\n"
829             + "      }\n"
830             + "    </script>\n"
831             + "  </head>\n"
832             + "  <body onload='test()'>\n"
833             + "    <select>\n"
834             + "      <option value='value1' label='label1'>text1</option>\n"
835             + "      <option value='value2' label='label2'></option>\n"
836             + "      <option value='value3' label=''>text3</option>\n"
837             + "      <option value='value4' >text4</option>\n"
838             + "    </select>\n"
839             + "  </body>\n"
840             + "</html>";
841         loadPageVerifyTitle2(html);
842     }
843 
844     /**
845      * @throws Exception if the test fails
846      */
847     @Test
848     @Alerts({"null", "[object Text]", "null"})
849     public void setText() throws Exception {
850         final String html = DOCTYPE_HTML
851             + "<html><head><script>\n"
852             + LOG_TITLE_FUNCTION
853             + "function test() {\n"
854             + "  var s = document.getElementById('testSelect');\n"
855             + "  var lastIndex = s.length;\n"
856             + "  s.length += 1;\n"
857             + "  log(s[lastIndex].firstChild);\n"
858             + "  s[lastIndex].text  = 'text2';\n"
859             + "  log(s[lastIndex].firstChild);\n"
860             + "  s[lastIndex].text  = '';\n"
861             + "  log(s[lastIndex].firstChild);\n"
862             + "}\n"
863             + "</script></head><body onload='test()'>\n"
864             + "  <select id='testSelect'>\n"
865             + "    <option value='value1' label='label1'>text1</option>\n"
866             + "  </select>\n"
867             + "</form>\n"
868             + "</body></html>";
869 
870         loadPageVerifyTitle2(html);
871     }
872 
873     /**
874      * Visibility of elements should not impact the determination of the value of option
875      * without value attribute or the text of options. This tests for one part a regression
876      * introduced in rev. 4367 as well probably as a problem that exists since a long time.
877      * @throws Exception if the test fails
878      */
879     @Test
880     @Alerts({"text1", "text1b", "text2"})
881     public void text_when_not_displayed() throws Exception {
882         final String html = DOCTYPE_HTML
883             + "<html><head><script>\n"
884             + LOG_TITLE_FUNCTION
885             + "function test() {\n"
886             + "  var s = document.getElementById('testSelect1');\n"
887             + "  log(s.options[0].text);\n"
888             + "  log(s.options[1].text);\n"
889             + "  var s2 = document.getElementById('testSelect2');\n"
890             + "  log(s2.options[0].text);\n"
891             + "}\n"
892             + "</script></head><body onload='test()'>\n"
893             + "  <div style='display: none'>\n"
894             + "  <select id='testSelect1'>\n"
895             + "    <option>text1</option>\n"
896             + "    <option><strong>text1b</strong></option>\n"
897             + "  </select>\n"
898             + "  </div>\n"
899             + "  <div style='visibility: hidden'>\n"
900             + "  <select id='testSelect2'>\n"
901             + "    <option>text2</option>\n"
902             + "  </select>\n"
903             + "  </div>\n"
904             + "</form>\n"
905             + "</body></html>";
906 
907         loadPageVerifyTitle2(html);
908     }
909 
910     /**
911      * For IE nested nodes aren't used as default value attribute.
912      * @throws Exception if the test fails
913      */
914     @Test
915     @Alerts({"text0", "text1", "text1b", "text2"})
916     public void defaultValueFromNestedNodes() throws Exception {
917         final String html = DOCTYPE_HTML
918             + "<html><head><script>\n"
919             + LOG_TITLE_FUNCTION
920             + "function test() {\n"
921             + "  var s0 = document.getElementById('testSelect0');\n"
922             + "  log(s0.options[0].value);\n"
923             + "  var s = document.getElementById('testSelect1');\n"
924             + "  log(s.options[0].value);\n"
925             + "  log(s.options[1].value);\n"
926             + "  var s2 = document.getElementById('testSelect2');\n"
927             + "  log(s2.options[0].value);\n"
928             + "}\n"
929             + "</script></head><body onload='test()'>\n"
930             + "  <select id='testSelect0'>\n"
931             + "    <option>text0</option>\n"
932             + "  </select>\n"
933             + "  <div style='display: none'>\n"
934             + "  <select id='testSelect1'>\n"
935             + "    <option>text1</option>\n"
936             + "    <option><strong>text1b</strong></option>\n"
937             + "  </select>\n"
938             + "  </div>\n"
939             + "  <div style='visibility: hidden'>\n"
940             + "  <select id='testSelect2'>\n"
941             + "    <option>text2</option>\n"
942             + "  </select>\n"
943             + "  </div>\n"
944             + "</form>\n"
945             + "</body></html>";
946         loadPageVerifyTitle2(html);
947     }
948 
949     /**
950      * @throws Exception if the test fails
951      */
952     @Test
953     @Alerts("[object HTMLFormElement]")
954     public void form() throws Exception {
955         final String html = DOCTYPE_HTML
956             + "<html>\n"
957             + "<body>\n"
958             + "  <form>\n"
959             + "    <select id='s'>\n"
960             + "      <option>a</option>\n"
961             + "    </select>\n"
962             + "  </form>\n"
963             + "  <script>\n"
964             + LOG_TITLE_FUNCTION
965             + "    log(document.getElementById('s').options[0].form);\n"
966             + "  </script>\n"
967             + "</body></html>";
968         loadPageVerifyTitle2(html);
969     }
970 
971     /**
972      * @throws Exception if the test fails
973      */
974     @Test
975     @Alerts(DEFAULT = {"o2",
976                        "InvalidStateError: Failed to execute 'iterateNext' on 'XPathResult': "
977                             + "The document has mutated since the result was returned.",
978                        "1", "0", "o2",
979                        "InvalidStateError: Failed to execute 'iterateNext' on 'XPathResult': "
980                             + "The document has mutated since the result was returned."},
981             FF = {"o2",
982                   "InvalidStateError: XPathResult.iterateNext: "
983                         + "The document has been mutated since the result was returned",
984                   "1", "0", "o2",
985                   "InvalidStateError: XPathResult.iterateNext: "
986                         + "The document has been mutated since the result was returned"},
987             FF_ESR = {"o2",
988                       "InvalidStateError: XPathResult.iterateNext: "
989                             + "The document has been mutated since the result was returned",
990                       "1", "0", "o2",
991                       "InvalidStateError: XPathResult.iterateNext: "
992                             + "The document has been mutated since the result was returned"})
993     @HtmlUnitNYI(CHROME = {"o2", "1", "0", "o2"},
994             EDGE = {"o2", "1", "0", "o2"},
995             FF = {"o2", "1", "0", "o2"},
996             FF_ESR = {"o2", "1", "0", "o2"})
997     public void xpathSelected() throws Exception {
998         final String selectionChangeCode = "    sel.options[1].selected = false;\n";
999 
1000         xpathSelected(selectionChangeCode, false);
1001     }
1002 
1003     /**
1004      * @throws Exception if the test fails
1005      */
1006     @Test
1007     @Alerts(DEFAULT = {"o2",
1008                        "InvalidStateError: Failed to execute 'iterateNext' on 'XPathResult': "
1009                             + "The document has mutated since the result was returned.",
1010                        "1", "1", "o2",
1011                        "InvalidStateError: Failed to execute 'iterateNext' on 'XPathResult': "
1012                             + "The document has mutated since the result was returned."},
1013             FF = {"o2",
1014                   "InvalidStateError: XPathResult.iterateNext: "
1015                         + "The document has been mutated since the result was returned",
1016                   "1", "1", "o2",
1017                   "InvalidStateError: XPathResult.iterateNext: "
1018                         + "The document has been mutated since the result was returned"},
1019             FF_ESR = {"o2",
1020                       "InvalidStateError: XPathResult.iterateNext: "
1021                         + "The document has been mutated since the result was returned",
1022                       "1", "1", "o2",
1023                       "InvalidStateError: XPathResult.iterateNext: "
1024                         + "The document has been mutated since the result was returned"})
1025     @HtmlUnitNYI(CHROME = {"o2", "1", "1", "o2"},
1026             EDGE = {"o2", "1", "1", "o2"},
1027             FF = {"o2", "1", "1", "o2"},
1028             FF_ESR = {"o2", "1", "1", "o2"})
1029     public void xpathSelectedSetAttribute() throws Exception {
1030         final String selectionChangeCode = "    sel.options[1].setAttribute('selected', false);\n";
1031 
1032         xpathSelected(selectionChangeCode, false);
1033     }
1034 
1035     /**
1036      * @throws Exception if the test fails
1037      */
1038     @Test
1039     @Alerts(DEFAULT = {"o2",
1040                        "InvalidStateError: Failed to execute 'iterateNext' on 'XPathResult': "
1041                             + "The document has mutated since the result was returned.",
1042                        "1", "-1", "o2",
1043                        "InvalidStateError: Failed to execute 'iterateNext' on 'XPathResult': "
1044                             + "The document has mutated since the result was returned."},
1045             FF = {"o2",
1046                   "InvalidStateError: XPathResult.iterateNext: "
1047                             + "The document has been mutated since the result was returned",
1048                   "1", "-1", "o2",
1049                   "InvalidStateError: XPathResult.iterateNext: "
1050                             + "The document has been mutated since the result was returned"},
1051             FF_ESR = {"o2",
1052                       "InvalidStateError: XPathResult.iterateNext: "
1053                             + "The document has been mutated since the result was returned",
1054                       "1", "-1", "o2",
1055                       "InvalidStateError: XPathResult.iterateNext: "
1056                             + "The document has been mutated since the result was returned"})
1057     @HtmlUnitNYI(CHROME = {"o2", "1", "-1", "o2"},
1058             EDGE = {"o2", "1", "-1", "o2"},
1059             FF = {"o2", "1", "-1", "o2"},
1060             FF_ESR = {"o2", "1", "-1", "o2"})
1061     public void xpathSelectedMultiple() throws Exception {
1062         final String selectionChangeCode = "    sel.options[1].selected = false;\n";
1063 
1064         xpathSelected(selectionChangeCode, true);
1065     }
1066 
1067     /**
1068      * @throws Exception if the test fails
1069      */
1070     @Test
1071     @Alerts(DEFAULT = {"o2",
1072                        "InvalidStateError: Failed to execute 'iterateNext' on 'XPathResult': "
1073                             + "The document has mutated since the result was returned.",
1074                        "1", "1", "o2",
1075                        "InvalidStateError: Failed to execute 'iterateNext' on 'XPathResult': "
1076                             + "The document has mutated since the result was returned."},
1077             FF = {"o2",
1078                   "InvalidStateError: XPathResult.iterateNext: "
1079                             + "The document has been mutated since the result was returned",
1080                   "1", "1", "o2",
1081                   "InvalidStateError: XPathResult.iterateNext: "
1082                             + "The document has been mutated since the result was returned"},
1083             FF_ESR = {"o2",
1084                       "InvalidStateError: XPathResult.iterateNext: "
1085                             + "The document has been mutated since the result was returned",
1086                       "1", "1", "o2",
1087                       "InvalidStateError: XPathResult.iterateNext: "
1088                             + "The document has been mutated since the result was returned"})
1089     @HtmlUnitNYI(CHROME = {"o2", "1", "1", "o2"},
1090             EDGE = {"o2", "1", "1", "o2"},
1091             FF = {"o2", "1", "1", "o2"},
1092             FF_ESR = {"o2", "1", "1", "o2"})
1093     public void xpathSelectedSetAttributeMultiple() throws Exception {
1094         final String selectionChangeCode = "    sel.options[1].setAttribute('selected', false);\n";
1095 
1096         xpathSelected(selectionChangeCode, false);
1097     }
1098 
1099     private void xpathSelected(final String selectionChangeCode, final boolean multiple) throws Exception {
1100         final String html = DOCTYPE_HTML
1101             + "<html>\n"
1102             + "<head>\n"
1103             + "<script>\n"
1104             + LOG_TITLE_FUNCTION
1105             + "  function test() {\n"
1106             + "    xpath();\n"
1107             + "    var sel = document.getElementById('s1');\n"
1108             + "    log(sel.selectedIndex);\n"
1109             + selectionChangeCode
1110             + "    log(sel.selectedIndex);\n"
1111             + "    xpath();\n"
1112             + "  }\n"
1113 
1114             + "  function xpath() {\n"
1115             + "    if (document.evaluate && XPathResult) {\n"
1116             + "      try {\n"
1117             + "        var result = document.evaluate('" + "//option[@selected]" + "', document.documentElement, "
1118                                             + "null, XPathResult.ANY_TYPE, null);\n"
1119 
1120             + "        var thisNode = result.iterateNext();\n"
1121             + "        while (thisNode) {\n"
1122             + "          log(thisNode.getAttribute('id'));\n"
1123             + "          thisNode = result.iterateNext();\n"
1124             + "        }\n"
1125             + "      } catch(e) { log(e); }\n"
1126             + "    } else {\n"
1127             + "      log('evaluate not supported');\n"
1128             + "    }\n"
1129             + "  }\n"
1130             + "</script>\n"
1131             + "</head>\n"
1132             + "<body onload='test()'>\n"
1133             + "  <form id='form1'>\n"
1134             + "    <select id='s1' name='select1' " + (multiple ? "multiple='multiple'" : "") + ">\n"
1135             + "      <option id='o1' value='option1'>Option1</option>\n"
1136             + "      <option id='o2' value='option2' selected='selected'>Option2</option>\n"
1137             + "      <option id='o3'value='option3'>Option3</option>\n"
1138             + "    </select>\n"
1139             + "  </form>\n"
1140             + "</body></html>";
1141 
1142         loadPageVerifyTitle2(html);
1143     }
1144 
1145     /**
1146      * @throws Exception if the test fails
1147      */
1148     @Test
1149     @Alerts({"value1", "text1", "label1", "value2", "text2", "text2"})
1150     public void label() throws Exception {
1151         final String html = DOCTYPE_HTML
1152             + "<html><head><script>\n"
1153             + LOG_TITLE_FUNCTION
1154             + "function test() {\n"
1155             + "  var s = document.getElementById('testSelect');\n"
1156             + "  var lastIndex = s.length;\n"
1157             + "  s.length += 1;\n"
1158             + "  s[lastIndex].value = 'value2';\n"
1159             + "  s[lastIndex].text  = 'text2';\n"
1160             + "  log(s[0].value);\n"
1161             + "  log(s[0].text);\n"
1162             + "  log(s[0].label);\n"
1163             + "  log(s[1].value);\n"
1164             + "  log(s[1].text);\n"
1165             + "  log(s[1].label);\n"
1166             + "}\n"
1167             + "</script></head><body onload='test()'>\n"
1168             + "  <select id='testSelect'>\n"
1169             + "    <option value='value1' label='label1'>text1</option>\n"
1170             + "  </select>\n"
1171             + "</form>\n"
1172             + "</body></html>";
1173 
1174         loadPageVerifyTitle2(html);
1175     }
1176 
1177     /**
1178      * @throws Exception if the test fails
1179      */
1180     @Test
1181     @Alerts({"", "", "", "", "text2", "text2", "text2", "label2"})
1182     public void setLabel() throws Exception {
1183         final String html = DOCTYPE_HTML
1184             + "<html><head><script>\n"
1185             + LOG_TITLE_FUNCTION
1186             + "function test() {\n"
1187             + "  var s = document.getElementById('testSelect');\n"
1188             + "  var lastIndex = s.length;\n"
1189             + "  s.length += 1;\n"
1190             + "  log(s[1].text);\n"
1191             + "  log(s[1].label);\n"
1192 
1193             + "  s[lastIndex].value = 'value2';\n"
1194             + "  log(s[1].text);\n"
1195             + "  log(s[1].label);\n"
1196 
1197             + "  s[lastIndex].text  = 'text2';\n"
1198             + "  log(s[1].text);\n"
1199             + "  log(s[1].label);\n"
1200 
1201             + "  s[lastIndex].label = 'label2';\n"
1202             + "  log(s[1].text);\n"
1203             + "  log(s[1].label);\n"
1204             + "}\n"
1205             + "</script></head><body onload='test()'>\n"
1206             + "  <select id='testSelect'>\n"
1207             + "    <option value='value1' label='label1'>text1</option>\n"
1208             + "  </select>\n"
1209             + "</form>\n"
1210             + "</body></html>";
1211 
1212         loadPageVerifyTitle2(html);
1213     }
1214 
1215     /**
1216      * @throws Exception if the test fails
1217      */
1218     @Test
1219     @Alerts({"0", "1", "2", "0"})
1220     public void index() throws Exception {
1221         final String html = DOCTYPE_HTML
1222             + "<html>\n"
1223             + "<head>\n"
1224             + "<script>\n"
1225             + LOG_TITLE_FUNCTION
1226             + "  function test() {\n"
1227             + "    var opt = document.getElementById('o1');\n"
1228             + "    log(opt.index);\n"
1229 
1230             + "    opt = document.getElementById('o2');\n"
1231             + "    log(opt.index);\n"
1232 
1233             + "    opt = document.getElementById('o3');\n"
1234             + "    log(opt.index);\n"
1235 
1236             + "    opt = document.createElement('option');\n"
1237             + "    log(opt.index);\n"
1238             + "  }\n"
1239             + "</script>\n"
1240             + "</head>\n"
1241             + "<body onload='test()'>\n"
1242             + "  <form id='form1'>\n"
1243             + "    <select id='s1'>\n"
1244             + "      <option id='o1' value='option1'>Option1</option>\n"
1245             + "      <option id='o2' value='option2' selected='selected'>Option2</option>\n"
1246             + "      <option id='o3'value='option3'>Option3</option>\n"
1247             + "    </select>\n"
1248             + "  </form>\n"
1249             + "</body></html>";
1250 
1251         loadPageVerifyTitle2(html);
1252     }
1253 
1254     /**
1255      * @throws Exception if the test fails
1256      */
1257     @Test
1258     @Alerts({"false-null", "true-selected", "false-null",
1259              "true-null", "false-selected", "false-null",
1260              "false-null", "false-selected", "false-null"})
1261     public void selectAndAttribute() throws Exception {
1262         final String html = DOCTYPE_HTML
1263             + "<html>\n"
1264             + "<head>\n"
1265             + "<script>\n"
1266             + LOG_TITLE_FUNCTION
1267             + "  function test() {\n"
1268             + "    var s1 = document.getElementById('select1');\n"
1269             + "    var o1 = document.getElementById('option1');\n"
1270             + "    var o2 = document.getElementById('option2');\n"
1271             + "    var o3 = document.getElementById('option3');\n"
1272 
1273             + "    log(o1.selected + '-' + o1.getAttribute('selected'));\n"
1274             + "    log(o2.selected + '-' + o2.getAttribute('selected'));\n"
1275             + "    log(o3.selected + '-' + o3.getAttribute('selected'));\n"
1276 
1277             + "    o1.selected = true;\n"
1278             + "    log(o1.selected + '-' + o1.getAttribute('selected'));\n"
1279             + "    log(o2.selected + '-' + o2.getAttribute('selected'));\n"
1280             + "    log(o3.selected + '-' + o3.getAttribute('selected'));\n"
1281 
1282             + "    s1.selectedIndex = 3;\n"
1283             + "    log(o1.selected + '-' + o1.getAttribute('selected'));\n"
1284             + "    log(o2.selected + '-' + o2.getAttribute('selected'));\n"
1285             + "    log(o3.selected + '-' + o3.getAttribute('selected'));\n"
1286             + "  }\n"
1287             + "</script>\n"
1288             + "</head>\n"
1289             + "<body onload='test()'>\n"
1290             + "  <form id='form1'>\n"
1291             + "    <select name='select1' id='select1'>\n"
1292             + "      <option value='option1' id='option1'>Option1</option>\n"
1293             + "      <option value='option2' id='option2' selected='selected'>Option2</option>\n"
1294             + "      <option value='option3' id='option3'>Option3</option>\n"
1295             + "    </select>\n"
1296             + "  </form>\n"
1297             + "</body></html>";
1298 
1299         loadPageVerifyTitle2(html);
1300     }
1301 
1302     /**
1303      * @throws Exception if the test fails
1304      */
1305     @Test
1306     @Alerts(DEFAULT = {"false-null", "true-true", "true-null",
1307                        "false-selected", "false-null", "true-true"},
1308             FF = {"false-null", "true-true", "true-null",
1309                   "false-selected", "false-null", "false-true"},
1310             FF_ESR = {"false-null", "true-true", "true-null",
1311                       "false-selected", "false-null", "false-true"})
1312     @HtmlUnitNYI(FF = {"false-null", "true-true", "true-null", "false-selected", "false-null", "true-true"},
1313             FF_ESR = {"false-null", "true-true", "true-null", "false-selected", "false-null", "true-true"})
1314     public void setSelectedAttribute() throws Exception {
1315         final String html = DOCTYPE_HTML
1316             + "<html>\n"
1317             + "<head>\n"
1318             + "<script>\n"
1319             + LOG_TITLE_FUNCTION
1320             + "  function test() {\n"
1321             + "    var o1 = document.getElementById('option1');\n"
1322             + "    log(o1.selected + '-' + o1.getAttribute('selected'));\n"
1323 
1324             + "    o1.setAttribute('selected', true);\n"
1325             + "    log(o1.selected + '-' + o1.getAttribute('selected'));\n"
1326 
1327             + "    o1.removeAttribute('selected');\n"
1328             + "    log(o1.selected + '-' + o1.getAttribute('selected'));\n"
1329 
1330             + "    var o2 = document.getElementById('option2');\n"
1331             + "    log(o2.selected + '-' + o2.getAttribute('selected'));\n"
1332 
1333             + "    o2.removeAttribute('selected');\n"
1334             + "    log(o2.selected + '-' + o2.getAttribute('selected'));\n"
1335 
1336             + "    o2.setAttribute('selected', true);\n"
1337             + "    log(o2.selected + '-' + o2.getAttribute('selected'));\n"
1338             + "  }\n"
1339             + "</script>\n"
1340             + "</head>\n"
1341             + "<body onload='test()'>\n"
1342             + "  <form id='form1'>\n"
1343             + "    <select name='select1' id='select1'>\n"
1344             + "      <option value='option1' id='option1'>Option1</option>\n"
1345             + "      <option value='option2' id='option2' selected='selected'>Option2</option>\n"
1346             + "    </select>\n"
1347             + "  </form>\n"
1348             + "</body></html>";
1349 
1350         loadPageVerifyTitle2(html);
1351     }
1352 
1353     /**
1354      * @throws Exception if the test fails
1355      */
1356     @Test
1357     @Alerts({"false-null", "true-true", "false-null",
1358              "false-null", "true-true", "false-null"})
1359     public void createOption() throws Exception {
1360         final String html = DOCTYPE_HTML
1361             + "<html>\n"
1362             + "<head>\n"
1363             + "<script>\n"
1364             + LOG_TITLE_FUNCTION
1365             + "  function test() {\n"
1366             + "    var o1 = document.createElement('option');\n"
1367 
1368             + "    log(o1.selected + '-' + o1.getAttribute('selected'));\n"
1369 
1370             + "    o1.setAttribute('selected', true);\n"
1371             + "    log(o1.selected + '-' + o1.getAttribute('selected'));\n"
1372 
1373             + "    o1.removeAttribute('selected');\n"
1374             + "    log(o1.selected + '-' + o1.getAttribute('selected'));\n"
1375 
1376             + "    var s1 = document.getElementById('select1');\n"
1377             + "    var o2 = document.createElement('option');\n"
1378             + "    s1.appendChild(o2);\n"
1379 
1380             + "    log(o2.selected + '-' + o2.getAttribute('selected'));\n"
1381 
1382             + "    o2.setAttribute('selected', true);\n"
1383             + "    log(o2.selected + '-' + o2.getAttribute('selected'));\n"
1384 
1385             + "    o2.removeAttribute('selected');\n"
1386             + "    log(o2.selected + '-' + o2.getAttribute('selected'));\n"
1387             + "  }\n"
1388             + "</script>\n"
1389             + "</head>\n"
1390             + "<body onload='test()'>\n"
1391             + "  <form id='form1'>\n"
1392             + "    <select name='select1' id='select1'>\n"
1393             + "      <option value='option1' id='option1'>Option1</option>\n"
1394             + "    </select>\n"
1395             + "  </form>\n"
1396             + "</body></html>";
1397 
1398         loadPageVerifyTitle2(html);
1399     }
1400 
1401     /**
1402      * @throws Exception if an error occurs
1403      */
1404     @Test
1405     @Alerts({"s-mouse over [select1]", "o-mouse over [option1]", "s-mouse over [option1]"})
1406     @BuggyWebDriver({"o-mouse over [option1]", "s-mouse over [option1]"})
1407     public void mouseOver() throws Exception {
1408         shutDownAll();
1409 
1410         final String html = DOCTYPE_HTML
1411             + "<html>\n"
1412             + "  <head>\n"
1413             + "    <script>\n"
1414             + LOG_TEXTAREA_FUNCTION
1415             + "    function dumpEvent(event, pre) {\n"
1416             + "      var eTarget;\n"
1417             + "      if (event.target) {\n"
1418             + "        eTarget = event.target;\n"
1419             + "      } else if (event.srcElement) {\n"
1420             + "        eTarget = event.srcElement;\n"
1421             + "      }\n"
1422             + "      // defeat Safari bug\n"
1423             + "      if (eTarget.nodeType == 3) {\n"
1424             + "        eTarget = eTarget.parentNode;\n"
1425             + "      }\n"
1426             + "      var msg = pre + '-mouse over';\n"
1427             + "      if (eTarget.name) {\n"
1428             + "        msg = msg + ' [' + eTarget.name + ']';\n"
1429             + "      } else {\n"
1430             + "        msg = msg + ' [' + eTarget.id + ']';\n"
1431             + "      }\n"
1432             + "      log(msg);\n"
1433             + "    }\n"
1434             + "    </script>\n"
1435             + "  </head>\n"
1436             + "<body>\n"
1437             + "  <form id='form1'>\n"
1438             + "    <select name='select1' id='select1' size='2' onmouseover='dumpEvent(event, \"s\");' >\n"
1439             + "      <option value='option1' id='option1' onmouseover='dumpEvent(event, \"o\");' >Option1</option>\n"
1440             + "      <option value='option2' id='option2'>Option2</option>\n"
1441             + "    </select>\n"
1442             + "  </form>\n"
1443             + LOG_TEXTAREA
1444             + "</body></html>";
1445 
1446         final WebDriver driver = loadPage2(html);
1447         final Actions actions = new Actions(driver);
1448         actions.moveToElement(driver.findElement(By.id("option1")));
1449         actions.perform();
1450 
1451         verifyTextArea2(driver, getExpectedAlerts());
1452     }
1453 
1454     /**
1455      * @throws Exception if an error occurs
1456      */
1457     @Test
1458     @Alerts({"s-mouse over [select1]", "o-mouse over [option1]", "s-mouse over [option1]"})
1459     @BuggyWebDriver({"o-mouse over [option1]", "s-mouse over [option1]"})
1460     public void mouseOverDisabledSelect() throws Exception {
1461         shutDownAll();
1462 
1463         final String html = DOCTYPE_HTML
1464             + "<html>\n"
1465             + "  <head>\n"
1466             + "    <script>\n"
1467             + LOG_TEXTAREA_FUNCTION
1468             + "    function dumpEvent(event, pre) {\n"
1469             + "      // target\n"
1470             + "      var eTarget;\n"
1471             + "      if (event.target) {\n"
1472             + "        eTarget = event.target;\n"
1473             + "      } else if (event.srcElement) {\n"
1474             + "        eTarget = event.srcElement;\n"
1475             + "      }\n"
1476             + "      // defeat Safari bug\n"
1477             + "      if (eTarget.nodeType == 3) {\n"
1478             + "        eTarget = eTarget.parentNode;\n"
1479             + "      }\n"
1480             + "      var msg = pre + '-mouse over';\n"
1481             + "      if (eTarget.name) {\n"
1482             + "        msg = msg + ' [' + eTarget.name + ']';\n"
1483             + "      } else {\n"
1484             + "        msg = msg + ' [' + eTarget.id + ']';\n"
1485             + "      }\n"
1486             + "      log(msg);\n"
1487             + "    }\n"
1488             + "    </script>\n"
1489             + "  </head>\n"
1490             + "<body>\n"
1491             + "  <form id='form1'>\n"
1492             + "    <select name='select1' id='select1' size='2' disabled='disabled' "
1493                             + "onmouseover='dumpEvent(event, \"s\");' >\n"
1494             + "      <option value='option1' id='option1' onmouseover='dumpEvent(event, \"o\");'>Option1</option>\n"
1495             + "      <option value='option2' id='option2'>Option2</option>\n"
1496             + "    </select>\n"
1497             + "  </form>\n"
1498             + LOG_TEXTAREA
1499             + "</body></html>";
1500 
1501         final WebDriver driver = loadPage2(html);
1502         final Actions actions = new Actions(driver);
1503         actions.moveToElement(driver.findElement(By.id("option1")));
1504         actions.perform();
1505 
1506         verifyTextArea2(driver, getExpectedAlerts());
1507     }
1508 
1509     /**
1510      * @throws Exception if an error occurs
1511      */
1512     @Test
1513     @Alerts({"s-mouse over [select1]", "o-mouse over [option1]", "s-mouse over [option1]"})
1514     @BuggyWebDriver({"o-mouse over [option1]", "s-mouse over [option1]"})
1515     public void mouseOverDisabledOption() throws Exception {
1516         shutDownAll();
1517 
1518         final String html = DOCTYPE_HTML
1519             + "<html>\n"
1520             + "  <head>\n"
1521             + "    <script>\n"
1522             + LOG_TEXTAREA_FUNCTION
1523             + "    function dumpEvent(event, pre) {\n"
1524             + "      // target\n"
1525             + "      var eTarget;\n"
1526             + "      if (event.target) {\n"
1527             + "        eTarget = event.target;\n"
1528             + "      } else if (event.srcElement) {\n"
1529             + "        eTarget = event.srcElement;\n"
1530             + "      }\n"
1531             + "      // defeat Safari bug\n"
1532             + "      if (eTarget.nodeType == 3) {\n"
1533             + "        eTarget = eTarget.parentNode;\n"
1534             + "      }\n"
1535             + "      var msg = pre + '-mouse over';\n"
1536             + "      if (eTarget.name) {\n"
1537             + "        msg = msg + ' [' + eTarget.name + ']';\n"
1538             + "      } else {\n"
1539             + "        msg = msg + ' [' + eTarget.id + ']';\n"
1540             + "      }\n"
1541             + "      if (msg.length == 0) { msg = '-' };\n"
1542             + "      log(msg);\n"
1543             + "    }\n"
1544             + "    </script>\n"
1545             + "  </head>\n"
1546             + "<body>\n"
1547             + "  <form id='form1'>\n"
1548             + "    <select name='select1' id='select1' size='2' onmouseover='dumpEvent(event, \"s\");' >\n"
1549             + "      <option value='option1' id='option1' onmouseover='dumpEvent(event, \"o\");' "
1550                                 + "disabled='disabled'>Option1</option>\n"
1551             + "      <option value='option2' id='option2'>Option2</option>\n"
1552             + "    </select>\n"
1553             + "  </form>\n"
1554             + LOG_TEXTAREA
1555             + "</body></html>";
1556 
1557         final WebDriver driver = loadPage2(html);
1558         final Actions actions = new Actions(driver);
1559         actions.moveToElement(driver.findElement(By.id("option1")));
1560         actions.perform();
1561 
1562         verifyTextArea2(driver, getExpectedAlerts());
1563     }
1564 }