use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowConcurrencyTest method setTimeoutOnFrameWindow.
/**
* Our Window proxy caused troubles.
* @throws Exception if the test fails
*/
@Test
public void setTimeoutOnFrameWindow() throws Exception {
final String html = "<html><head><title>foo</title><script>\n" + " function test() {\n" + " frames[0].setTimeout(f, 0);\n" + " }\n" + " function f() {\n" + " alert('in f');\n" + " }\n" + "</script></head><body onload='test()'>\n" + "<iframe src='about:blank'></iframe>\n" + "</body></html>";
final List<String> collectedAlerts = new ArrayList<>();
client_.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection conn = new MockWebConnection();
conn.setDefaultResponse(html);
client_.setWebConnection(conn);
client_.getPage(URL_FIRST);
assertEquals(0, client_.waitForBackgroundJavaScriptStartingBefore(1000));
final String[] expectedAlerts = { "in f" };
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method showModalDialog.
/**
* Basic test for the <tt>showModalDialog</tt> method. See bug #703.
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = { "undefined", "Jane", "Smith", "sdg", "finished" }, CHROME = "not available", EDGE = "not available", FF = "not available", FF_ESR = "not available")
public void showModalDialog() throws Exception {
final String html1 = "<html><head><script>\n" + " function test() {\n" + " if (!window.showModalDialog) {alert('not available'); return; }\n" + " alert(window.returnValue);\n" + " var o = new Object();\n" + " o.firstName = 'Jane';\n" + " o.lastName = 'Smith';\n" + " var ret = showModalDialog('myDialog.html', o, 'dialogHeight:300px; dialogLeft:200px;');\n" + " alert(ret);\n" + " alert('finished');\n" + " }\n" + "</script></head><body>\n" + " <button onclick='test()' id='b'>Test</button>\n" + "</body></html>";
final String html2 = "<html><head><script>\n" + " var o = window.dialogArguments;\n" + " alert(o.firstName);\n" + " alert(o.lastName);\n" + " window.returnValue = 'sdg';\n" + "</script></head>\n" + "<body>foo</body></html>";
final WebClient client = getWebClient();
final List<String> actual = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(actual));
final MockWebConnection conn = new MockWebConnection();
conn.setResponse(URL_FIRST, html1);
conn.setResponse(new URL(URL_FIRST, "myDialog.html"), html2);
client.setWebConnection(conn);
final HtmlPage page = client.getPage(URL_FIRST);
final HtmlElement button = page.getHtmlElementById("b");
final HtmlPage dialogPage = button.click();
if (getExpectedAlerts().length > 1) {
final DialogWindow dialog = (DialogWindow) dialogPage.getEnclosingWindow();
dialog.close();
}
assertEquals(getExpectedAlerts(), actual);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method status.
/**
* @throws Exception if the test fails
*/
@Test
public void status() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String firstContent = "<html><head><title>First</title><script>\n" + "function doTest() {\n" + " alert(window.status);\n" + " window.status = 'newStatus';\n" + " alert(window.status);\n" + "}\n" + "</script></head><body onload='doTest()'>\n" + "</body></html>";
final URL url = URL_FIRST;
webConnection.setResponse(url, firstContent);
webClient.setWebConnection(webConnection);
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final List<String> collectedStatus = new ArrayList<>();
webClient.setStatusHandler(new StatusHandler() {
@Override
public void statusMessageChanged(final Page page, final String message) {
collectedStatus.add(message);
}
});
final HtmlPage firstPage = webClient.getPage(URL_FIRST);
assertEquals("First", firstPage.getTitleText());
final String[] expectedAlerts = { "", "newStatus" };
assertEquals("alerts", expectedAlerts, collectedAlerts);
final String[] expectedStatus = { "newStatus" };
assertEquals("status", expectedStatus, collectedStatus);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method confirm.
/**
* @throws Exception if the test fails
*/
@Test
public void confirm() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
final List<String> collectedConfirms = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
webClient.setConfirmHandler(new ConfirmHandler() {
@Override
public boolean handleConfirm(final Page page, final String message) {
collectedConfirms.add(message);
return true;
}
});
final String firstContent = "<html><head><title>First</title><script>function doTest() {alert(confirm('foo'))}</script>\n" + "</head><body onload='doTest()'></body></html>";
webConnection.setResponse(URL_FIRST, firstContent);
webClient.setWebConnection(webConnection);
final HtmlPage firstPage = webClient.getPage(URL_FIRST);
assertEquals("First", firstPage.getTitleText());
assertEquals(new String[] { "foo" }, collectedConfirms);
assertEquals(new String[] { "true" }, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method prompt.
/**
* @throws Exception if the test fails
*/
@Test
public void prompt() throws Exception {
try (WebClient webClient = getWebClient()) {
try (MockWebConnection webConnection = new MockWebConnection()) {
final List<String> collectedAlerts = new ArrayList<>();
final List<String> collectedPrompts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
webClient.setPromptHandler((page, message, defaultValue) -> {
collectedPrompts.add(message);
return "Flintstone";
});
final String html = "<html><head><title>First</title>\n" + "<script>function doTest() {alert(prompt('foo'))}</script>\n" + "</head><body onload='doTest()'></body></html>";
webConnection.setResponse(URL_FIRST, html);
webClient.setWebConnection(webConnection);
final HtmlPage firstPage = webClient.getPage(URL_FIRST);
assertEquals("First", firstPage.getTitleText());
assertEquals(new String[] { "foo" }, collectedPrompts);
assertEquals(new String[] { "Flintstone" }, collectedAlerts);
}
}
}
Aggregations