Search in sources :

Example 6 with AttachmentHandler

use of com.gargoylesoftware.htmlunit.attachment.AttachmentHandler in project htmlunit by HtmlUnit.

the class HtmlAnchor2Test method click_unexpectedPageDownloadAttributeAttachmentHandler.

/**
 * @throws Exception if the test fails
 */
@Test
public void click_unexpectedPageDownloadAttributeAttachmentHandler() throws Exception {
    final String html = "<html><head></head>\n" + "<body>\n" + "  <a href='" + URL_SECOND + "' id='link' download='test.json'>link</a>\n" + "</body>\n" + "</html>";
    getMockWebConnection().setResponse(URL_SECOND, "{name: \"Test\"};", MimeType.APPLICATION_JSON);
    final LinkedList<Page> pages = new LinkedList<Page>();
    getWebClient().setAttachmentHandler(new AttachmentHandler() {

        @Override
        public void handleAttachment(final Page page) {
            pages.add(page);
        }
    });
    try {
        final HtmlPage page = loadPage(html);
        assertEquals(1, getWebClient().getWebWindows().size());
        page.getElementById("link").click();
        assertEquals(2, getWebClient().getWebWindows().size());
        final WebWindow newWindow = getWebClient().getWebWindows().get(getWebClient().getWebWindows().size() - 1);
        assertTrue(newWindow.getEnclosedPage() instanceof UnexpectedPage);
        assertEquals(1, pages.size());
        assertTrue(pages.get(0) instanceof UnexpectedPage);
    } finally {
        getWebClient().setAttachmentHandler(null);
    }
}
Also used : UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) AttachmentHandler(com.gargoylesoftware.htmlunit.attachment.AttachmentHandler) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) Page(com.gargoylesoftware.htmlunit.Page) LinkedList(java.util.LinkedList) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Aggregations

Page (com.gargoylesoftware.htmlunit.Page)6 UnexpectedPage (com.gargoylesoftware.htmlunit.UnexpectedPage)6 AttachmentHandler (com.gargoylesoftware.htmlunit.attachment.AttachmentHandler)6 LinkedList (java.util.LinkedList)6 Test (org.junit.Test)6 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)4 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)2