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 static org.htmlunit.javascript.host.xml.XMLDocumentTest.LOAD_XML_DOCUMENT_FROM_FILE_FUNCTION;
18  import static org.htmlunit.javascript.host.xml.XMLDocumentTest.callLoadXMLDocumentFromFile;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.htmlunit.CollectingAlertHandler;
24  import org.htmlunit.MockWebConnection;
25  import org.htmlunit.SimpleWebTestCase;
26  import org.htmlunit.WebClient;
27  import org.htmlunit.junit.annotation.Alerts;
28  import org.htmlunit.util.MimeType;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests for {@link HTMLCollection}.
33   *
34   * @author Marc Guillemot
35   * @author Ahmed Ashour
36   * @author Frank Danek
37   */
38  public class HTMLCollection2Test extends SimpleWebTestCase {
39  
40      /**
41       * @throws Exception if the test fails
42       */
43      @Test
44      @Alerts("1")
45      public void childNodes() throws Exception {
46          final String firstContent = DOCTYPE_HTML
47              + "<html><head><title>foo</title><script>\n"
48              + "  function test() {\n"
49              + "    var doc = " + callLoadXMLDocumentFromFile("'" + URL_SECOND + "'") + ";\n"
50              + "    alert(doc.documentElement.childNodes.length);\n"
51              + "  }\n"
52              + LOAD_XML_DOCUMENT_FROM_FILE_FUNCTION
53              + "</script></head><body onload='test()'>\n"
54              + "</body></html>";
55  
56          final String secondContent = "<title>Immortality</title>";
57  
58          final List<String> collectedAlerts = new ArrayList<>();
59          final WebClient client = getWebClient();
60          client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
61          final MockWebConnection conn = new MockWebConnection();
62          conn.setResponse(URL_FIRST, firstContent);
63          conn.setResponse(URL_SECOND, secondContent, MimeType.TEXT_XML);
64          client.setWebConnection(conn);
65  
66          client.getPage(URL_FIRST);
67          assertEquals(getExpectedAlerts(), collectedAlerts);
68      }
69  }