1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import java.net.URL;
18 import java.time.Duration;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.htmlunit.html.HtmlPage;
25 import org.htmlunit.javascript.JavaScriptEngine;
26 import org.junit.After;
27 import org.junit.Before;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public abstract class SimpleWebTestCase extends WebTestCase {
47
48 private WebClient webClient_;
49
50
51
52
53
54
55
56 public final HtmlPage loadPage(final String html) throws Exception {
57 return loadPage(html, null);
58 }
59
60
61
62
63
64
65
66
67
68 public final HtmlPage loadPage(final String html, final List<String> collectedAlerts) throws Exception {
69 return loadPage(getBrowserVersion(), html, collectedAlerts, URL_FIRST);
70 }
71
72
73
74
75
76
77
78
79
80 protected final HtmlPage loadPage(final String html, final List<String> collectedAlerts,
81 final URL url) throws Exception {
82
83 return loadPage(BrowserVersion.getDefault(), html, collectedAlerts, url);
84 }
85
86
87
88
89
90
91
92
93
94
95 protected final HtmlPage loadPage(final BrowserVersion browserVersion,
96 final String html, final List<String> collectedAlerts, final URL url) throws Exception {
97
98 if (webClient_ == null) {
99 webClient_ = new WebClient(browserVersion);
100 }
101 return loadPage(webClient_, html, collectedAlerts, url);
102 }
103
104
105
106
107
108
109
110
111
112
113 protected final HtmlPage loadPage(final WebClient client,
114 final String html, final List<String> collectedAlerts, final URL url) throws Exception {
115
116 if (collectedAlerts != null) {
117 client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
118 }
119
120 final MockWebConnection webConnection = getMockWebConnection();
121 webConnection.setDefaultResponse(html);
122 client.setWebConnection(webConnection);
123
124 return client.getPage(url);
125 }
126
127
128
129
130
131
132
133
134
135 protected final HtmlPage loadPage(final WebClient client,
136 final String html, final List<String> collectedAlerts) throws Exception {
137
138 return loadPage(client, html, collectedAlerts, URL_FIRST);
139 }
140
141
142
143
144
145
146
147 protected static final MockWebConnection getMockConnection(final HtmlPage page) {
148 return (MockWebConnection) page.getWebClient().getWebConnection();
149 }
150
151
152
153
154
155 protected WebClient createNewWebClient() {
156 final WebClient webClient = new WebClient(getBrowserVersion());
157 return webClient;
158 }
159
160
161
162
163
164 protected final WebClient getWebClient() {
165 if (webClient_ == null) {
166 webClient_ = createNewWebClient();
167 }
168 return webClient_;
169 }
170
171
172
173
174
175 protected final WebClient getWebClientWithMockWebConnection() {
176 if (webClient_ == null) {
177 webClient_ = createNewWebClient();
178 webClient_.setWebConnection(getMockWebConnection());
179 }
180 return webClient_;
181 }
182
183
184
185
186
187
188
189
190
191 protected final HtmlPage loadPageWithAlerts(final String html) throws Exception {
192 return loadPageWithAlerts(html, URL_FIRST, null);
193 }
194
195
196
197
198
199
200
201
202
203
204
205 protected final HtmlPage loadPageWithAlerts(final String html, final URL url, final Duration waitForJS)
206 throws Exception {
207 if (getExpectedAlerts() == null) {
208 throw new IllegalStateException("You must annotate the test class with '@RunWith(BrowserRunner.class)'");
209 }
210
211
212 expandExpectedAlertsVariables(url);
213
214 final WebClient client = getWebClientWithMockWebConnection();
215 final List<String> collectedAlerts = new ArrayList<>();
216 client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
217
218 final MockWebConnection webConnection = getMockWebConnection();
219 webConnection.setResponse(url, html);
220
221 final HtmlPage page = client.getPage(url);
222 if (waitForJS != null) {
223 assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(waitForJS.toMillis()));
224 }
225 assertEquals(getExpectedAlerts(), collectedAlerts);
226 return page;
227 }
228
229
230
231
232
233 @Before
234 public void before() {
235 if (webClient_ != null && webClient_.getJavaScriptEngine() instanceof JavaScriptEngine) {
236 assertTrue(getJavaScriptThreads().isEmpty());
237 }
238 }
239
240
241
242
243 @Override
244 @After
245 public void releaseResources() {
246 super.releaseResources();
247 boolean rhino = false;
248 if (webClient_ != null) {
249 rhino = webClient_.getJavaScriptEngine() instanceof JavaScriptEngine;
250 webClient_.close();
251 webClient_.getCookieManager().clearCookies();
252 }
253 webClient_ = null;
254
255 if (rhino) {
256 final List<Thread> jsThreads = getJavaScriptThreads();
257 assertEquals(0, jsThreads.size());
258
259
260
261
262 if (jsThreads.size() > 0) {
263 final Map<String, StackTraceElement[]> stackTraces = new HashMap<>();
264 for (final Thread t : jsThreads) {
265 final StackTraceElement[] elts = t.getStackTrace();
266 if (elts != null) {
267 stackTraces.put(t.getName(), elts);
268 }
269 }
270
271 if (!stackTraces.isEmpty()) {
272 System.err.println("JS threads still running:");
273 for (final Map.Entry<String, StackTraceElement[]> entry : stackTraces.entrySet()) {
274 System.err.println("Thread: " + entry.getKey());
275 final StackTraceElement[] elts = entry.getValue();
276 for (final StackTraceElement elt : elts) {
277 System.err.println(elt);
278 }
279 }
280 throw new RuntimeException("JS threads are still running: " + jsThreads.size());
281 }
282 }
283 }
284 }
285 }