Java Code Examples for com.gargoylesoftware.htmlunit.WebClient#setAlertHandler()

The following examples show how to use com.gargoylesoftware.htmlunit.WebClient#setAlertHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WindowTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void prompt_noPromptHandler() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    final List<String> collectedPrompts = new ArrayList<>();

    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final String firstContent
        = "<html><head><title>First</title><script>function doTest() {alert(prompt('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(Collections.EMPTY_LIST, collectedPrompts);
    assertEquals(new String[] {"null"}, collectedAlerts);
}
 
Example 2
Source File: HtmlAnchor2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void clickShiftJavascript() throws Exception {
    final String first
        = "<html><head><title>First</title></head><body>\n"
        + "  <a href='javascript: window.location=\"" + URL_SECOND + "\"' id='a2'>link to foo2</a>\n"
        + "</body></html>";
    final String second
        = "<html><head><title>Second</title></head><body></body></html>";

    final WebClient client = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, first);
    webConnection.setResponse(URL_SECOND, second);
    client.setWebConnection(webConnection);

    assertEquals(1, getWebClient().getTopLevelWindows().size());
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");

    final HtmlPage secondPage = anchor.click(true, false, false);
    assertEquals(2, getWebClient().getTopLevelWindows().size());
    assertEquals("Second", secondPage.getTitleText());
}
 
Example 3
Source File: Event3Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void testEventBubblingReturns(final String onclick1,
    final String onclick2, final String onclick3, final boolean changesPage) throws Exception {

    final String html1
        = "<html><head><title>First</title></head><body>\n"
        + "<div onclick='alert(\"d\"); " + onclick1 + "'>\n"
        + "<span onclick='alert(\"s\"); " + onclick2 + "'>\n"
        + "<a href='" + URL_SECOND + "' id='a' onclick='alert(\"a\"); " + onclick3 + "'>go</a>\n"
        + "</span>\n"
        + "</div>\n"
        + "</body></html>";

    final String html2 = "<html><head><title>Second</title></head><body></body></html>";

    final WebClient client = getWebClientWithMockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setResponse(URL_FIRST, html1);
    webConnection.setResponse(URL_SECOND, html2);

    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a");

    final HtmlPage secondPage = anchor.click();
    assertEquals(new String[] {"a", "s", "d"}, collectedAlerts);

    if (changesPage) {
        assertNotSame(page, secondPage);
    }
    else {
        assertSame(page, secondPage);
    }
}
 
Example 4
Source File: LocationTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void testGetVariousAttributes(final String url, final String[] expectedAlerts) throws Exception {
    final WebClient client = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();

    final String content
        = "<html><head><title>First</title><script>\n"
        + "function doTest() {\n"
        + "  var location = document.location;\n"
        + "  alert(location.hash);\n"
        + "  alert(location.host);\n"
        + "  alert(location.hostname);\n"
        + "  alert(location.href);\n"
        + "  alert(location.pathname);\n"
        + "  alert(location.port);\n"
        + "  alert(location.protocol);\n"
        + "  alert(location.search);\n"
        + "}\n</script></head>\n"
        + "<body onload='doTest()'></body></html>";

    webConnection.setDefaultResponse(content);
    client.setWebConnection(webConnection);

    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    // Try page with only a server name
    client.getPage(url);
    assertEquals(expectedAlerts, collectedAlerts);
}
 
Example 5
Source File: LocationTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if an error occurs
 */
@Test
public void fileUrlFormat() throws Exception {
    final URL url = getClass().getResource("LocationTest_fileUrlFormat.html");
    assertNotNull(url);

    final WebClient client = getWebClient();
    final List<String> alerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(alerts));
    client.getPage(url);

    assertEquals(1, alerts.size());
    assertTrue(alerts.get(0).startsWith("file:///"));
}
 
Example 6
Source File: HTMLElement3Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"onblur text2", "onblur text1"})
public void onBlurOnWindowFocusChange() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();

    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final String firstHtml = "<html><head><title>First</title></head>\n"
        + "<body><form name='form1'>\n"
        + "<input id='text1' onblur='alert(\"onblur text1\")'>\n"
        + "<button type='button' id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</a>\n"
        + "</form></body></html>";
    webConnection.setResponse(URL_FIRST, firstHtml);

    final String secondHtml = "<html><head><title>Second</title></head>\n"
        + "<body onLoad='doTest()'>\n"
        + "<input id='text2' onblur='alert(\"onblur text2\")'>\n"
        + "<script>\n"
        + "  function doTest() {\n"
        + "    opener.document.getElementById('text1').focus();\n"
        + "    document.getElementById('text2').focus();\n"
        + "  }\n"
        + "</script></body></html>";

    webConnection.setResponse(URL_SECOND, secondHtml);
    webClient.setWebConnection(webConnection);

    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());

    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    final HtmlPage secondPage = buttonA.click();
    assertEquals("Second", secondPage.getTitleText());
    webClient.setCurrentWindow(firstPage.getEnclosingWindow());
    webClient.setCurrentWindow(secondPage.getEnclosingWindow());
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
 
Example 7
Source File: HtmlArea2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void click_onclickReturnsTrue() throws Exception {
    final WebClient client = createWebClient("alert('foo');return true;");
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlArea area = page.getHtmlElementById("second");

    final HtmlPage thirdPage = area.click();
    assertEquals(new String[] {"foo"}, collectedAlerts);
    assertEquals("second", thirdPage.getTitleText());
}
 
Example 8
Source File: HtmlFormTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Simulate a bug report where an anchor contained JavaScript that caused a form submit.
 * According to the bug report, the form would be submitted even though the onsubmit
 * handler would return false. This wasn't reproducible but I added a test for it anyway.</p>
 *
 * <p>UPDATE: If the form submit is triggered by JavaScript then the onsubmit handler is not
 * supposed to be called so it doesn't matter what value it returns.</p>
 * @throws Exception if the test fails
 */
@Test
public void submit_AnchorCausesSubmit_onSubmitHandler_returnFalse() throws Exception {
    final String firstHtml
        = "<html><head><title>First</title></head>\n"
        + "<script>function doalert(message){alert(message);}</script>\n"
        + "<body><form name='form1' method='get' action='" + URL_SECOND + "' "
        + "onSubmit='doalert(\"clicked\");return false;'>\n"
        + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
        + "<a id='link1' href='javascript:document.form1.submit()'>Click me</a>\n"
        + "</body></html>";
    final String secondHtml = "<html><head><title>Second</title></head><body></body></html>";

    final WebClient client = getWebClientWithMockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setResponse(URL_FIRST, firstHtml);
    webConnection.setDefaultResponse(secondHtml);

    final HtmlPage firstPage = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = firstPage.getHtmlElementById("link1");

    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    final HtmlPage secondPage = anchor.click();
    assertEquals("Second", secondPage.getTitleText());

    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
 
Example 9
Source File: FalsifyingWebConnectionTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void blockSomeRequests() throws Exception {
    final WebClient webClient = getWebClient();

    final String html = "<html><head>\n"
        + "<script src='http://www.google-analytics.com/ga.js'></script>\n"
        + "<script src='myJs.js'></script>\n"
        + "</head><body>\n"
        + "hello world!"
        + "<body></html>";

    final MockWebConnection mockConnection = new MockWebConnection();
    mockConnection.setResponse(URL_FIRST, html);
    mockConnection.setResponse(new URL(URL_FIRST, "myJs.js"), "alert('hello');");
    webClient.setWebConnection(mockConnection);

    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    // create a WebConnection that filters google-analytics scripts
    // c'tor configures connection on the web client
    try (FalsifyingWebConnection connection = new FalsifyingWebConnection(webClient) {
        @Override
        public WebResponse getResponse(final WebRequest request) throws IOException {
            if ("www.google-analytics.com".equals(request.getUrl().getHost())) {
                return createWebResponse(request, "", MimeType.APPLICATION_JAVASCRIPT); // -> empty script
            }
            return super.getResponse(request);
        }
    }) {

        webClient.getPage(URL_FIRST);

        assertEquals(2, mockConnection.getRequestCount());
        final String[] expectedAlerts = {"hello"};
        assertEquals(expectedAlerts, collectedAlerts);
    }
}
 
Example 10
Source File: HtmlAnchor2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void clickCtrl() throws Exception {
    final String first
        = "<html><head><title>First</title></head><body>\n"
        + "  <a href='" + URL_SECOND + "' id='a2'>link to foo2</a>\n"
        + "</body></html>";
    final String second
        = "<html><head><title>Second</title></head><body></body></html>";

    final WebClient client = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, first);
    webConnection.setResponse(URL_SECOND, second);
    client.setWebConnection(webConnection);

    assertEquals(1, getWebClient().getTopLevelWindows().size());
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");

    final HtmlPage secondPage = anchor.click(false, true, false);
    assertEquals(2, getWebClient().getTopLevelWindows().size());
    assertEquals("First", secondPage.getTitleText());
}
 
Example 11
Source File: HtmlAnchor2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void clickShiftCtrlJavascript() throws Exception {
    final String first
        = "<html><head><title>First</title></head><body>\n"
        + "  <a href='javascript: window.location=\"" + URL_SECOND + "\"' id='a2'>link to foo2</a>\n"
        + "</body></html>";
    final String second
        = "<html><head><title>Second</title></head><body></body></html>";

    final WebClient client = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, first);
    webConnection.setResponse(URL_SECOND, second);
    client.setWebConnection(webConnection);

    assertEquals(1, getWebClient().getTopLevelWindows().size());
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");

    final HtmlPage secondPage = anchor.click(true, true, false);
    assertEquals(2, getWebClient().getTopLevelWindows().size());
    assertEquals("First", secondPage.getTitleText());
}
 
Example 12
Source File: HtmlAnchor2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void click_onClickHandler() throws Exception {
    final String firstContent
        = "<html><head><title>First</title></head><body>\n"
        + "<a href='http://www.foo1.com' id='a1'>link to foo1</a>\n"
        + "<a href='" + URL_SECOND + "' id='a2' "
        + "onClick='alert(\"clicked\")'>link to foo2</a>\n"
        + "<a href='http://www.foo3.com' id='a3'>link to foo3</a>\n"
        + "</body></html>";
    final String secondContent
        = "<html><head><title>Second</title></head><body></body></html>";

    final WebClient client = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    client.setWebConnection(webConnection);

    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");

    assertEquals(Collections.EMPTY_LIST, collectedAlerts);

    final HtmlPage secondPage = anchor.click();

    assertEquals(new String[] {"clicked"}, collectedAlerts);
    assertEquals("Second", secondPage.getTitleText());
}
 
Example 13
Source File: HtmlFormTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void submit_javascriptActionMixedCase() throws Exception {
    final String firstHtml
        = "<html><head><title>First</title></head><body>\n"
        + "<form method='get' action='jaVAscrIpt:alert(\"clicked\")'>\n"
        + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
        + "</body></html>";
    final String secondHtml = "<html><head><title>Second</title></head><body></body></html>";

    final WebClient client = getWebClientWithMockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setResponse(URL_FIRST, firstHtml);
    webConnection.setResponse(URL_SECOND, secondHtml);

    final HtmlPage firstPage = client.getPage(URL_FIRST);
    final HtmlSubmitInput button = firstPage.getHtmlElementById("button");

    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    final HtmlPage secondPage = button.click();
    assertEquals(firstPage.getTitleText(), secondPage.getTitleText());

    assertEquals(new String[] {"clicked"}, collectedAlerts);
}
 
Example 14
Source File: HtmlFormTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void submit_onSubmitHandler() throws Exception {
    final String firstHtml
        = "<html><head><title>First</title></head><body>\n"
        + "<form method='get' action='" + URL_SECOND + "' onSubmit='alert(\"clicked\")'>\n"
        + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
        + "</body></html>";
    final String secondHtml = "<html><head><title>Second</title></head><body></body></html>";

    final WebClient client = getWebClientWithMockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setResponse(URL_FIRST, firstHtml);
    webConnection.setDefaultResponse(secondHtml);

    final HtmlPage firstPage = client.getPage(URL_FIRST);
    final HtmlSubmitInput button = firstPage.getHtmlElementById("button");

    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    final HtmlPage secondPage = button.click();
    assertEquals("Second", secondPage.getTitleText());

    assertEquals(new String[] {"clicked"}, collectedAlerts);
}
 
Example 15
Source File: HtmlAnchor2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void click_javascriptUrl_javascriptDisabled() throws Exception {
    final String htmlContent
        = "<html><head><title>foo</title></head><body>\n"
        + "<a href='http://www.foo1.com' id='a1'>link to foo1</a>\n"
        + "<a href='javascript:alert(\"clicked\")' id='a2'>link to foo2</a>\n"
        + "<a href='http://www.foo3.com' id='a3'>link to foo3</a>\n"
        + "</body></html>";
    final WebClient client = getWebClient();
    client.getOptions().setJavaScriptEnabled(false);

    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setDefaultResponse(htmlContent);
    client.setWebConnection(webConnection);

    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");

    assertEquals(Collections.EMPTY_LIST, collectedAlerts);

    final HtmlPage secondPage = anchor.click();

    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    assertSame(page, secondPage);
}
 
Example 16
Source File: HtmlAnchor2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void click_onClickHandler_javascriptDisabled() throws Exception {
    final String htmlContent
        = "<html><head><title>foo</title></head><body>\n"
        + "<a href='http://www.foo1.com' id='a1'>link to foo1</a>\n"
        + "<a href='http://www.foo2.com' id='a2' "
        + "onClick='alert(\"clicked\")'>link to foo2</a>\n"
        + "<a href='http://www.foo3.com' id='a3'>link to foo3</a>\n"
        + "</body></html>";
    final WebClient client = getWebClient();
    client.getOptions().setJavaScriptEnabled(false);

    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setDefaultResponse(htmlContent);
    client.setWebConnection(webConnection);

    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");

    assertEquals(Collections.EMPTY_LIST, collectedAlerts);

    final HtmlPage secondPage = anchor.click();

    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    final List<?> expectedParameters = Collections.EMPTY_LIST;

    assertEquals("url", "http://www.foo2.com/", secondPage.getUrl());
    assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
    assertEquals("parameters", expectedParameters, webConnection.getLastParameters());
}
 
Example 17
Source File: WindowTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void parentAndTop() throws Exception {
    final String firstContent
        = "<html><head><title>First</title></head><body>\n"
        + "  <iframe name='left' src='" + URL_SECOND + "'></iframe>\n"
        + "</body></html>";
    final String secondContent
        = "<html><head><title>Second</title></head><body>\n"
        + "  <iframe name='innermost' src='" + URL_THIRD + "'></iframe>\n"
        + "</body></html>";
    final String thirdContent
        = "<html><head><title>Third</title><script>\n"
        + "function doAlert() {\n"
        + "  alert(parent != this);\n"
        + "  alert(top != this);\n"
        + "  alert(parent != top);\n"
        + "  alert(parent.parent == top);\n"
        + "  alert(parent.frames[0] == this);\n"
        + "  alert(top.frames[0] == parent);\n"
        + "}\n"
        + "</script></head>\n"
        + "<body><a id='clickme' onClick='doAlert()'>foo</a></body></html>";

    final WebClient webClient = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    webConnection.setResponse(URL_THIRD, thirdContent);

    webClient.setWebConnection(webConnection);

    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());

    final WebWindow innermostWebWindow = webClient.getWebWindowByName("innermost");
    final HtmlPage innermostPage = (HtmlPage) innermostWebWindow.getEnclosedPage();
    innermostPage.getHtmlElementById("clickme").click();

    assertNotSame(innermostWebWindow.getParentWindow(), innermostWebWindow);
    assertNotSame(innermostWebWindow.getTopWindow(), innermostWebWindow);
    assertNotSame(innermostWebWindow.getParentWindow(), innermostWebWindow.getTopWindow());
    assertSame(innermostWebWindow.getParentWindow().getParentWindow(), innermostWebWindow.getTopWindow());

    assertEquals(new String[] {"true", "true", "true", "true", "true", "true"}, collectedAlerts);
}
 
Example 18
Source File: HtmlPage2Test.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Differs from {@link #loadExternalJavaScript()} by the absolute reference of the javascript source.
 * @throws Exception if the test fails
 */
@Test
@Alerts("25")
public void loadExternalJavaScript_absolute() throws Exception {
    final String html =
        "<html><head>\n"
        + "<script>\n"
        + "function makeIframe() {\n"
        + "  var iframesrc = '<html><head>';\n"
        + "  iframesrc += '<script src=\"" + URL_SECOND + "\"></' + 'script>';\n"
        + "  iframesrc += '<script>';\n"
        + "  iframesrc += 'function doSquared() {';\n"
        + "  iframesrc += '    try {';\n"
        + "  iframesrc += '      var y = squared(5);';\n"
        + "  iframesrc += '      alert(y);';\n"
        + "  iframesrc += '    } catch (e) {';\n"
        + "  iframesrc += '      alert(\"error\");';\n"
        + "  iframesrc += '    }';\n"
        + "  iframesrc += '}';\n"
        + "  iframesrc += '</' + 'script>';\n"
        + "  iframesrc += '</head>';\n"
        + "  iframesrc += '<body onLoad=\"doSquared()\" >';\n"
        + "  iframesrc += '</body>';\n"
        + "  iframesrc += '</html>';\n"
        + "  var iframe = document.createElement('IFRAME');\n"
        + "  iframe.id = 'iMessage';\n"
        + "  iframe.name = 'iMessage';\n"
        + "  iframe.src = \"javascript:'\" + iframesrc + \"'\";\n"
        + "  document.body.appendChild(iframe);\n"
        + "}\n"
        + "</script></head>\n"
        + "<body onload='makeIframe()'>\n"
        + "</body></html>";

    final String js = "function squared(n) {return n * n}";

    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();

    webConnection.setResponse(URL_FIRST, html);
    webConnection.setResponse(URL_SECOND, js);
    webClient.setWebConnection(webConnection);

    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    webClient.getPage(URL_FIRST);
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
 
Example 19
Source File: XMLHttpRequest3Test.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Tests asynchronous use of XMLHttpRequest, where the XHR request fails due to IOException (Connection refused).
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = {"0", "1", "2", "4", MSG_NO_CONTENT, MSG_PROCESSING_ERROR},
        IE = {"0", "1", "1", "2", "4", MSG_NO_CONTENT, MSG_PROCESSING_ERROR})
public void asyncUseWithNetworkConnectionFailure() throws Exception {
    final String html =
          "<html>\n"
        + "<head>\n"
        + "<title>XMLHttpRequest Test</title>\n"
        + "<script>\n"
        + "var request;\n"
        + "function testAsync() {\n"
        + "  request = new XMLHttpRequest();\n"
        + "  request.onreadystatechange = onReadyStateChange;\n"
        + "  request.onerror = onError;\n"
        + "  alert(request.readyState);\n"
        + "  request.open('GET', '" + URL_SECOND + "', true);\n"
        + "  request.send('');\n"
        + "}\n"
        + "function onError() {\n"
        + "  alert('" + MSG_PROCESSING_ERROR + "');\n"
        + "}\n"
        + "function onReadyStateChange() {\n"
        + "  alert(request.readyState);\n"
        + "  if (request.readyState == 4) {\n"
        + "    if (request.responseText.length == 0)\n"
        + "      alert('" + MSG_NO_CONTENT + "');\n"
        + "    else\n"
        + "      throw 'Unexpected content, should be zero length but is: \"' + request.responseText + '\"';\n"
        + "  }\n"
        + "}\n"
        + "</script>\n"
        + "</head>\n"
        + "<body onload='testAsync()'>\n"
        + "</body>\n"
        + "</html>";

    final WebClient client = getWebClient();
    final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection conn = new DisconnectedMockWebConnection();
    conn.setResponse(URL_FIRST, html);
    client.setWebConnection(conn);
    client.getPage(URL_FIRST);

    assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(1000));
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
 
Example 20
Source File: HtmlPage2Test.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("25")
public void loadExternalJavaScript() throws Exception {
    final String html =
        "<html><head>\n"
        + "<script>\n"
        + "function makeIframe() {\n"
        + "  var iframesrc = '<html><head>';\n"
        + "  iframesrc += '<script src=\"" + "js.js" + "\"></' + 'script>';\n"
        + "  iframesrc += '<script>';\n"
        + "  iframesrc += 'function doSquared() {';\n"
        + "  iframesrc += '    try {';\n"
        + "  iframesrc += '      var y = squared(5);';\n"
        + "  iframesrc += '      alert(y);';\n"
        + "  iframesrc += '    } catch (e) {';\n"
        + "  iframesrc += '      alert(\"error\");';\n"
        + "  iframesrc += '    }';\n"
        + "  iframesrc += '}';\n"
        + "  iframesrc += '</' + 'script>';\n"
        + "  iframesrc += '</head>';\n"
        + "  iframesrc += '<body onLoad=\"doSquared()\" >';\n"
        + "  iframesrc += '</body>';\n"
        + "  iframesrc += '</html>';\n"
        + "  var iframe = document.createElement('IFRAME');\n"
        + "  iframe.id = 'iMessage';\n"
        + "  iframe.name = 'iMessage';\n"
        + "  iframe.src = \"javascript:'\" + iframesrc + \"'\";\n"
        + "  document.body.appendChild(iframe);\n"
        + "}\n"
        + "</script></head>\n"
        + "<body onload='makeIframe()'>\n"
        + "</body></html>";

    final String js = "function squared(n) {return n * n}";

    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();

    webConnection.setResponse(URL_FIRST, html);
    webConnection.setResponse(new URL(URL_FIRST, "js.js"), js);
    webClient.setWebConnection(webConnection);

    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    webClient.getPage(URL_FIRST);
    assertEquals(getExpectedAlerts(), collectedAlerts);
}