1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.net.URL;
18
19 import org.htmlunit.SimpleWebTestCase;
20 import org.htmlunit.junit.annotation.Alerts;
21 import org.junit.jupiter.api.Test;
22
23
24
25
26
27
28
29 public class HtmlLinkTest extends SimpleWebTestCase {
30
31
32
33
34 @Test
35 @Alerts("body onLoad")
36 public void onLoad() throws Exception {
37 getWebClientWithMockWebConnection().getOptions().setCssEnabled(false);
38 getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.css"), "");
39
40 final String html = DOCTYPE_HTML
41 + "<html>\n"
42 + "<head>\n"
43 + " <link rel='stylesheet' href='simple.css' "
44 + "onload='alert(\"onLoad\")' onerror='alert(\"onError\")'>\n"
45 + "</head>\n"
46 + "<body onload='alert(\"body onLoad\")'>\n"
47 + "</body>\n"
48 + "</html>";
49
50 loadPageWithAlerts(html);
51 assertEquals(1, getMockWebConnection().getRequestCount());
52 }
53
54
55
56
57 @Test
58 public void onLoadDynamic() throws Exception {
59 getWebClientWithMockWebConnection().getOptions().setCssEnabled(false);
60 getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.css"), "");
61
62 final String html = DOCTYPE_HTML
63 + "<html>\n"
64 + "<head>\n"
65 + " <script>\n"
66 + " function test() {\n"
67 + " var dynLink = document.createElement('link');\n"
68 + " dynLink.rel = 'stylesheet';\n"
69 + " dynLink.type = 'text/css';\n"
70 + " dynLink.href = 'simple.css';"
71 + " dynLink.onload = function (e) { log(\"onLoad \" + e) };\n"
72 + " dynLink.onerror = function (e) { log(\"onError \" + e) };\n"
73 + " document.head.appendChild(dynLink);\n"
74
75 + " var dynLink = document.createElement('link');\n"
76 + " dynLink.rel = 'stylesheet';\n"
77 + " dynLink.type = 'text/css';\n"
78 + " dynLink.href = 'unknown.css';"
79 + " dynLink.onload = function (e) { log(\"onLoad \" + e) };\n"
80 + " dynLink.onerror = function (e) { log(\"onError \" + e) };\n"
81 + " document.head.appendChild(dynLink);\n"
82 + " }\n"
83
84 + " function log(x) {\n"
85 + " document.getElementById('log').value += x + '\\n';\n"
86 + " }\n"
87 + " </script>\n"
88 + "</head>\n"
89 + "<body onload='test()'></body>\n"
90 + " <textarea id='log' cols='80' rows='40'></textarea>\n"
91 + "</body>\n"
92 + "</html>";
93
94 final HtmlPage page = loadPageWithAlerts(html);
95 final String text = page.getElementById("log").getAttribute("value").trim().replaceAll("\r", "");
96 assertEquals(String.join("\n", getExpectedAlerts()), text);
97
98 assertEquals(1, getMockWebConnection().getRequestCount());
99 }
100 }