Search in sources :

Example 16 with CollectingAlertHandler

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

the class WindowTest method prompt_noPromptHandler.

/**
 * @throws Exception if the test fails
 */
@Test
public void prompt_noPromptHandler() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    final List<String> collectedPrompts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final String firstContent = "<html><head><title>First</title><script>function doTest() {alert(prompt('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(Collections.EMPTY_LIST, collectedPrompts);
    assertEquals(new String[] { "null" }, 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)

Example 17 with CollectingAlertHandler

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

the class WindowTest method promptWithDefault.

/**
 * @throws Exception if the test fails
 */
@Test
public void promptWithDefault() 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);
                collectedPrompts.add(defaultValue);
                return defaultValue;
            });
            final String html = "<html><head><title>First</title>\n" + "<script>function doTest() {alert(prompt('foo', 'some default'))}</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", "some default" }, collectedPrompts);
            assertEquals(new String[] { "some default" }, 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)

Example 18 with CollectingAlertHandler

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

the class WindowTest method openWindow_blocked.

/**
 * Verifies that <tt>window.open</tt> behaves correctly when popups are blocked.
 * @throws Exception if an error occurs
 */
@Test
public void openWindow_blocked() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "<script>\n" + "  var w;\n" + "  function test() {\n" + "    w = window.open('', 'foo');\n" + "  }\n" + "</script>\n" + "</head>\n" + "<body onload='test()'>\n" + "<div id='d' onclick='alert(w)'>test</div>\n" + "</body></html>";
    final List<String> actual = new ArrayList<>();
    final WebClient client = getWebClient();
    client.getOptions().setPopupBlockerEnabled(true);
    client.setAlertHandler(new CollectingAlertHandler(actual));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setDefaultResponse(html);
    client.setWebConnection(webConnection);
    final HtmlPage page = client.getPage("http://foo");
    page.getHtmlElementById("d").click();
    final String[] expected = { "null" };
    assertEquals(expected, actual);
}
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 19 with CollectingAlertHandler

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

the class WindowTest method parentAndTop.

/**
 * @throws Exception if the test fails
 */
@Test
public void parentAndTop() throws Exception {
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "  <iframe name='left' src='" + URL_SECOND + "'></iframe>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body>\n" + "  <iframe name='innermost' src='" + URL_THIRD + "'></iframe>\n" + "</body></html>";
    final String thirdContent = "<html><head><title>Third</title><script>\n" + "function doAlert() {\n" + "  alert(parent != this);\n" + "  alert(top != this);\n" + "  alert(parent != top);\n" + "  alert(parent.parent == top);\n" + "  alert(parent.frames[0] == this);\n" + "  alert(top.frames[0] == parent);\n" + "}\n" + "</script></head>\n" + "<body><a id='clickme' onClick='doAlert()'>foo</a></body></html>";
    final WebClient webClient = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    webConnection.setResponse(URL_THIRD, thirdContent);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    final WebWindow innermostWebWindow = webClient.getWebWindowByName("innermost");
    final HtmlPage innermostPage = (HtmlPage) innermostWebWindow.getEnclosedPage();
    innermostPage.getHtmlElementById("clickme").click();
    assertNotSame(innermostWebWindow.getParentWindow(), innermostWebWindow);
    assertNotSame(innermostWebWindow.getTopWindow(), innermostWebWindow);
    assertNotSame(innermostWebWindow.getParentWindow(), innermostWebWindow.getTopWindow());
    assertSame(innermostWebWindow.getParentWindow().getParentWindow(), innermostWebWindow.getTopWindow());
    assertEquals(new String[] { "true", "true", "true", "true", "true", "true" }, collectedAlerts);
}
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) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Example 20 with CollectingAlertHandler

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

the class HTMLObjectElementTest method activeXInteraction.

/**
 * Simple hack to proof, that a test driver can manipulate
 * a activeX mock at runtime.
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = {}, IE = { "Javascript called this method!", "ActiveX is still alive" })
public void activeXInteraction() throws Exception {
    final String clsid = "clsid:TESTING-CLASS-ID";
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n" + "<script>\n" + "  function test() {\n" + "    var obj = document.all.id1;\n" + "    if (obj.GetMessage) {\n" + "      alert(obj.GetMessage());\n" + "    }\n" + "  }\n" + "</script>\n" + "</head>\n" + "<body>\n" + "  <object id='id1' classid='" + clsid + "'></object>\n" + "  <button id='myButton' onClick='test()'>Click Me</button>\n" + "</body></html>";
    final WebClient client = getWebClientWithMockWebConnection();
    final Map<String, String> activeXObjectMap = new HashMap<>();
    activeXObjectMap.put(clsid, "com.gargoylesoftware.htmlunit.javascript.MockActiveXObject");
    client.setActiveXObjectMap(activeXObjectMap);
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final HtmlPage page = loadPage(html);
    page.getHtmlElementById("myButton").click();
    final HtmlElement elem = page.getHtmlElementById("id1");
    final HTMLObjectElement jsElem = (HTMLObjectElement) elem.getScriptableObject();
    final MockActiveXObject activeX = (MockActiveXObject) jsElem.unwrap();
    if (null != activeX) {
        activeX.setMessage("ActiveX is still alive");
        page.getHtmlElementById("myButton").click();
    }
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
Also used : HashMap(java.util.HashMap) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) MockActiveXObject(com.gargoylesoftware.htmlunit.javascript.MockActiveXObject) HtmlPageTest(com.gargoylesoftware.htmlunit.html.HtmlPageTest) 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