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