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.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  import org.openqa.selenium.By;
21  import org.openqa.selenium.WebDriver;
22  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
23  
24  /**
25   * Tests for {@link HtmlTable}.
26   *
27   * @author Ahmed Ashour
28   * @author Ronald Brill
29   */
30  public class HtmlTable2Test extends WebDriverTestCase {
31  
32      /**
33       * Verifies getVisibleText().
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts("One Two\n1 2")
38      public void getVisibleText() throws Exception {
39          final String htmlContent = DOCTYPE_HTML
40              + "<html>\n"
41              + "<head></head>\n"
42              + "<body>\n"
43              + "  <table id='tester'>"
44              + "    <tr><th>One</th><th>Two</th></tr>"
45              + "    <tr><td>1</td><td>2</td></tr>"
46              + "  </table>\n"
47              + "</body></html>";
48  
49          final WebDriver driver = loadPage2(htmlContent);
50          final String text = driver.findElement(By.id("tester")).getText();
51          assertEquals(getExpectedAlerts()[0], text);
52  
53          if (driver instanceof HtmlUnitDriver) {
54              final HtmlPage page = (HtmlPage) getEnclosedPage();
55              assertEquals(getExpectedAlerts()[0], page.getBody().getVisibleText());
56          }
57      }
58  
59      /**
60       * @throws Exception if an error occurs
61       */
62      @Test
63      @Alerts("true")
64      public void cellWidth() throws Exception {
65          final String htmlContent = DOCTYPE_HTML
66              + "<html>\n"
67              + "<head>"
68              + "<script>\n"
69              + LOG_TITLE_FUNCTION
70              + "  function test() {\n"
71              + "    var th = document.getElementById('tester');\n"
72              + "    log(th.clientWidth < 100);\n"
73              + "  }\n"
74              + "</script>\n"
75              + "</head>\n"
76              + "<body onload='test()'>\n"
77              + "  <table id='dataTable'>"
78              + "    <thead>\n"
79              + "      <tr>\n"
80              + "        <th id='tester'>Head\n"
81              + "          \n"
82              + "          \n"
83              + "          1\n"
84              + "        </th>\n"
85              + "      </tr>\n"
86              + "    </thead>\n"
87              + "  <tbody>\n"
88              + "</tbody>\n"
89              + "</table>\n"
90              + "</body></html>";
91  
92          loadPageVerifyTitle2(htmlContent);
93      }
94  
95      /**
96       * @throws Exception if the test fails
97       */
98      @Test
99      @Alerts("[object HTMLTableElement]")
100     public void simpleScriptable() throws Exception {
101         final String html = DOCTYPE_HTML
102             + "<html><head>\n"
103             + "<script>\n"
104             + LOG_TITLE_FUNCTION
105             + "  function test() {\n"
106             + "    log(document.getElementById('myId'));\n"
107             + "  }\n"
108             + "</script>\n"
109             + "</head><body onload='test()'>\n"
110             + "  <table id='myId'/>\n"
111             + "</body></html>";
112 
113         final WebDriver driver = loadPageVerifyTitle2(html);
114         if (driver instanceof HtmlUnitDriver) {
115             final HtmlPage page = (HtmlPage) getEnclosedPage();
116             assertTrue(HtmlTable.class.isInstance(page.getHtmlElementById("myId")));
117         }
118     }
119 
120     /**
121      * Table can have multiple children of &lt;thead&gt;, &lt;tbody&gt; and &lt;tfoot&gt;.
122      * Also, IE adds TR between THEAD and TD if missing.
123      * @throws Exception if the test fails
124      */
125     @Test
126     @Alerts({"TBODY->TR->TD->Two", "THEAD->TR->TD->One", "THEAD->TR->TD->Three"})
127     public void two_theads() throws Exception {
128         final String html = DOCTYPE_HTML
129             + "<html><head>\n"
130             + "<script>\n"
131             + LOG_TITLE_FUNCTION
132             + "  function test() {\n"
133             + "    for (var child = myTable1.firstChild; child != null; child = child.nextSibling) {\n"
134             + "      log(debug(child));\n"
135             + "    }\n"
136             + "  }\n"
137             + "  function debug(node) {\n"
138             + "    return node.nodeValue != null ? node.nodeValue : (node.nodeName + '->' + debug(node.firstChild));\n"
139             + "  }\n"
140             + "</script></head>\n"
141             + "<body onload='test()'>\n"
142             + "<table id='myTable1'>"
143             + "<td>Two</td>"
144             + "<thead><td>One</td></thead>"
145             + "<thead><tr><td>Three</td></tr></thead>"
146             + "</table>\n"
147             + "</body></html>";
148 
149         loadPageVerifyTitle2(html);
150     }
151 
152     /**
153      * Regression test for Bug #274: JavaScript inside <tt>&lt;table&gt;</tt> run twice.
154      * @throws Exception if the test fails
155      */
156     @Test
157     @Alerts({"foo", "BODY"})
158     public void jsInTable() throws Exception {
159         final String content = DOCTYPE_HTML
160             + "<html>\n"
161             + "<head>\n"
162             + "<script>\n"
163             + LOG_TITLE_FUNCTION
164             + "</script>\n"
165             + "</head>\n"
166             + "<body>\n"
167             + "<table>\n"
168             + "<tr><td>cell1</td></tr>\n"
169             + "<script>log('foo');</script>\n"
170             + "<tr><td>cell1</td></tr>\n"
171             + "</table>\n"
172             + "<div id='div1'>foo</div>\n"
173             + "<script>log(document.getElementById('div1').parentNode.tagName);</script>\n"
174             + "</body></html>";
175 
176         loadPageVerifyTitle2(content);
177     }
178 }