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.htmlunit.util.MimeType;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Unit tests for {@link HTMLObjectElement}.
24   *
25   * @author Daniel Gredler
26   * @author Ahmed Ashour
27   * @author Ronald Brill
28   * @author Frank Danek
29   */
30  public class HTMLObjectElement2Test extends WebDriverTestCase {
31  
32      /**
33       * @throws Exception if the test fails
34       */
35      @Test
36      @Alerts({"[object HTMLFormElement]", "null"})
37      public void form() throws Exception {
38          final String html = DOCTYPE_HTML
39              + "<html>\n"
40              + "<body>\n"
41              + "  <form>\n"
42              + "    <object id='o1'></object>\n"
43              + "</form>\n"
44              + "<object id='o2'></object>\n"
45              + "<script>\n"
46              + LOG_TITLE_FUNCTION
47              + "  log(document.getElementById('o1').form);\n"
48              + "  log(document.getElementById('o2').form);\n"
49              + "</script>\n"
50              + "</body></html>";
51  
52          loadPageVerifyTitle2(html);
53      }
54  
55      /**
56       * There was an error that this code throws an illegal state ex.
57       * @throws Exception if the test fails
58       */
59      @Test
60      @Alerts("[object XMLDocument]")
61      public void responseXML_htmlObject() throws Exception {
62          final String html = DOCTYPE_HTML
63              + "<html><head><script>\n"
64              + LOG_TITLE_FUNCTION
65              + "  function test() {\n"
66              + "    var xhr = new XMLHttpRequest();\n"
67              + "    xhr.open('GET', 'foo.xml', false);\n"
68              + "    xhr.send('');\n"
69              + "    try {\n"
70              + "      log(xhr.responseXML);\n"
71              + "    } catch(e) { logEx(e); }\n"
72              + "  }\n"
73              + "</script></head><body onload='test()'>\n"
74              + "</body></html>";
75  
76          final String xml = "<html xmlns='http://www.w3.org/1999/xhtml'>\n"
77                      + "<object classid='CLSID:test'/>\n"
78                      + "</html>";
79  
80          getMockWebConnection().setDefaultResponse(xml, MimeType.TEXT_XML);
81          loadPage2(html);
82          verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
83      }
84  }