Search in sources :

Example 1 with OnbeforeunloadHandler

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());
}
Also used : OnbeforeunloadHandler(com.gargoylesoftware.htmlunit.OnbeforeunloadHandler) ArrayList(java.util.ArrayList) Page(com.gargoylesoftware.htmlunit.Page) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 2 with OnbeforeunloadHandler

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));
}
Also used : OnbeforeunloadHandler(com.gargoylesoftware.htmlunit.OnbeforeunloadHandler) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Aggregations

OnbeforeunloadHandler (com.gargoylesoftware.htmlunit.OnbeforeunloadHandler)2 Page (com.gargoylesoftware.htmlunit.Page)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1 Alerts (com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1