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.HtmlPage;
19  import org.htmlunit.html.HtmlParagraph;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.jupiter.api.Test;
22  import org.openqa.selenium.WebDriver;
23  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
24  
25  /**
26   * Tests for {@link HTMLParagraphElement}.
27   * @author Daniel Gredler
28   * @author Ahmed Ashour
29   * @author Frank Danek
30   */
31  public class HTMLParagraphElementTest extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts("[object HTMLParagraphElement]")
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              + "  <p id='myId'/>\n"
49              + "</body></html>";
50  
51          final WebDriver driver = loadPageVerifyTitle2(html);
52          if (driver instanceof HtmlUnitDriver) {
53              final HtmlPage page = (HtmlPage) getEnclosedPage();
54              assertTrue(HtmlParagraph.class.isInstance(page.getHtmlElementById("myId")));
55          }
56      }
57  
58      /**
59       * @throws Exception if an error occurs
60       */
61      @Test
62      @Alerts({"", "hello", "left", "hi", "right"})
63      public void align() throws Exception {
64          final String html = DOCTYPE_HTML
65              + "<html>\n"
66              + "  <head>\n"
67              + "    <script>\n"
68              + LOG_TITLE_FUNCTION
69              + "      function test() {\n"
70              + "        var p = document.getElementById('p');\n"
71              + "        log(p.align);\n"
72              + "        set(p, 'hello');\n"
73              + "        log(p.align);\n"
74              + "        set(p, 'left');\n"
75              + "        log(p.align);\n"
76              + "        set(p, 'hi');\n"
77              + "        log(p.align);\n"
78              + "        set(p, 'right');\n"
79              + "        log(p.align);\n"
80              + "      }\n"
81              + "      function set(e, value) {\n"
82              + "        try {\n"
83              + "          e.align = value;\n"
84              + "        } catch(e) { logEx(e); }\n"
85              + "      }\n"
86              + "    </script>\n"
87              + "  </head>\n"
88              + "  <body onload='test()'><p id='p'>foo</p></body>\n"
89              + "</html>";
90  
91          loadPageVerifyTitle2(html);
92      }
93  
94      /**
95       * @throws Exception if an error occurs
96       */
97      @Test
98      @Alerts({"undefined", "undefined", "undefined", "undefined", "undefined", "undefined",
99               "undefined", "left", "none", "right", "all", "2", "abc", "8"})
100     public void clear() throws Exception {
101         final String html = DOCTYPE_HTML
102             + "<html><body>\n"
103             + "<p id='p1'>p1</p>\n"
104             + "<p id='p2' clear='left'>p2</p>\n"
105             + "<p id='p3' clear='all'>p3</p>\n"
106             + "<p id='p4' clear='right'>p4</p>\n"
107             + "<p id='p5' clear='none'>p5</p>\n"
108             + "<p id='p6' clear='2'>p6</p>\n"
109             + "<p id='p7' clear='foo'>p7</p>\n"
110             + "<script>\n"
111             + LOG_TITLE_FUNCTION
112             + "function set(p, value) {\n"
113             + "  try {\n"
114             + "    p.clear = value;\n"
115             + "  } catch(e) {\n"
116             + "    log('!');\n"
117             + "  }\n"
118             + "}\n"
119             + "var p1 = document.getElementById('p1');\n"
120             + "var p2 = document.getElementById('p2');\n"
121             + "var p3 = document.getElementById('p3');\n"
122             + "var p4 = document.getElementById('p4');\n"
123             + "var p5 = document.getElementById('p5');\n"
124             + "var p6 = document.getElementById('p6');\n"
125             + "var p7 = document.getElementById('p7');\n"
126             + "log(p1.clear);\n"
127             + "log(p2.clear);\n"
128             + "log(p3.clear);\n"
129             + "log(p4.clear);\n"
130             + "log(p5.clear);\n"
131             + "log(p6.clear);\n"
132             + "log(p7.clear);\n"
133             + "set(p1, 'left');\n"
134             + "set(p2, 'none');\n"
135             + "set(p3, 'right');\n"
136             + "set(p4, 'all');\n"
137             + "set(p5, 2);\n"
138             + "set(p6, 'abc');\n"
139             + "set(p7, '8');\n"
140             + "log(p1.clear);\n"
141             + "log(p2.clear);\n"
142             + "log(p3.clear);\n"
143             + "log(p4.clear);\n"
144             + "log(p5.clear);\n"
145             + "log(p6.clear);\n"
146             + "log(p7.clear);\n"
147             + "</script>\n"
148             + "</body></html>";
149 
150         loadPageVerifyTitle2(html);
151     }
152 }