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.WebDriver;
23  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
24  
25  /**
26   * Tests for {@link HtmlUnknownElement}.
27   *
28   * @author Ahmed Ashour
29   * @author Frank Danek
30   */
31  @RunWith(BrowserRunner.class)
32  public class HtmlUnknownElementTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts({"[object HTMLUnknownElement]", "[object HTMLUnknownElement]", "[object HTMLElement]"})
39      public void simpleScriptable() throws Exception {
40          final String html = DOCTYPE_HTML
41              + "<html><head>\n"
42              + "<script>\n"
43              + LOG_TITLE_FUNCTION
44              + "  function test() {\n"
45              + "    log(document.getElementById('myId1'));\n"
46              + "    log(document.getElementById('myId2'));\n"
47              + "    log(document.getElementById('myId3'));\n"
48              + "  }\n"
49              + "</script>\n"
50              + "</head><body onload='test()'>\n"
51              + "<xml id='myId1'/>\n"
52              + "<doesnt_exist id='myId2'/>\n"
53              + "<doesnt-exist id='myId3'/>\n"
54              + "</body></html>";
55  
56          final WebDriver driver = loadPageVerifyTitle2(html);
57          if (driver instanceof HtmlUnitDriver) {
58              final HtmlPage page = (HtmlPage) getEnclosedPage();
59              assertTrue(HtmlUnknownElement.class.isInstance(page.getHtmlElementById("myId1")));
60              assertTrue(HtmlUnknownElement.class.isInstance(page.getHtmlElementById("myId2")));
61              assertTrue(HtmlUnknownElement.class.isInstance(page.getHtmlElementById("myId3")));
62          }
63      }
64  
65      /**
66       * @throws Exception if the test fails
67       */
68      @Test
69      @Alerts({"[object HTMLUnknownElement]", "[object HTMLUnknownElement]", "[object HTMLElement]"})
70      public void simpleScriptable_strict() throws Exception {
71          final String header = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" "
72                  + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
73          final String html = header + "<html><head>\n"
74              + "<script>\n"
75              + LOG_TITLE_FUNCTION
76              + "  function test() {\n"
77              + "    log(document.getElementById('myId1'));\n"
78              + "    log(document.getElementById('myId2'));\n"
79              + "    log(document.getElementById('myId3'));\n"
80              + "  }\n"
81              + "</script>\n"
82              + "</head><body onload='test()'>\n"
83              + "<xml id='myId1'/>\n"
84              + "<doesnt_exist id='myId2'/>\n"
85              + "<doesnt-exist id='myId3'/>\n"
86              + "</body></html>";
87  
88          final WebDriver driver = loadPageVerifyTitle2(html);
89          if (driver instanceof HtmlUnitDriver) {
90              final HtmlPage page = (HtmlPage) getEnclosedPage();
91              assertTrue(HtmlUnknownElement.class.isInstance(page.getHtmlElementById("myId1")));
92              assertTrue(HtmlUnknownElement.class.isInstance(page.getHtmlElementById("myId2")));
93              assertTrue(HtmlUnknownElement.class.isInstance(page.getHtmlElementById("myId3")));
94          }
95      }
96  
97      /**
98       * @throws Exception if the test fails
99       */
100     @Test
101     public void asXml() throws Exception {
102         final String html = DOCTYPE_HTML
103             + "<html><body><title>foo</title>\n"
104             + "<foo></foo>\n"
105             + "</body></html>";
106 
107         final WebDriver driver = loadPage2(html);
108 
109         final String xml = driver.getPageSource();
110         assertTrue("Node not expanded in: " + xml, xml.contains("</foo>"));
111     }
112 }