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