Search in sources :

Example 46 with CollectingAlertHandler

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

the class XMLHttpRequest3Test method ajaxInfluencesSubmitHeaders.

/**
 * Test for a strange error we found: An ajax running
 * in parallel shares the additional headers with a form
 * submit.
 *
 * @throws Exception if an error occurs
 */
@Test
public void ajaxInfluencesSubmitHeaders() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/content.html", ContentServlet.class);
    servlets.put("/ajax_headers.html", AjaxHeaderServlet.class);
    servlets.put("/form_headers.html", FormHeaderServlet.class);
    startWebServer("./", null, servlets);
    collectedHeaders_.clear();
    XMLHttpRequest3Test.STATE_ = 0;
    final WebClient client = getWebClient();
    final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final HtmlPage page = client.getPage(URL_FIRST + "content.html");
    final DomElement elem = page.getElementById("doIt");
    while (STATE_ < 1) {
        Thread.sleep(42);
    }
    ((HtmlSubmitInput) elem).click();
    client.waitForBackgroundJavaScript(DEFAULT_WAIT_TIME);
    assertEquals(collectedHeaders_.toString(), 2, collectedHeaders_.size());
    String headers = collectedHeaders_.get(0);
    if (!headers.startsWith("Form: ")) {
        headers = collectedHeaders_.get(1);
    }
    assertTrue(headers, headers.startsWith("Form: "));
    assertFalse(headers, headers.contains("Html-Unit=is great,;"));
    headers = collectedHeaders_.get(0);
    if (!headers.startsWith("Ajax: ")) {
        headers = collectedHeaders_.get(1);
    }
    assertTrue(headers, headers.startsWith("Ajax: "));
    assertTrue(headers, headers.contains("Html-Unit=is great,;"));
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HashMap(java.util.HashMap) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlSubmitInput(com.gargoylesoftware.htmlunit.html.HtmlSubmitInput) StreamingServlet(com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequestTest.StreamingServlet) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 47 with CollectingAlertHandler

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

the class XMLHttpRequest3Test method asyncUseWithNetworkConnectionFailure.

/**
 * 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", "4", MSG_NO_CONTENT, MSG_PROCESSING_ERROR }, IE = { "0", "1", "1", "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 == null)\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);
}
Also used : CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 48 with CollectingAlertHandler

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

the class XMLHttpRequest3Test method thisValueInHandler.

/**
 * Tests the value of "this" in handler.
 * @throws Exception if the test fails
 */
@Test
@Alerts("this == request")
public void thisValueInHandler() 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.open('GET', 'foo.xml', true);\n" + "        request.send('');\n" + "      }\n" + "      function onReadyStateChange() {\n" + "        if (request.readyState == 4) {\n" + "          if (this == request)\n" + "            alert('this == request');\n" + "          else if (this == onReadyStateChange)\n" + "            alert('this == handler');\n" + "          else alert('not expected: ' + this)\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 MockWebConnection();
    conn.setResponse(URL_FIRST, html);
    conn.setDefaultResponse("");
    client.setWebConnection(conn);
    client.getPage(URL_FIRST);
    assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(1000));
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
Also used : CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 49 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project maven-doxia-sitetools by apache.

the class JavascriptVerifier method verify.

/**
 * Verifies a HtmlPage.
 *
 * @param file the file to verify.
 *
 * @throws Exception if something goes wrong.
 */
public void verify(String file) throws Exception {
    File jsTest = getTestFile("target/output/javascript.html");
    assertNotNull(jsTest);
    assertTrue(jsTest.exists());
    // HtmlUnit
    try (WebClient webClient = new WebClient()) {
        webClient.getOptions().setCssEnabled(false);
        final List<String> collectedAlerts = new ArrayList<String>(4);
        webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
        HtmlPage page = (HtmlPage) webClient.getPage(jsTest.toURI().toURL());
        assertNotNull(page);
        HtmlElement element = page.getHtmlElementById("contentBox");
        assertNotNull(element);
        HtmlDivision division = (HtmlDivision) element;
        assertNotNull(division);
        Iterator<HtmlElement> elementIterator = division.getHtmlElementDescendants().iterator();
        // ----------------------------------------------------------------------
        // 
        // ----------------------------------------------------------------------
        HtmlSection section = (HtmlSection) elementIterator.next();
        assertNotNull(section);
        HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
        assertNotNull(h2);
        assertEquals("Test", h2.asText().trim());
        HtmlAnchor a = (HtmlAnchor) elementIterator.next();
        assertNotNull(a);
        assertEquals("Test", a.getAttribute("name"));
        HtmlParagraph p = (HtmlParagraph) elementIterator.next();
        assertNotNull(p);
        assertEquals("You should see a JavaScript alert...", p.asText().trim());
        HtmlScript script = (HtmlScript) elementIterator.next();
        assertNotNull(script);
        assertEquals("text/javascript", script.getAttribute("type"));
        assertEquals("", script.asText().trim());
        List<String> expectedAlerts = Collections.singletonList("Hello!");
        assertEquals(expectedAlerts, collectedAlerts);
    }
}
Also used : HtmlScript(com.gargoylesoftware.htmlunit.html.HtmlScript) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlHeading2(com.gargoylesoftware.htmlunit.html.HtmlHeading2) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) ArrayList(java.util.ArrayList) HtmlDivision(com.gargoylesoftware.htmlunit.html.HtmlDivision) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) HtmlParagraph(com.gargoylesoftware.htmlunit.html.HtmlParagraph) HtmlSection(com.gargoylesoftware.htmlunit.html.HtmlSection) PlexusExtension.getTestFile(org.codehaus.plexus.testing.PlexusExtension.getTestFile) File(java.io.File)

Example 50 with CollectingAlertHandler

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

the class WindowConcurrencyTest method setTimeoutStopped.

/**
 * Test that a script started by a timer is stopped if the page that started it
 * is not loaded anymore.
 * @throws Exception if the test fails
 */
@Test
public void setTimeoutStopped() throws Exception {
    final String firstContent = "<html><head>\n" + "<script language='JavaScript'>window.setTimeout('alert(\"Yo!\")', 10000);</script>\n" + "</head><body onload='document.location.replace(\"" + URL_SECOND + "\")'></body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
    final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
    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);
    assertEquals(0, page.getWebClient().waitForBackgroundJavaScript(2000));
    assertEquals("Second", page.getTitleText());
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) Test(org.junit.Test)

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