Search in sources :

Example 81 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class Selection2Test method test.

private void test(final String action, final String x, final String alert) throws Exception {
    final String html = "<html>\n" + "<body onload='test()'>\n" + "  <span id='s1'>abc</span><span id='s2'>xyz</span><span id='s3'>foo</span>\n" + "  <input type='button' id='b' onclick=\"" + action + ";test();\" value='click'></input>\n" + "<script>\n" + "  var selection = document.selection; // IE\n" + "  if(!selection) selection = window.getSelection(); // FF\n" + "  var s1 = document.getElementById('s1');\n" + "  var s2 = document.getElementById('s2');\n" + "  function test() {\n" + "    try {\n" + "      var x = " + x + ";\n" + "      alert(" + alert + ");\n" + "    } catch (e) {\n" + "      alert('unsupported action');\n" + "    }\n" + "  }\n" + "</script>\n" + "</body></html>";
    final WebClient client = getWebClient();
    final MockWebConnection conn = new MockWebConnection();
    conn.setDefaultResponse(html);
    client.setWebConnection(conn);
    final List<String> actual = new ArrayList<>();
    final AlertHandler alertHandler = new CollectingAlertHandler(actual);
    client.setAlertHandler(alertHandler);
    final HtmlPage page = client.getPage(URL_FIRST);
    final DomNode s1Text = page.getHtmlElementById("s1").getFirstChild();
    final DomNode s2Text = page.getHtmlElementById("s2").getFirstChild();
    final HtmlInput input = page.getHtmlElementById("b");
    final org.w3c.dom.ranges.Range range = new SimpleRange();
    range.setStart(s1Text, 2);
    range.setEnd(s2Text, 1);
    page.setSelectionRange(range);
    input.click();
    assertEquals(getExpectedAlerts(), actual);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) AlertHandler(com.gargoylesoftware.htmlunit.AlertHandler) SimpleRange(com.gargoylesoftware.htmlunit.html.impl.SimpleRange)

Example 82 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class FalsifyingWebConnectionTest method blockSomeRequests.

/**
 * @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));
    // 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())) {
                // -> empty script
                return createWebResponse(request, "", MimeType.APPLICATION_JAVASCRIPT);
            }
            return super.getResponse(request);
        }
    }) {
        webClient.getPage(URL_FIRST);
        assertEquals(2, mockConnection.getRequestCount());
        final String[] expectedAlerts = { "hello" };
        assertEquals(expectedAlerts, collectedAlerts);
    }
}
Also used : WebRequest(com.gargoylesoftware.htmlunit.WebRequest) ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) Test(org.junit.Test)

Example 83 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class FalsifyingWebConnectionTest method simulateHttpError.

/**
 * @throws Exception if the test fails
 */
@Test
public void simulateHttpError() throws Exception {
    final WebClient webClient = getWebClient();
    final String html = "<html><head>\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));
    // first test this "site" when everything is ok
    webClient.getPage(URL_FIRST);
    final String[] expectedAlerts = { "hello" };
    assertEquals(expectedAlerts, collectedAlerts);
    // c'tor configures connection on the web client
    try (FalsifyingWebConnection connection = new FalsifyingWebConnection(webClient) {

        @Override
        public WebResponse getResponse(final WebRequest request) throws IOException {
            if (request.getUrl().getPath().endsWith(".js")) {
                return createWebResponse(request, "", MimeType.TEXT_HTML, 500, "Application Error");
            }
            return super.getResponse(request);
        }
    }) {
        try {
            webClient.getPage(URL_FIRST);
            fail("HTTP Exception expected!");
        } catch (final FailingHttpStatusCodeException e) {
        // that's fine
        }
    }
}
Also used : WebRequest(com.gargoylesoftware.htmlunit.WebRequest) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) Test(org.junit.Test)

Example 84 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HtmlFormTest method submit_javascriptActionLeadingWhitespace.

/**
 * @throws Exception if the test fails
 */
@Test
public void submit_javascriptActionLeadingWhitespace() 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);
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 85 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HtmlFormTest method submit_onSubmitHandler_preventDefaultOnly.

/**
 * Testcase for 3072010.
 *
 * @throws Exception if the test fails
 */
@Test
@Alerts("clicked")
public void submit_onSubmitHandler_preventDefaultOnly() throws Exception {
    final String firstHtml = "<html><body>\n" + "<form method='post' action='/foo' >\n" + "<input type='submit' id='button'/>\n" + "</form>\n" + "<script>\n" + "function foo(e) {\n" + "  alert('clicked');\n" + "  e.returnValue = false;\n" + "  if (e.preventDefault) {\n" + "    e.preventDefault();\n" + "  }\n" + "}\n" + "var oForm = document.forms[0];\n" + "oForm.addEventListener('submit', foo, false);\n" + "</script>\n" + "</body></html>";
    final WebClient client = getWebClientWithMockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    getMockWebConnection().setResponse(URL_FIRST, firstHtml);
    getMockWebConnection().setDefaultResponse("");
    final HtmlPage firstPage = client.getPage(URL_FIRST);
    final HtmlSubmitInput button = firstPage.getHtmlElementById("button");
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    final HtmlPage secondPage = button.click();
    assertSame(firstPage, secondPage);
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Aggregations

CollectingAlertHandler (com.gargoylesoftware.htmlunit.CollectingAlertHandler)96 WebClient (com.gargoylesoftware.htmlunit.WebClient)92 ArrayList (java.util.ArrayList)89 Test (org.junit.Test)87 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)82 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)35 URL (java.net.URL)22 Alerts (com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)20 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)7 HtmlAnchor (com.gargoylesoftware.htmlunit.html.HtmlAnchor)7 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)6 HashMap (java.util.HashMap)5 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)3 Servlet (javax.servlet.Servlet)3 HttpServlet (javax.servlet.http.HttpServlet)3 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)2 Page (com.gargoylesoftware.htmlunit.Page)2 ScriptException (com.gargoylesoftware.htmlunit.ScriptException)2 WebWindowEvent (com.gargoylesoftware.htmlunit.WebWindowEvent)2 WebWindowListener (com.gargoylesoftware.htmlunit.WebWindowListener)2