1
2
3
4
5
6
7
8
9
10
11
12
13
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
35
36
37
38
39
40 @RunWith(BrowserRunner.class)
41 public class HTMLCollection2Test extends SimpleWebTestCase {
42
43
44
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 }