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