use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlPageTest method onunLoadHandler.
/**
* @throws Exception if the test fails
*/
@Test
public void onunLoadHandler() throws Exception {
final String htmlContent = "<html><head><title>foo</title>\n" + "</head><body onunload='alert(\"foo\");alert(\"bar\")'>\n" + "</body></html>";
final String[] expectedAlerts = { "foo", "bar" };
final List<String> collectedAlerts = new ArrayList<>();
final WebClient client = getWebClient();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection conn = new MockWebConnection();
conn.setResponse(URL_FIRST, htmlContent);
client.setWebConnection(conn);
client.getPage(URL_FIRST);
client.getPage(URL_FIRST);
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlObjectTest method classid.
/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = "undefined", IE = "false")
public void classid() throws Exception {
if (getBrowserVersion().isIE() && !ActiveXObjectTest.isJacobInstalled()) {
return;
}
final String html = "<html><head>\n" + // Window Media Player CLASSID
"<object id='wm' classid='clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6'></object>\n" + "<script>\n" + " function test() {\n" + " alert(document.all.wm.fullScreen);\n" + " }\n" + "</script>\n" + "</head><body onload='test()'>\n" + "</body></html>";
final WebClient client = getWebClient();
client.getOptions().setActiveXNative(true);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, html);
client.setWebConnection(webConnection);
client.getPage(URL_FIRST);
assertEquals(getExpectedAlerts(), collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlScriptTest method scriptCloneDoesNotReloadScript.
/**
* Verifies that cloned script nodes do not reload or re-execute their content (bug 1954869).
* @throws Exception if an error occurs
*/
@Test
@Alerts("loaded")
public void scriptCloneDoesNotReloadScript() throws Exception {
final String html = "<html><body><script src='" + URL_SECOND + "'></script></body></html>";
final String js = "alert('loaded')";
final WebClient client = getWebClient();
final MockWebConnection conn = new MockWebConnection();
conn.setResponse(URL_FIRST, html);
conn.setResponse(URL_SECOND, js, MimeType.APPLICATION_JAVASCRIPT);
client.setWebConnection(conn);
final List<String> actual = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(actual));
final HtmlPage page = client.getPage(URL_FIRST);
assertEquals(2, conn.getRequestCount());
page.cloneNode(true);
assertEquals(2, conn.getRequestCount());
assertEquals(getExpectedAlerts(), actual);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlScriptTest method whitespaceInSrc.
/**
* Verifies that we're lenient about whitespace before and after URLs in the "src" attribute.
* @throws Exception if an error occurs
*/
@Test
@Alerts("ok")
public void whitespaceInSrc() throws Exception {
final String html = "<html><head><script src=' " + URL_SECOND + " '></script></head><body>abc</body></html>";
final String js = "alert('ok')";
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, html);
webConnection.setResponse(URL_SECOND, js);
client.setWebConnection(webConnection);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
client.getPage(URL_FIRST);
assertEquals(getExpectedAlerts(), collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class GeolocationTest method getCurrentPosition.
private void getCurrentPosition(final boolean geolocationEnabled) throws Exception {
final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
servlets.put("/test", GetCurrentPositionTestServlet.class);
servlets.put("/browserLocation", BrowserLocationServlet.class);
startWebServer("./", new String[0], servlets);
Geolocation.setProviderUrl(URL_FIRST + "browserLocation");
final WebClient client = getWebClient();
if (geolocationEnabled) {
client.getOptions().setGeolocationEnabled(true);
}
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
client.getPage(URL_FIRST + "test");
client.waitForBackgroundJavaScript(2000);
assertEquals(getExpectedAlerts(), collectedAlerts);
}
Aggregations