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