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.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  import org.openqa.selenium.By;
21  import org.openqa.selenium.WebDriver;
22  
23  /**
24   * Unit tests for {@link HTMLSpanElement}.
25   *
26   * @author Daniel Gredler
27   * @author Marc Guillemot
28   * @author Ahmed Ashour
29   * @author Frank Danek
30   * @author Ronald Brill
31   */
32  public class HTMLSpanElementTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if an error occurs
36       */
37      @Test
38      @Alerts("no")
39      public void doScroll() throws Exception {
40          final String html = DOCTYPE_HTML
41              + "<html>\n"
42              + "  <head>\n"
43              + "    <script>\n"
44              + LOG_TITLE_FUNCTION
45              + "      function test() {\n"
46              + "        var span = document.getElementById('s');\n"
47              + "        if(span.doScroll) {\n"
48              + "          log('yes');\n"
49              + "          span.doScroll();\n"
50              + "          span.doScroll('down');\n"
51              + "        } else {\n"
52              + "          log('no');\n"
53              + "        }\n"
54              + "      }\n"
55              + "    </script>\n"
56              + "  </head>\n"
57              + "  <body onload='test()'><span id='s'>abc</span></body>\n"
58              + "</html>";
59  
60          loadPageVerifyTitle2(html);
61      }
62  
63      /**
64       * @throws Exception if an error occurs
65       */
66      @Test
67      @Alerts("[object HTMLSpanElement] undefined")
68      public void cite() throws Exception {
69          final String html = DOCTYPE_HTML
70              + "<html>\n"
71              + "  <head>\n"
72              + "    <script>\n"
73              + LOG_TITLE_FUNCTION
74              + "      function test() {\n"
75              + "        debug(document.createElement('span'));\n"
76              + "      }\n"
77              + "      function debug(e) {\n"
78              + "        log(e + ' ' + e.cite);\n"
79              + "      }\n"
80              + "    </script>\n"
81              + "  </head>\n"
82              + "  <body onload='test()'></body>\n"
83              + "</html>";
84  
85          loadPageVerifyTitle2(html);
86      }
87  
88      /**
89       * @throws Exception if the test fails
90       */
91      @Test
92      @Alerts("beforeSpace afterSpace")
93      public void getText() throws Exception {
94          final String html = DOCTYPE_HTML
95              + "<html><head></head><body>\n"
96              + "<div id='foo'><span>beforeSpace</span><span> </span><span>afterSpace</span></div>\n"
97              + "</body></html>";
98  
99          final WebDriver driver = loadPage2(html);
100         assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("foo")).getText());
101     }
102 }