Search in sources :

Example 11 with UnexpectedPage

use of com.gargoylesoftware.htmlunit.UnexpectedPage 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

UnexpectedPage (com.gargoylesoftware.htmlunit.UnexpectedPage)11 Test (org.junit.Test)10 Page (com.gargoylesoftware.htmlunit.Page)9 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)4 AttachmentHandler (com.gargoylesoftware.htmlunit.attachment.AttachmentHandler)4 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)4 BatchFile (hudson.tasks.BatchFile)4 File (java.io.File)4 LinkedList (java.util.LinkedList)4 ZipFile (java.util.zip.ZipFile)4 VirtualFile (jenkins.util.VirtualFile)4 Issue (org.jvnet.hudson.test.Issue)4 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)3 ExtensionList (hudson.ExtensionList)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)3 InputStream (java.io.InputStream)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)1