Search in sources :

Example 11 with CollectingAlertHandler

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

the class WindowConcurrencyTest method setTimeoutOnFrameWindow.

/**
 * Our Window proxy caused troubles.
 * @throws Exception if the test fails
 */
@Test
public void setTimeoutOnFrameWindow() throws Exception {
    final String html = "<html><head><title>foo</title><script>\n" + "  function test() {\n" + "    frames[0].setTimeout(f, 0);\n" + "  }\n" + "  function f() {\n" + "    alert('in f');\n" + "  }\n" + "</script></head><body onload='test()'>\n" + "<iframe src='about:blank'></iframe>\n" + "</body></html>";
    final List<String> collectedAlerts = new ArrayList<>();
    client_.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection conn = new MockWebConnection();
    conn.setDefaultResponse(html);
    client_.setWebConnection(conn);
    client_.getPage(URL_FIRST);
    assertEquals(0, client_.waitForBackgroundJavaScriptStartingBefore(1000));
    final String[] expectedAlerts = { "in f" };
    assertEquals(expectedAlerts, collectedAlerts);
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) Test(org.junit.Test)

Example 12 with CollectingAlertHandler

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

the class WindowTest method showModalDialog.

/**
 * Basic test for the <tt>showModalDialog</tt> method. See bug #703.
 * @throws Exception if an error occurs
 */
@Test
@Alerts(DEFAULT = { "undefined", "Jane", "Smith", "sdg", "finished" }, CHROME = "not available", EDGE = "not available", FF = "not available", FF_ESR = "not available")
public void showModalDialog() throws Exception {
    final String html1 = "<html><head><script>\n" + "  function test() {\n" + "    if (!window.showModalDialog) {alert('not available'); return; }\n" + "    alert(window.returnValue);\n" + "    var o = new Object();\n" + "    o.firstName = 'Jane';\n" + "    o.lastName = 'Smith';\n" + "    var ret = showModalDialog('myDialog.html', o, 'dialogHeight:300px; dialogLeft:200px;');\n" + "    alert(ret);\n" + "    alert('finished');\n" + "  }\n" + "</script></head><body>\n" + "  <button onclick='test()' id='b'>Test</button>\n" + "</body></html>";
    final String html2 = "<html><head><script>\n" + "  var o = window.dialogArguments;\n" + "  alert(o.firstName);\n" + "  alert(o.lastName);\n" + "  window.returnValue = 'sdg';\n" + "</script></head>\n" + "<body>foo</body></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, html1);
    conn.setResponse(new URL(URL_FIRST, "myDialog.html"), html2);
    client.setWebConnection(conn);
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlElement button = page.getHtmlElementById("b");
    final HtmlPage dialogPage = button.click();
    if (getExpectedAlerts().length > 1) {
        final DialogWindow dialog = (DialogWindow) dialogPage.getEnclosingWindow();
        dialog.close();
    }
    assertEquals(getExpectedAlerts(), actual);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) DialogWindow(com.gargoylesoftware.htmlunit.DialogWindow) URL(java.net.URL) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 13 with CollectingAlertHandler

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

the class WindowTest method status.

/**
 * @throws Exception if the test fails
 */
@Test
public void status() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final String firstContent = "<html><head><title>First</title><script>\n" + "function doTest() {\n" + "  alert(window.status);\n" + "  window.status = 'newStatus';\n" + "  alert(window.status);\n" + "}\n" + "</script></head><body onload='doTest()'>\n" + "</body></html>";
    final URL url = URL_FIRST;
    webConnection.setResponse(url, firstContent);
    webClient.setWebConnection(webConnection);
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final List<String> collectedStatus = new ArrayList<>();
    webClient.setStatusHandler(new StatusHandler() {

        @Override
        public void statusMessageChanged(final Page page, final String message) {
            collectedStatus.add(message);
        }
    });
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    final String[] expectedAlerts = { "", "newStatus" };
    assertEquals("alerts", expectedAlerts, collectedAlerts);
    final String[] expectedStatus = { "newStatus" };
    assertEquals("status", expectedStatus, collectedStatus);
}
Also used : StatusHandler(com.gargoylesoftware.htmlunit.StatusHandler) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) Test(org.junit.Test)

Example 14 with CollectingAlertHandler

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

the class WindowTest method confirm.

/**
 * @throws Exception if the test fails
 */
@Test
public void confirm() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    final List<String> collectedConfirms = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    webClient.setConfirmHandler(new ConfirmHandler() {

        @Override
        public boolean handleConfirm(final Page page, final String message) {
            collectedConfirms.add(message);
            return true;
        }
    });
    final String firstContent = "<html><head><title>First</title><script>function doTest() {alert(confirm('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(new String[] { "foo" }, collectedConfirms);
    assertEquals(new String[] { "true" }, collectedAlerts);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ConfirmHandler(com.gargoylesoftware.htmlunit.ConfirmHandler) ArrayList(java.util.ArrayList) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 15 with CollectingAlertHandler

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

the class WindowTest method prompt.

/**
 * @throws Exception if the test fails
 */
@Test
public void prompt() throws Exception {
    try (WebClient webClient = getWebClient()) {
        try (MockWebConnection webConnection = new MockWebConnection()) {
            final List<String> collectedAlerts = new ArrayList<>();
            final List<String> collectedPrompts = new ArrayList<>();
            webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
            webClient.setPromptHandler((page, message, defaultValue) -> {
                collectedPrompts.add(message);
                return "Flintstone";
            });
            final String html = "<html><head><title>First</title>\n" + "<script>function doTest() {alert(prompt('foo'))}</script>\n" + "</head><body onload='doTest()'></body></html>";
            webConnection.setResponse(URL_FIRST, html);
            webClient.setWebConnection(webConnection);
            final HtmlPage firstPage = webClient.getPage(URL_FIRST);
            assertEquals("First", firstPage.getTitleText());
            assertEquals(new String[] { "foo" }, collectedPrompts);
            assertEquals(new String[] { "Flintstone" }, collectedAlerts);
        }
    }
}
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) 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