1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript;
16
17 import java.net.URL;
18
19 import org.htmlunit.MockWebConnection;
20 import org.htmlunit.WebDriverTestCase;
21 import org.htmlunit.junit.BrowserRunner;
22 import org.htmlunit.junit.annotation.Alerts;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.openqa.selenium.WebDriver;
26
27
28
29
30
31
32
33
34
35 @RunWith(BrowserRunner.class)
36 public class PostponedActionTest extends WebDriverTestCase {
37
38
39
40
41 @Test
42 @Alerts("before after second.html third.html")
43 public void loadingJavaScript() throws Exception {
44 final String html = DOCTYPE_HTML
45 + "<html>\n"
46 + "<head>\n"
47 + "<script>\n"
48 + "function test() {\n"
49 + " document.title += ' before';\n"
50 + " var iframe2 = document.createElement('iframe');\n"
51 + " iframe2.src = 'frame2.html';\n"
52 + " document.body.appendChild(iframe2);\n"
53 + " var iframe3 = document.createElement('iframe');\n"
54 + " document.body.appendChild(iframe3);\n"
55 + " iframe3.src = 'frame3.html';\n"
56 + " document.title += ' after';\n"
57 + "}\n"
58 + "</script>\n"
59 + "</head>\n"
60 + "<body onload='test()'>\n"
61 + "</body>\n"
62 + "</html>";
63 final String secondContent
64 = "<script>top.document.title += ' second.html';</script>";
65 final String thirdContent
66 = "<script>top.document.title += ' third.html';</script>";
67
68 final MockWebConnection conn = getMockWebConnection();
69 conn.setResponse(new URL(URL_FIRST, "frame2.html"), secondContent);
70 conn.setResponse(new URL(URL_FIRST, "frame3.html"), thirdContent);
71
72 final WebDriver driver = loadPage2(html);
73 assertTitle(driver, getExpectedAlerts()[0]);
74 }
75
76
77
78
79 @Test
80 @Alerts("before after second.html")
81 public void loadingJavaScript2() throws Exception {
82 final String firstContent = DOCTYPE_HTML
83 + "<html>\n"
84 + "<head>\n"
85 + "<script>\n"
86 + "function test() {\n"
87 + " document.title += ' before';\n"
88 + " var iframe = document.createElement('iframe');\n"
89 + " document.body.appendChild(iframe);\n"
90 + " iframe.contentWindow.location.replace('" + URL_SECOND + "');\n"
91 + " document.title += ' after';\n"
92 + "}\n"
93 + "</script>\n"
94 + "</head>\n"
95 + "<body onload='test()'>\n"
96 + "</body>\n"
97 + "</html>";
98 final String secondContent
99 = "<script>top.document.title += ' second.html';</script>";
100
101 getMockWebConnection().setResponse(URL_SECOND, secondContent);
102
103 final WebDriver driver = loadPage2(firstContent);
104 assertTitle(driver, getExpectedAlerts()[0]);
105 }
106
107
108
109
110
111
112 @Test
113 @Alerts("setting timeout before after iframe.html simpleAlert")
114 public void loadingJavaScriptWithTimeout() throws Exception {
115 final String html = DOCTYPE_HTML
116 + "<html>\n"
117 + "<head>\n"
118 + "<script>\n"
119 + "function test() {\n"
120 + " document.title += ' before';\n"
121 + " var iframe = document.createElement('iframe');\n"
122 + " iframe.src = 'iframe.html';\n"
123 + " document.body.appendChild(iframe);\n"
124 + " document.title += ' after';\n"
125 + "}\n"
126 + "function timeout() {\n"
127 + " document.title += ' setting timeout';\n"
128 + " window.setTimeout(function() {test()}, 400);\n"
129 + " window.setTimeout(function() {top.document.title += ' simpleAlert'}, 500);\n"
130 + "}\n"
131 + "</script>\n"
132 + "</head>\n"
133 + "<body onload='timeout()'>\n"
134 + "</body>\n"
135 + "</html>";
136 final String secondContent
137 = "<script>top.document.title += ' iframe.html'</script>";
138
139 final MockWebConnection conn = getMockWebConnection();
140 conn.setResponse(new URL(URL_FIRST, "iframe.html"), secondContent);
141
142 final WebDriver driver = loadPage2(html);
143 assertTitle(driver, getExpectedAlerts()[0]);
144 }
145
146
147
148
149 @Test
150 @Alerts("page 2 / script executed")
151 public void javascriptReload() throws Exception {
152 final String html = DOCTYPE_HTML
153 + "<html>\n"
154 + "<head>\n"
155 + "</head>\n"
156 + "<body onload='document.forms[0].submit()'>\n"
157 + " <form action='step2.html' method='post'>\n"
158 + " </form>"
159 + "</body>\n"
160 + "</html>";
161
162 final String secondContent = DOCTYPE_HTML
163 + "<html><head>\n"
164 + "<title>page 2</title>\n"
165 + "</head>\n"
166 + "<body>\n"
167 + "<script async>document.title += ' / script executed§';</script>\n"
168 + "</body>\n"
169 + "</html>";
170
171 final MockWebConnection conn = getMockWebConnection();
172 getMockWebConnection().setResponse(new URL(URL_FIRST, "step2.html"), secondContent);
173
174 final WebDriver driver = loadPage2(html);
175 verifyTitle2(DEFAULT_WAIT_TIME, driver, getExpectedAlerts()[0]);
176 }
177 }