Search in sources :

Example 61 with CollectingAlertHandler

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

the class HtmlAnchor2Test method clickCtrlJavascript.

/**
 * @throws Exception if the test fails
 */
@Test
public void clickCtrlJavascript() 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(false, true, false);
    assertEquals(2, getWebClient().getTopLevelWindows().size());
    assertEquals("First", secondPage.getTitleText());
}
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 62 with CollectingAlertHandler

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

the class XHtmlPageTest method xpath1.

/**
 * Regression test for Bug #764. Originally located in {@link com.gargoylesoftware.htmlunit.xml.XmlPageTest}.
 * @throws Exception if an error occurs
 */
@Test
public void xpath1() throws Exception {
    final String html = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE html PUBLIC \n" + "  \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n" + "  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" + "<html xmlns='http://www.w3.org/1999/xhtml' xmlns:xhtml='http://www.w3.org/1999/xhtml'>\n" + "<body><DIV>foo</DIV></body>\n" + "</html>";
    final WebClient client = getWebClient();
    final List<String> actual = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(actual));
    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, html, MimeType.TEXT_XML);
    client.setWebConnection(conn);
    final XHtmlPage page = client.getPage(URL_FIRST);
    final DomNode body = page.getDocumentElement().getFirstChild().getNextSibling();
    final DomNode div = body.getFirstChild();
    assertEquals(HtmlBody.class, body.getClass());
    assertEquals("body", body.getLocalName());
    assertEquals("DIV", div.getLocalName());
    assertNotNull(page.getFirstByXPath(".//xhtml:body"));
    assertNotNull(page.getFirstByXPath(".//xhtml:DIV"));
    assertNull(page.getFirstByXPath(".//xhtml:div"));
}
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 63 with CollectingAlertHandler

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

the class XMLHttpRequest3Test method noParallelJSExecutionInPage.

/**
 * Asynchronous callback should be called in "main" js thread and not parallel to other js execution.
 * See http://sourceforge.net/p/htmlunit/bugs/360/.
 * @throws Exception if the test fails
 */
@Test
public void noParallelJSExecutionInPage() throws Exception {
    final String content = "<html><head><script>\n" + "var j = 0;\n" + "function test() {\n" + "  req = new XMLHttpRequest();\n" + "  req.onreadystatechange = handler;\n" + "  req.open('post', 'foo.xml', true);\n" + "  req.send('');\n" + "  alert('before long loop');\n" + "  for (var i = 0; i < 5000; i++) {\n" + "    j = j + 1;\n" + "  }\n" + "  alert('after long loop');\n" + "}\n" + "function handler() {\n" + "  if (req.readyState == 4) {\n" + "    alert('ready state handler, content loaded: j=' + j);\n" + "  }\n" + "}\n" + "</script></head>\n" + "<body onload='test()'></body></html>";
    final WebClient client = getWebClient();
    final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection conn = new MockWebConnection() {

        @Override
        public WebResponse getResponse(final WebRequest webRequest) throws IOException {
            collectedAlerts.add(webRequest.getUrl().toExternalForm());
            return super.getResponse(webRequest);
        }
    };
    conn.setResponse(URL_FIRST, content);
    final URL urlPage2 = new URL(URL_FIRST, "foo.xml");
    conn.setResponse(urlPage2, "<foo/>\n", MimeType.TEXT_XML);
    client.setWebConnection(conn);
    client.getPage(URL_FIRST);
    assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(1000));
    final String[] alerts = { URL_FIRST.toExternalForm(), "before long loop", "after long loop", urlPage2.toExternalForm(), "ready state handler, content loaded: j=5000" };
    assertEquals(alerts, collectedAlerts);
}
Also used : WebRequest(com.gargoylesoftware.htmlunit.WebRequest) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) Test(org.junit.Test)

Example 64 with CollectingAlertHandler

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

the class JavaScriptEngineTest method shutdownShouldKill.

/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("starting")
public void shutdownShouldKill() throws Exception {
    final String html = "<html>\n" + "<head><title>Test page</title>\n" + "<script>\n" + "  function test() {\n" + "    alert('starting');\n" + "    while(true) { Math.sin(3.14) * Math.sin(3.14);}\n" + "  }\n" + "</script>\n" + "</head>\n" + "<body onload='setTimeout(test, 50);'>\n" + "</body></html>";
    try (WebClient webClient = getWebClient()) {
        final List<String> collectedAlerts = new ArrayList<>();
        webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
        loadPage(html);
        Thread.sleep(100);
        assertEquals(getExpectedAlerts(), collectedAlerts);
    }
    Thread.sleep(400);
    assertTrue(getJavaScriptThreads().isEmpty());
}
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)

Example 65 with CollectingAlertHandler

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

the class JavaScriptEngineTest method prototypeScope.

private void prototypeScope(final String name, final String value) throws Exception {
    final String content1 = "<html><head>\n" + "<script>\n" + "window.open('second.html', 'secondWindow');\n" + "</script>\n" + "</head><body></body></html>";
    final String content2 = "<html><head>\n" + "<script>\n" + "alert('in page 2');\n" + name + ".prototype.foo = function() {\n" + "   alert('in foo');\n" + "};\n" + "var x = " + value + ";\n" + "x.foo();\n" + "</script>\n" + "</head><body></body></html>";
    final String[] expectedAlerts = { "in page 2", "in foo" };
    final WebClient client = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setDefaultResponse(content2);
    webConnection.setResponse(URL_FIRST, content1);
    client.setWebConnection(webConnection);
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    client.getPage(URL_FIRST);
    assertEquals(expectedAlerts, collectedAlerts);
}
Also used : ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient)

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