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.html;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  import org.openqa.selenium.By;
21  import org.openqa.selenium.WebDriver;
22  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
23  
24  /**
25   * Tests for {@link HtmlHeading1} to {@link HtmlHeading6}.
26   *
27   * @author Ahmed Ashour
28   * @author Ronald Brill
29   * @author Frank Danek
30   */
31  public class HtmlHeading2Test extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts("[object HTMLHeadingElement]")
38      public void simpleScriptable() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><head>\n"
41              + "<script>\n"
42              + LOG_TITLE_FUNCTION
43              + "  function test() {\n"
44              + "    log(document.getElementById('myId'));\n"
45              + "  }\n"
46              + "</script>\n"
47              + "</head><body onload='test()'>\n"
48              + "  <h2 id='myId'>asdf</h2>\n"
49              + "</body></html>";
50  
51          final WebDriver driver = loadPageVerifyTitle2(html);
52          if (driver instanceof HtmlUnitDriver) {
53              final HtmlElement element = toHtmlElement(driver.findElement(By.id("myId")));
54              assertTrue(element instanceof HtmlHeading2);
55          }
56      }
57  
58      /**
59       * @throws Exception if an error occurs
60       */
61      @Test
62      @Alerts({"left", "right", "center", "justify", "wrong", ""})
63      public void getAlign() throws Exception {
64          final String html = DOCTYPE_HTML
65              + "<html><body>\n"
66              + "<form>\n"
67              + "  <h1 id='e1' align='left'>Header1</h1>\n"
68              + "  <h2 id='e2' align='right'>Header2</h2>\n"
69              + "  <h3 id='e3' align='center'>Header3</h3>\n"
70              + "  <h4 id='e4' align='justify'>Header4</h4>\n"
71              + "  <h5 id='e5' align='wrong'>Header5</h5>\n"
72              + "  <h6 id='e6'>Header6</h6>\n"
73              + "</form>\n"
74  
75              + "<script>\n"
76              + LOG_TITLE_FUNCTION
77              + "  log(e1.align);\n"
78              + "  log(e2.align);\n"
79              + "  log(e3.align);\n"
80              + "  log(e4.align);\n"
81              + "  log(e5.align);\n"
82              + "  log(e6.align);\n"
83              + "</script>\n"
84              + "</body></html>";
85  
86          loadPageVerifyTitle2(html);
87      }
88  
89      /**
90       * @throws Exception if an error occurs
91       */
92      @Test
93      @Alerts({"CenTer", "8", "foo"})
94      public void setAlign() throws Exception {
95          final String html = DOCTYPE_HTML
96              + "<html><body>\n"
97              + "<form>\n"
98              + "  <h1 id='e1' align='left'>Header1</h1>\n"
99              + "</form>\n"
100 
101             + "<script>\n"
102             + LOG_TITLE_FUNCTION
103             + "  function setAlign(elem, value) {\n"
104             + "    try {\n"
105             + "      elem.align = value;\n"
106             + "    } catch(e) { logEx(e); }\n"
107             + "  }\n"
108 
109             + "  var elem = document.getElementById('e1');\n"
110             + "  setAlign(elem, 'CenTer');\n"
111             + "  log(elem.align);\n"
112             + "  setAlign(e1, '8');\n"
113             + "  log(e1.align);\n"
114             + "  setAlign(e1, 'foo');\n"
115             + "  log(e1.align);\n"
116             + "</script>\n"
117             + "</body></html>";
118 
119         loadPageVerifyTitle2(html);
120     }
121 
122     /**
123      * @throws Exception if an error occurs
124      */
125     @Test
126     @Alerts({"undefined", "undefined", "undefined", "undefined", "undefined", "undefined",
127              "undefined", "left", "none", "right", "all", "2", "abc", "8"})
128     public void clear() throws Exception {
129         final String html = DOCTYPE_HTML
130             + "<html><body>\n"
131             + "<h1 id='h1'>h1</h1>\n"
132             + "<h2 id='h2' clear='left'>h2</h2>\n"
133             + "<h3 id='h3' clear='all'>h3</h3>\n"
134             + "<h4 id='h4' clear='right'>h4</h4>\n"
135             + "<h5 id='h5' clear='none'>h5</h5>\n"
136             + "<h6 id='h6' clear='2'>h6</h6>\n"
137             + "<h1 id='h7' clear='foo'>h7</h1>\n"
138             + "<script>\n"
139             + LOG_TITLE_FUNCTION
140             + "function set(h, value) {\n"
141             + "  try {\n"
142             + "    h.clear = value;\n"
143             + "  } catch(e) {\n"
144             + "    log('!');\n"
145             + "  }\n"
146             + "}\n"
147             + "var h1 = document.getElementById('h1');\n"
148             + "var h2 = document.getElementById('h2');\n"
149             + "var h3 = document.getElementById('h3');\n"
150             + "var h4 = document.getElementById('h4');\n"
151             + "var h5 = document.getElementById('h5');\n"
152             + "var h6 = document.getElementById('h6');\n"
153             + "var h7 = document.getElementById('h7');\n"
154             + "log(h1.clear);\n"
155             + "log(h2.clear);\n"
156             + "log(h3.clear);\n"
157             + "log(h4.clear);\n"
158             + "log(h5.clear);\n"
159             + "log(h6.clear);\n"
160             + "log(h7.clear);\n"
161             + "set(h1, 'left');\n"
162             + "set(h2, 'none');\n"
163             + "set(h3, 'right');\n"
164             + "set(h4, 'all');\n"
165             + "set(h5, 2);\n"
166             + "set(h6, 'abc');\n"
167             + "set(h7, '8');\n"
168             + "log(h1.clear);\n"
169             + "log(h2.clear);\n"
170             + "log(h3.clear);\n"
171             + "log(h4.clear);\n"
172             + "log(h5.clear);\n"
173             + "log(h6.clear);\n"
174             + "log(h7.clear);\n"
175             + "</script>\n"
176             + "</body></html>";
177 
178         loadPageVerifyTitle2(html);
179     }
180 }