Search in sources :

Example 66 with CollectingAlertHandler

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

the class JavaScriptEngineTest method scopeOfNewFunctionCalledFormOtherWindow.

/**
 * Checks that a dynamically compiled function works in the scope of its birth and not the other window.
 * @throws Exception if the test fails
 */
@Test
public void scopeOfNewFunctionCalledFormOtherWindow() throws Exception {
    final String firstContent = "<html><head>\n" + "<script>\n" + "var foo = 'foo';\n" + "var test = new Function('alert(foo);');\n" + "</script>\n" + "</head>\n" + "<body onload='test()'>\n" + "  <iframe src='page2.html'/>\n" + "</body>\n" + "</html>";
    final String secondContent = "<html><head><script>\n" + "var foo = 'foo2';\n" + "parent.test();\n" + "var f = parent.test;\n" + "f();\n" + "</script></head></html>";
    final WebClient client = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setDefaultResponse(secondContent);
    webConnection.setResponse(URL_FIRST, firstContent);
    client.setWebConnection(webConnection);
    final String[] expectedAlerts = { "foo", "foo", "foo" };
    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) Test(org.junit.Test)

Example 67 with CollectingAlertHandler

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

the class ActiveXObjectTest method setProperty.

/**
 * @throws Exception if the test fails
 */
@Test
public void setProperty() throws Exception {
    if (!getBrowserVersion().isIE()) {
        return;
    }
    if (!isJacobInstalled()) {
        return;
    }
    final String html = "<html><head><title>foo</title><script>\n" + "  function test() {\n" + "    try {\n" + "      var ie = new ActiveXObject('InternetExplorer.Application');\n" + "      var full = ie.FullScreen;\n" + "      ie.FullScreen = true;\n" + "      alert(ie.FullScreen);\n" + "      ie.FullScreen = full;\n" + "    } catch(e) {alert('exception: ' + e.message);}\n" + "  }\n" + "</script></head><body onload='test()'>\n" + "</body></html>";
    final WebClient client = getWebClient();
    client.getOptions().setActiveXNative(true);
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, html);
    client.setWebConnection(webConnection);
    final HtmlPage page = client.getPage(URL_FIRST);
    assertEquals("True", page.getTitleText());
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 68 with CollectingAlertHandler

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

the class HtmlFrameSetTest method onunload.

/**
 * @throws Exception if the test fails
 */
@Test
@NotYetImplemented
public void onunload() throws Exception {
    final String mainHtml = "<frameset onunload=\"document.location.href='3.html'\">\n" + "<frame name='f1' src='1.html'/>\n" + "</frameset>";
    final String frame1 = "<html><head><title>1</title></head>\n" + "<body><button id='myButton' onclick=\"top.location.href='2.html'\"/></body>\n" + "</html>";
    final String html2 = "<html><head><title>2</title></head>\n" + "<body>hello</body>\n" + "</html>";
    final String html3 = "<html><head><title>3</title></head>\n" + "<body>hello</body>\n" + "</html>";
    final WebClient webClient = getWebClientWithMockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection conn = getMockWebConnection();
    conn.setResponse(URL_FIRST, mainHtml);
    conn.setResponse(new URL(URL_FIRST, "1.html"), frame1);
    conn.setResponse(new URL(URL_FIRST, "2.html"), html2);
    conn.setResponse(new URL(URL_FIRST, "3.html"), html3);
    final HtmlPage mainPage = webClient.getPage(URL_FIRST);
    final HtmlPage framePage = (HtmlPage) mainPage.getFrameByName("f1").getEnclosedPage();
    final HtmlPage page = framePage.getHtmlElementById("myButton").click();
    assertEquals("3", page.getTitleText());
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) NotYetImplemented(com.gargoylesoftware.htmlunit.junit.BrowserRunner.NotYetImplemented) Test(org.junit.Test)

Example 69 with CollectingAlertHandler

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

the class HTMLDocument2Test method cookieInLocalFile.

/**
 * Verifies that cookies work when working with local files (not remote sites with real domains).
 * Required for local testing of Dojo 1.1.1.
 * @throws Exception if an error occurs
 */
@Test
@Alerts({ "", "", "blah=bleh" })
public void cookieInLocalFile() throws Exception {
    final WebClient client = getWebClient();
    final List<String> actual = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(actual));
    final URL url = getClass().getResource("HTMLDocumentTest_cookieInLocalFile.html");
    client.getPage(url);
    assertEquals(getExpectedAlerts(), actual);
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 70 with CollectingAlertHandler

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

the class HTMLElement3Test method clickHashAnchor.

/**
 * @throws Exception if the test fails
 */
@Test
public void clickHashAnchor() throws Exception {
    final String html = "<html><head><title>HashAnchor</title></head>\n" + "<body>\n" + "  <script language='javascript'>\n" + "    function test() {alert('test hash');}\n" + "  </script>\n" + "  <a onClick='javascript:test();' href='#' name='hash'>Click</a>\n" + "</body>\n" + "</html>";
    final String[] expectedAlerts = { "test hash" };
    // first use direct load
    final List<String> loadCollectedAlerts = new ArrayList<>();
    final HtmlPage loadPage = loadPage(html, loadCollectedAlerts);
    final HtmlAnchor loadHashAnchor = loadPage.getAnchorByName("hash");
    loadHashAnchor.click();
    assertEquals(expectedAlerts, loadCollectedAlerts);
    // finally try via the client
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, html);
    webClient.setWebConnection(webConnection);
    final CollectingAlertHandler clientCollectedAlertsHandler = new CollectingAlertHandler();
    webClient.setAlertHandler(clientCollectedAlertsHandler);
    final HtmlPage clientPage = webClient.getPage(URL_FIRST);
    final HtmlAnchor clientHashAnchor = clientPage.getAnchorByName("hash");
    clientHashAnchor.click();
    assertEquals(expectedAlerts, clientCollectedAlertsHandler.getCollectedAlerts());
}
Also used : HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) 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) 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