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