1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.libraries;
16
17 import static java.nio.charset.StandardCharsets.ISO_8859_1;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.time.Duration;
22
23 import org.apache.commons.io.IOUtils;
24 import org.htmlunit.MockWebConnection;
25 import org.htmlunit.WebDriverTestCase;
26 import org.htmlunit.junit.annotation.Alerts;
27 import org.htmlunit.util.MimeType;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33
34
35
36 public class LibraryDependencyTest extends WebDriverTestCase {
37
38
39
40
41
42 @Alerts("2")
43 @Test
44 public void contextFactory_Browser() throws Exception {
45 final String firstHtml = DOCTYPE_HTML
46 + "<html>\n"
47 + "<head>\n"
48 + " <title>1</title>\n"
49 + " <script src='" + URL_THIRD + "' type='text/javascript'></script>\n"
50 + "</head>\n"
51 + "<body>\n"
52 + "<script>\n"
53 + " setTimeout(finishCreateAccount, 2500);\n"
54 + " function finishCreateAccount() {\n"
55 + " completionUrl = '" + URL_SECOND + "';\n"
56 + " document.location.replace(completionUrl);\n"
57 + " }\n"
58 + "</script>\n"
59 + "</body>\n"
60 + "</html>";
61 final String secondHtml = DOCTYPE_HTML
62 + "<html>\n"
63 + "<head>\n"
64 + " <title>2</title>\n"
65 + " <script src='" + URL_THIRD + "' type='text/javascript'></script>\n"
66 + "</head>\n"
67 + "<body onload='alert(2)'>\n"
68 + "<div id='id2'>Page2</div>\n"
69 + "</body>\n"
70 + "</html>";
71 final String prototype = getContent("libraries/prototype/1.6.0/dist/prototype.js");
72
73 final MockWebConnection webConnection = getMockWebConnection();
74 webConnection.setResponse(URL_SECOND, secondHtml);
75 webConnection.setResponse(URL_THIRD, prototype, MimeType.TEXT_JAVASCRIPT);
76
77 loadPageWithAlerts2(firstHtml, Duration.ofMillis(10_000));
78 }
79
80 private String getContent(final String resourceName) throws IOException {
81 try (InputStream in = getClass().getClassLoader().getResourceAsStream(resourceName)) {
82 return IOUtils.toString(in, ISO_8859_1);
83 }
84 }
85 }