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.xml;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.javascript.host.xml.XMLDocumentTest;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.htmlunit.util.MimeType;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link XmlPage}.
25   *
26   * @author Ahmed Ashour
27   * @author Frank Danek
28   */
29  public class XmlPage2Test extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts("8")
36      public void load_XMLComment() throws Exception {
37          final String html = "<html><head>\n"
38              + "<script>\n"
39              + LOG_TITLE_FUNCTION
40              + "  function test() {\n"
41              + "    var doc = " + XMLDocumentTest.callLoadXMLDocumentFromFile("'" + URL_SECOND + "'") + ";\n"
42              + "    log(doc.documentElement.childNodes[0].nodeType);\n"
43              + "  }\n"
44              + XMLDocumentTest.LOAD_XML_DOCUMENT_FROM_FILE_FUNCTION
45              + "</script></head><body onload='test()'>\n"
46              + "</body></html>";
47  
48          final String xml = "<test><!-- --></test>";
49  
50          getMockWebConnection().setResponse(URL_SECOND, xml, MimeType.TEXT_XML);
51          loadPageVerifyTitle2(html);
52      }
53  
54      /**
55       * @throws Exception if the test fails
56       */
57      @Test
58      @Alerts({ "true", "14"})
59      public void createElement() throws Exception {
60          final String html = "<html><head>\n"
61              + "<script>\n"
62              + LOG_TITLE_FUNCTION
63              + "  function test() {\n"
64              + "    var doc = document.implementation.createDocument('', '', null);\n"
65              + "    doc.appendChild(doc.createElement('elementName'));\n"
66              + "    var xml = " + XMLDocumentTest.callSerializeXMLDocumentToString("doc") + ";\n"
67              + "    log(xml.indexOf('<elementName') != -1);\n"
68              + "    log(xml.length);\n"
69              + "  }\n"
70              + XMLDocumentTest.SERIALIZE_XML_DOCUMENT_TO_STRING_FUNCTION
71              + "</script></head><body onload='test()'>\n"
72              + "</body></html>";
73  
74          loadPageVerifyTitle2(html);
75      }
76  
77      /**
78       * @throws Exception if the test fails
79       */
80      @Test
81      @Alerts("[object Element]")
82      public void createElementNS() throws Exception {
83          final String html = "<html><head>\n"
84              + "<script>\n"
85              + LOG_TITLE_FUNCTION
86              + "  function test() {\n"
87              + "    var doc = document.implementation.createDocument('', '', null);\n"
88              + "    try {\n"
89              + "      log(doc.createElementNS('myNS', 'ppp:eee'));\n"
90              + "    } catch(e) { logEx(e) }\n"
91              + "  }\n"
92              + "</script></head><body onload='test()'>\n"
93              + "</body></html>";
94  
95          loadPageVerifyTitle2(html);
96      }
97  
98  }