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.html.HtmlUnorderedList;
19  import org.htmlunit.junit.BrowserRunner;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.openqa.selenium.By;
24  import org.openqa.selenium.WebDriver;
25  import org.openqa.selenium.WebElement;
26  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
27  
28  /**
29   * Unit tests for {@link HTMLUListElement}.
30   *
31   * @author Daniel Gredler
32   * @author Marc Guillemot
33   * @author Ronald Brill
34   * @author Frank Danek
35   */
36  @RunWith(BrowserRunner.class)
37  public class HTMLUListElementTest extends WebDriverTestCase {
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts("[object HTMLUListElement]")
44      public void simpleScriptable() throws Exception {
45          final String html = DOCTYPE_HTML
46              + "<html><head>\n"
47              + "<script>\n"
48              + LOG_TITLE_FUNCTION
49              + "  function test() {\n"
50              + "    log(document.getElementById('myId'));\n"
51              + "  }\n"
52              + "</script>\n"
53              + "</head><body onload='test()'>\n"
54              + "  <ul id='myId'/>\n"
55              + "</body></html>";
56  
57          final WebDriver driver = loadPageVerifyTitle2(html);
58          if (driver instanceof HtmlUnitDriver) {
59              final WebElement element = driver.findElement(By.id("myId"));
60              assertTrue(toHtmlElement(element) instanceof HtmlUnorderedList);
61          }
62      }
63  
64      /**
65       * @throws Exception if an error occurs
66       */
67      @Test
68      @Alerts({"false", "true", "true", "true", "null", "", "blah", "2",
69               "true", "false", "true", "false", "", "null", "", "null"})
70      public void compact() throws Exception {
71          final String html = DOCTYPE_HTML
72              + "<html>\n"
73              + "  <head>\n"
74              + "    <script>\n"
75              + LOG_TITLE_FUNCTION
76              + "      function test() {\n"
77              + "        log(document.getElementById('u1').compact);\n"
78              + "        log(document.getElementById('u2').compact);\n"
79              + "        log(document.getElementById('u3').compact);\n"
80              + "        log(document.getElementById('u4').compact);\n"
81              + "        log(document.getElementById('u1').getAttribute('compact'));\n"
82              + "        log(document.getElementById('u2').getAttribute('compact'));\n"
83              + "        log(document.getElementById('u3').getAttribute('compact'));\n"
84              + "        log(document.getElementById('u4').getAttribute('compact'));\n"
85  
86              + "        document.getElementById('u1').compact = true;\n"
87              + "        document.getElementById('u2').compact = false;\n"
88              + "        document.getElementById('u3').compact = 'xyz';\n"
89              + "        document.getElementById('u4').compact = null;\n"
90              + "        log(document.getElementById('u1').compact);\n"
91              + "        log(document.getElementById('u2').compact);\n"
92              + "        log(document.getElementById('u3').compact);\n"
93              + "        log(document.getElementById('u4').compact);\n"
94              + "        log(document.getElementById('u1').getAttribute('compact'));\n"
95              + "        log(document.getElementById('u2').getAttribute('compact'));\n"
96              + "        log(document.getElementById('u3').getAttribute('compact'));\n"
97              + "        log(document.getElementById('u4').getAttribute('compact'));\n"
98              + "      }\n"
99              + "    </script>\n"
100             + "  </head>\n"
101             + "  <body onload='test()'>\n"
102             + "    <ul id='u1'><li>a</li><li>b</li></ul>\n"
103             + "    <ul compact='' id='u2'><li>a</li><li>b</li></ul>\n"
104             + "    <ul compact='blah' id='u3'><li>a</li><li>b</li></ul>\n"
105             + "    <ul compact='2' id='u4'><li>a</li><li>b</li></ul>\n"
106             + "  </body>\n"
107             + "</html>";
108 
109         loadPageVerifyTitle2(html);
110     }
111 
112     /**
113      * @throws Exception if an error occurs
114      */
115     @Test
116     @Alerts({"", "", "blah", "A", "null", "", "blah", "A", "1", "a", "A", "i", "I", "u"})
117     public void type() throws Exception {
118         final String html = DOCTYPE_HTML
119                 + "<html>\n"
120                 + "  <head>\n"
121                 + "    <script>\n"
122                 + LOG_TITLE_FUNCTION
123                 + "      function test() {\n"
124                 + "        log(document.getElementById('u1').type);\n"
125                 + "        log(document.getElementById('u2').type);\n"
126                 + "        log(document.getElementById('u3').type);\n"
127                 + "        log(document.getElementById('u4').type);\n"
128                 + "        log(document.getElementById('u1').getAttribute('type'));\n"
129                 + "        log(document.getElementById('u2').getAttribute('type'));\n"
130                 + "        log(document.getElementById('u3').getAttribute('type'));\n"
131                 + "        log(document.getElementById('u4').getAttribute('type'));\n"
132 
133                 + "        document.getElementById('u1').type = '1';\n"
134                 + "        log(document.getElementById('u1').type);\n"
135 
136                 + "        document.getElementById('u1').type = 'a';\n"
137                 + "        log(document.getElementById('u1').type);\n"
138 
139                 + "        document.getElementById('u1').type = 'A';\n"
140                 + "        log(document.getElementById('u1').type);\n"
141 
142                 + "        document.getElementById('u1').type = 'i';\n"
143                 + "        log(document.getElementById('u1').type);\n"
144 
145                 + "        document.getElementById('u1').type = 'I';\n"
146                 + "        log(document.getElementById('u1').type);\n"
147 
148                 + "        try { document.getElementById('u1').type = 'u' } catch(e) {logEx(e);}\n"
149                 + "        log(document.getElementById('u1').type);\n"
150                 + "      }\n"
151                 + "    </script>\n"
152                 + "  </head>\n"
153                 + "  <body onload='test()'>\n"
154                 + "    <ul id='u1'><li>a</li><li>b</li></ul>\n"
155                 + "    <ul type='' id='u2'><li>a</li><li>b</li></ul>\n"
156                 + "    <ul type='blah' id='u3'><li>a</li><li>b</li></ul>\n"
157                 + "    <ul type='A' id='u4'><li>a</li><li>b</li></ul>\n"
158                 + "  </body>\n"
159                 + "</html>";
160 
161         loadPageVerifyTitle2(html);
162     }
163 }