MCPcopy Index your code
hub / github.com/HtmlUnit/htmlunit / usage

Method usage

src/test/java/org/htmlunit/CacheTest.java:213–262  ·  view source on GitHub ↗

@throws Exception if the test fails

()

Source from the content-addressed store, hash-verified

211 * @throws Exception if the test fails
212 */
213 @Test
214 public void usage() throws Exception {
215 final String content = DOCTYPE_HTML
216 + "<html><head><title>page 1</title>\n"
217 + "<script src='foo1.js'></script>\n"
218 + "<script src='foo2.js'></script>\n"
219 + "</head><body>\n"
220 + "<a href='page2.html'>to page 2</a>\n"
221 + "</body></html>";
222
223 final String content2 = DOCTYPE_HTML
224 + "<html><head><title>page 2</title>\n"
225 + "<script src='foo2.js'></script>\n"
226 + "</head><body>\n"
227 + "<a href='page1.html'>to page 1</a>\n"
228 + "</body></html>";
229
230 final String script1 = "alert('in foo1');";
231 final String script2 = "alert('in foo2');";
232
233 final WebClient webClient = getWebClient();
234 final MockWebConnection connection = new MockWebConnection();
235 webClient.setWebConnection(connection);
236
237 final URL urlPage1 = new URL(URL_FIRST, "page1.html");
238 connection.setResponse(urlPage1, content);
239 final URL urlPage2 = new URL(URL_FIRST, "page2.html");
240 connection.setResponse(urlPage2, content2);
241
242 final List<NameValuePair> headers = new ArrayList<>();
243 headers.add(new NameValuePair(LAST_MODIFIED, "Sun, 15 Jul 2007 20:46:27 GMT"));
244 connection.setResponse(new URL(URL_FIRST, "foo1.js"), script1, 200, "ok",
245 MimeType.TEXT_JAVASCRIPT, headers);
246 connection.setResponse(new URL(URL_FIRST, "foo2.js"), script2, 200, "ok",
247 MimeType.TEXT_JAVASCRIPT, headers);
248
249 final List<String> collectedAlerts = new ArrayList<>();
250 webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
251
252 final HtmlPage page1 = webClient.getPage(urlPage1);
253 final String[] expectedAlerts = {"in foo1", "in foo2"};
254 assertEquals(expectedAlerts, collectedAlerts);
255
256 collectedAlerts.clear();
257 page1.getAnchors().get(0).click();
258
259 assertEquals(new String[] {"in foo2"}, collectedAlerts);
260 assertEquals("no request for scripts should have been performed",
261 urlPage2, connection.getLastWebRequest().getUrl());
262 }
263
264 /**
265 * @throws Exception if the test fails

Callers

nothing calls this directly

Calls 13

setWebConnectionMethod · 0.95
setResponseMethod · 0.95
setAlertHandlerMethod · 0.95
getPageMethod · 0.95
getAnchorsMethod · 0.95
getLastWebRequestMethod · 0.95
getWebClientMethod · 0.65
getUrlMethod · 0.65
addMethod · 0.45
assertEqualsMethod · 0.45
clearMethod · 0.45
clickMethod · 0.45

Tested by

no test coverage detected