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