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