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.BrowserRunner;
27 import org.htmlunit.junit.annotation.Alerts;
28 import org.htmlunit.util.MimeType;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31
32
33
34
35
36
37
38 @RunWith(BrowserRunner.class)
39 public class LibraryDependencyTest extends WebDriverTestCase {
40
41
42
43
44
45 @Alerts("2")
46 @Test
47 public void contextFactory_Browser() throws Exception {
48 final String firstHtml = DOCTYPE_HTML
49 + "<html>\n"
50 + "<head>\n"
51 + " <title>1</title>\n"
52 + " <script src='" + URL_THIRD + "' type='text/javascript'></script>\n"
53 + "</head>\n"
54 + "<body>\n"
55 + "<script>\n"
56 + " setTimeout(finishCreateAccount, 2500);\n"
57 + " function finishCreateAccount() {\n"
58 + " completionUrl = '" + URL_SECOND + "';\n"
59 + " document.location.replace(completionUrl);\n"
60 + " }\n"
61 + "</script>\n"
62 + "</body>\n"
63 + "</html>";
64 final String secondHtml = DOCTYPE_HTML
65 + "<html>\n"
66 + "<head>\n"
67 + " <title>2</title>\n"
68 + " <script src='" + URL_THIRD + "' type='text/javascript'></script>\n"
69 + "</head>\n"
70 + "<body onload='alert(2)'>\n"
71 + "<div id='id2'>Page2</div>\n"
72 + "</body>\n"
73 + "</html>";
74 final String prototype = getContent("libraries/prototype/1.6.0/dist/prototype.js");
75
76 final MockWebConnection webConnection = getMockWebConnection();
77 webConnection.setResponse(URL_SECOND, secondHtml);
78 webConnection.setResponse(URL_THIRD, prototype, MimeType.TEXT_JAVASCRIPT);
79
80 loadPageWithAlerts2(firstHtml, Duration.ofMillis(10_000));
81 }
82
83 private String getContent(final String resourceName) throws IOException {
84 try (InputStream in = getClass().getClassLoader().getResourceAsStream(resourceName)) {
85 return IOUtils.toString(in, ISO_8859_1);
86 }
87 }
88 }