use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HTMLDocumentWriteTest method writeScriptInManyTimes.
/**
* @throws Exception if the test fails
*/
@Test
public void writeScriptInManyTimes() throws Exception {
final String html = "<html><head><title>foo</title>\n" + "<script>\n" + "document.write('<script src=\"script.js\">');\n" + "document.write('<' + '/script>');\n" + "document.write('<script>alert(\"foo2\");</' + 'script>');\n" + "</script>\n" + "</head>\n" + "<body>\n" + "</body></html>";
final String[] expectedAlerts = { "foo", "foo2" };
final URL scriptUrl = new URL(URL_FIRST, "script.js");
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
client.setWebConnection(webConnection);
webConnection.setDefaultResponse(html);
webConnection.setResponse(scriptUrl, "alert('foo');\n", MimeType.APPLICATION_JAVASCRIPT);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
client.getPage(URL_FIRST);
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HTMLDocument2Test method cookie_read.
/**
* @throws Exception if the test fails
*/
@Test
@Alerts({ "one", "two", "three", "four" })
public void cookie_read() throws Exception {
final WebClient webClient = getWebClientWithMockWebConnection();
final String html = "<html><head><title>First</title><script>\n" + "function doTest() {\n" + " var cookieSet = document.cookie.split('; ');\n" + " var setSize = cookieSet.length;\n" + " var crumbs;\n" + " for (var x = 0; x < setSize; x++) {\n" + " crumbs = cookieSet[x].split('=');\n" + " alert(crumbs[0]);\n" + " alert(crumbs[1]);\n" + " }\n" + "}\n" + "</script></head><body onload='doTest()'>\n" + "</body></html>";
final URL url = URL_FIRST;
getMockWebConnection().setResponse(url, html);
final CookieManager mgr = webClient.getCookieManager();
mgr.addCookie(new Cookie(url.getHost(), "one", "two", "/", null, false));
mgr.addCookie(new Cookie(url.getHost(), "three", "four", "/", null, false));
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage firstPage = webClient.getPage(url);
assertEquals("First", firstPage.getTitleText());
assertEquals(getExpectedAlerts(), collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class JavaScriptEngineTest method javaScriptEngineCallsForVariableAccess.
/**
* Test that the JavaScript engine gets called correctly for variable access.
* @throws Exception if the test fails
*/
@Test
public void javaScriptEngineCallsForVariableAccess() throws Exception {
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final String content = "<html><head><title>foo</title><script>\n" + "myDate = 'foo';\n" + "function doUnqualifiedVariableAccess() {\n" + " alert('unqualified: ' + myDate);\n" + "}\n" + "function doQualifiedVariableAccess() {\n" + " alert('qualified: ' + window.myDate);\n" + "}\n" + "</script></head><body>\n" + "<p>hello world</p>\n" + "<a id='unqualified' onclick='doUnqualifiedVariableAccess();'>unqualified</a>\n" + "<a id='qualified' onclick='doQualifiedVariableAccess();'>qualified</a>\n" + "</body></html>";
webConnection.setDefaultResponse(content);
client.setWebConnection(webConnection);
final CountingJavaScriptEngine countingJavaScriptEngine = new CountingJavaScriptEngine(client);
client.setJavaScriptEngine(countingJavaScriptEngine);
final HtmlPage page = client.getPage(URL_FIRST);
assertEquals(1, countingJavaScriptEngine.getExecutionCount());
assertEquals(0, countingJavaScriptEngine.getCallCount());
page.getHtmlElementById("unqualified").click();
assertEquals(1, countingJavaScriptEngine.getExecutionCount());
assertEquals(1, countingJavaScriptEngine.getCallCount());
page.getHtmlElementById("qualified").click();
assertEquals(1, countingJavaScriptEngine.getExecutionCount());
assertEquals(2, countingJavaScriptEngine.getCallCount());
final String[] expectedAlerts = { "unqualified: foo", "qualified: foo" };
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class JavaScriptEngineTest method scopeInInactivePage.
/**
* If a reference has been hold on a page and the page is not
* anymore the one contained in "its" window, JavaScript should not be executed.
* (see {@link com.gargoylesoftware.htmlunit.javascript.host.WindowConcurrencyTest#cleanSetTimeout}).
* @throws Exception if the test fails
*/
@Test
public void scopeInInactivePage() throws Exception {
final String firstContent = "<html><head>\n" + "<script>\n" + "var foo = 'foo';\n" + "</script>\n" + "</head>\n" + "<body>\n" + " <a href='page2.html'>to page 2</a>\n" + " <div id='testdiv' onclick='alert(foo)'>foo</div>\n" + "</body>\n" + "</html>";
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse("<html></html>");
webConnection.setResponse(URL_FIRST, firstContent);
client.setWebConnection(webConnection);
final String[] expectedAlerts = { "foo" };
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = client.getPage(URL_FIRST);
final HtmlElement div = page.getHtmlElementById("testdiv");
div.click();
assertEquals(expectedAlerts, collectedAlerts);
collectedAlerts.clear();
page.getAnchors().get(0).click();
// ignore response, and click in the page again that is not "active" anymore
div.click();
assertEquals(Collections.emptyList(), collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class JavaScriptEngineTest method externalScriptWithNewLineBeforeClosingScriptTag.
/**
* Regression test for https://sourceforge.net/tracker/?func=detail&atid=448266&aid=1552746&group_id=47038.
* @throws Exception if the test fails
*/
@Test
public void externalScriptWithNewLineBeforeClosingScriptTag() throws Exception {
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String htmlContent = "<html><head><title>foo</title>\n" + "</head><body>\n" + // \n between opening and closing tag is important
"<script src='test.js'>\n</script>\n" + "</body></html>";
final String jsContent = "function externalMethod() {\n" + " alert('Got to external method');\n" + "}\n" + "externalMethod();\n";
webConnection.setResponse(URL_FIRST, htmlContent);
webConnection.setDefaultResponse(jsContent, 200, "OK", "text/javascript");
client.setWebConnection(webConnection);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
client.getPage(URL_FIRST);
assertEquals(new String[] { "Got to external method" }, collectedAlerts);
}
Aggregations