use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class JavaScriptEngineTest method scriptErrorIsolated.
/**
* Check that wrong JavaScript just causes its context to fail but not the whole page.
* @throws Exception if something goes wrong
*/
@Test
public void scriptErrorIsolated() throws Exception {
final String content = "<html>\n" + "<head>\n" + "<script>alert(1);</script>\n" + "<script>alert(2</script>\n" + "<script>alert(3);</script>\n" + "</head>\n" + "<body onload='alert(4);notExisting()'>\n" + "<button onclick='alert(5)'>click me</button>\n" + "</body>\n" + "</html>";
final String[] expectedAlerts = { "1", "3", "4" };
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(content);
client.setWebConnection(webConnection);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
// first test with script exceptions thrown
try {
client.getPage(URL_FIRST);
fail("Should have thrown a script error");
} catch (final Exception e) {
// nothing
}
assertEquals(new String[] { "1" }, collectedAlerts);
collectedAlerts.clear();
// and with script exception not thrown
client.getOptions().setThrowExceptionOnScriptError(false);
client.getPage(URL_FIRST);
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class JavaScriptEngineTest method compiledScriptCached.
/**
* Test that compiled script are cached.
* @throws Exception if the test fails
*/
@Test
public void compiledScriptCached() throws Exception {
final String content1 = "<html><head><title>foo</title>\n" + "<script src='script.js'></script>\n" + "</head><body>\n" + "<a href='page2.html'>to page 2</a>\n" + "</body></html>";
final String content2 = "<html><head><title>page 2</title>\n" + "<script src='script.js'></script>\n" + "</head><body>\n" + "</body></html>";
final String script = "alert(document.title)";
final WebClient client = getWebClient();
final MockWebConnection connection = new MockWebConnection();
client.setWebConnection(connection);
connection.setResponse(URL_FIRST, content1);
connection.setResponse(new URL(URL_FIRST, "page2.html"), content2);
final List<NameValuePair> headersAllowingCache = new ArrayList<>();
headersAllowingCache.add(new NameValuePair("Last-Modified", "Sun, 15 Jul 2007 20:46:27 GMT"));
connection.setResponse(new URL(URL_FIRST, "script.js"), script, 200, "ok", "text/javascript", headersAllowingCache);
final CountingJavaScriptEngine countingJavaScriptEngine = new CountingJavaScriptEngine(client);
client.setJavaScriptEngine(countingJavaScriptEngine);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page1 = client.getPage(URL_FIRST);
assertEquals(new String[] { "foo" }, collectedAlerts);
assertEquals(1, countingJavaScriptEngine.getExecuteScriptCount());
assertEquals(1, countingJavaScriptEngine.getCompileCount());
collectedAlerts.clear();
page1.getAnchors().get(0).click();
assertEquals(new String[] { "page 2" }, collectedAlerts);
assertEquals(2, countingJavaScriptEngine.getExecuteScriptCount());
assertEquals(1, countingJavaScriptEngine.getCompileCount());
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class JavaScriptEngineTest method functionDefinedInExternalFile_CalledFromInlineScript.
/**
* @throws Exception if the test fails
*/
@Test
public void functionDefinedInExternalFile_CalledFromInlineScript() throws Exception {
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String htmlContent = "<html><head><title>foo</title><script src='./test.js'></script>\n" + "</head><body>\n" + " <script>externalMethod()</script>\n" + "</body></html>";
final String jsContent = "function externalMethod() {\n" + " alert('Got to external method');\n" + "} ";
webConnection.setResponse(new URL("http://first/index.html"), htmlContent);
webConnection.setResponse(new URL("http://first/test.js"), jsContent, "text/javascript");
client.setWebConnection(webConnection);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = client.getPage("http://first/index.html");
assertEquals("foo", page.getTitleText());
assertEquals(new String[] { "Got to external method" }, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class JavaScriptEngineTest method activeXObjectWithMap.
/**
* Test that Java objects placed in the active x map can be instantiated and used within
* JavaScript using the IE specific ActiveXObject constructor.
* @throws Exception if the test fails
*/
@Test
public void activeXObjectWithMap() throws Exception {
if (!getBrowserVersion().hasFeature(JS_XML_SUPPORT_VIA_ACTIVEXOBJECT)) {
return;
}
final Map<String, String> activexToJavaMapping = new HashMap<>();
activexToJavaMapping.put("MockActiveXObject", "com.gargoylesoftware.htmlunit.javascript.MockActiveXObject");
activexToJavaMapping.put("FakeObject", "com.gargoylesoftware.htmlunit.javascript.NoSuchObject");
activexToJavaMapping.put("BadObject", null);
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
client.setWebConnection(webConnection);
client.setActiveXObjectMap(activexToJavaMapping);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
webConnection.setDefaultResponse(getJavaScriptContent("new ActiveXObject('UnknownObject')"));
try {
client.getPage("http://www.yahoo.com");
fail("An exception should be thrown for non existent object in the map.");
} catch (final ScriptException e) {
// Success
}
webConnection.setDefaultResponse(getJavaScriptContent("new ActiveXObject('BadObject', 'server')"));
try {
client.getPage("http://www.yahoo.com");
fail("An exception should be thrown for an invalid object in the map.");
} catch (final ScriptException e) {
// Success
}
// Test for a non existent class in the map
webConnection.setDefaultResponse(getJavaScriptContent("new ActiveXObject('FakeObject')"));
try {
client.getPage("http://www.yahoo.com");
fail("An exception should be thrown for a non existent object in the map.");
} catch (final ScriptException e) {
// Success
}
assertEquals("should no alerts yet", Collections.EMPTY_LIST, collectedAlerts);
// Try a valid object in the map
webConnection.setDefaultResponse(getJavaScriptContent("var t = new ActiveXObject('MockActiveXObject'); alert(t.MESSAGE);\n"));
client.getPage("http://www.yahoo.com");
assertEquals("The active x object did not bind to the object.", new String[] { MockActiveXObject.MESSAGE }, collectedAlerts);
collectedAlerts.clear();
webConnection.setDefaultResponse(getJavaScriptContent("var t = new ActiveXObject('MockActiveXObject', 'server'); alert(t.GetMessage());\n"));
client.getPage("http://www.yahoo.com");
assertEquals("The active x object did not bind to the object.", new String[] { new MockActiveXObject().GetMessage() }, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class JavaScriptEngineTest method externalScriptWithApostrophesInComment.
/**
* An odd case, if two external scripts are referenced and the <script> element
* of the first contain a comment which contain an apostrophe, then the second script
* is ignored. Bug #632.
* @throws Exception if the test fails
*/
@Test
public void externalScriptWithApostrophesInComment() throws Exception {
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String htmlContent = "<html><head><title>foo</title>\n" + "<script src='/foo.js' id='script1'><!-- this shouldn't be a problem --></script>\n" + "<script src='/foo2.js' id='script2'><!-- this shouldn't be a problem --></script>\n" + "</head><body>\n" + "<p>hello world</p>\n" + "</body></html>";
webConnection.setResponse(URL_FIRST, htmlContent);
webConnection.setResponse(new URL(URL_FIRST, "foo.js"), "alert('got here');", "text/javascript");
webConnection.setResponse(new URL(URL_FIRST, "foo2.js"), "alert('got here 2');", "text/javascript");
client.setWebConnection(webConnection);
final String[] expectedAlerts = { "got here", "got here 2" };
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = client.getPage(URL_FIRST);
assertEquals(expectedAlerts, collectedAlerts);
assertNotNull(page.getHtmlElementById("script1"));
assertNotNull(page.getHtmlElementById("script2"));
}
Aggregations