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