use of com.gargoylesoftware.htmlunit.OnbeforeunloadHandler in project htmlunit by HtmlUnit.
the class HtmlPageTest method testOnbeforeunloadHandler.
/**
* @param browserVersion the browser version to use
* @param handlerOk whether <tt>OnbeforeunloadHandler.handleEvent</tt> will return {@code true} of {@code false}
* @param expectedPageTitle the expected title of the page after clicking
*/
private void testOnbeforeunloadHandler(final boolean handlerOk, final String expectedPageTitle) throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final List<String> collectedConfirms = new ArrayList<>();
webClient.setOnbeforeunloadHandler(new OnbeforeunloadHandler() {
@Override
public boolean handleEvent(final Page page, final String message) {
collectedConfirms.add(message);
return handlerOk;
}
});
final String expectedMessage = "Any string value here forces a dialog box to appear before closing the window.";
final String firstContent = "<html><head><title>first</title>\n" + "<script>\n" + " function closeIt(event) {\n" + " event.returnValue = '" + expectedMessage + "';\n" + " }\n" + "</script>\n" + "</head><body onbeforeunload='closeIt(event)'>\n" + " <a href='" + URL_SECOND + "'>Second page</a>\n" + "</body></html>";
final String secondContent = "<html><head><title>second</title>\n" + "</head><body>\n" + "</body></html>";
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponse(URL_SECOND, secondContent);
webClient.setWebConnection(webConnection);
final HtmlPage page = webClient.getPage(URL_FIRST);
final HtmlAnchor anchor = page.getAnchors().get(0);
final HtmlPage secondPage = anchor.click();
assertEquals(new String[] { expectedMessage }, collectedConfirms);
assertEquals(expectedPageTitle, secondPage.getTitleText());
}
use of com.gargoylesoftware.htmlunit.OnbeforeunloadHandler in project htmlunit by HtmlUnit.
the class WindowTest method onbeforeunload_calledBeforeDownload.
/**
* Download of next page is done first after onbeforeunload is done.
* @throws Exception if an error occurs
*/
@Test
@Alerts("x")
public void onbeforeunload_calledBeforeDownload() throws Exception {
final String html = "<html><body><script>\n" + " window.onbeforeunload = function() { alert('x'); return 'hello'; };\n" + " window.location = 'foo.html';\n" + "</script></body></html>";
final WebClient webClient = getWebClientWithMockWebConnection();
getMockWebConnection().setDefaultResponse("");
final OnbeforeunloadHandler handler = new OnbeforeunloadHandler() {
@Override
public boolean handleEvent(final Page page, final String returnValue) {
final String[] expectedRequests = { "" };
assertEquals(expectedRequests, getMockWebConnection().getRequestedUrls(URL_FIRST));
return true;
}
};
webClient.setOnbeforeunloadHandler(handler);
loadPageWithAlerts(html);
final String[] expectedRequests = { "", "foo.html" };
assertEquals(expectedRequests, getMockWebConnection().getRequestedUrls(URL_FIRST));
}
Aggregations