Search in sources :

Example 51 with CollectingAlertHandler

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

the class WindowConcurrencyTest method verifyGoingToNewPageStopsJavaScript.

/**
 * Verifies that when you go to a new page, background JS jobs are stopped (see bug 2127419).
 * @throws Exception if the test fails
 */
@Test
public void verifyGoingToNewPageStopsJavaScript() throws Exception {
    final String html1 = "<html><head><title>foo</title><script>\n" + "  function f() {\n" + "    alert('Oh no!');\n" + "  }\n" + "  function test() {\n" + "    window.timeoutId = setInterval(f, 1000);\n" + "  }\n" + "</script></head><body onload='test()'>\n" + "</body></html>";
    final String html2 = "<html></html>";
    final List<String> collectedAlerts = new ArrayList<>();
    client_.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, html1);
    conn.setResponse(URL_SECOND, html2);
    client_.setWebConnection(conn);
    client_.getPage(URL_FIRST);
    client_.getPage(URL_SECOND);
    client_.waitForBackgroundJavaScript(5000);
    client_.waitForBackgroundJavaScript(5000);
    assertTrue(collectedAlerts.isEmpty());
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) Test(org.junit.Test)

Example 52 with CollectingAlertHandler

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

the class WindowTest method close.

/**
 * Test closing using JavaScript.
 * @throws Exception if the test fails
 */
@Test
public void close() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "<a href='" + URL_SECOND + "' id='link' target='_blank'>Link</a>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body>\n" + "<h1>Second</h1><form>\n" + "<input type='submit' name='action' value='Close' id='button' " + "onclick='window.close(); return false;'>\n" + "</form></body></html>";
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    assertEquals(1, webClient.getWebWindows().size());
    final WebWindow firstWindow = firstPage.getEnclosingWindow();
    final HtmlPage secondPage = firstPage.getHtmlElementById("link").click();
    assertEquals("Second", secondPage.getTitleText());
    assertEquals(2, webClient.getWebWindows().size());
    final WebWindow secondWindow = secondPage.getEnclosingWindow();
    assertNotSame(firstWindow, secondWindow);
    final List<WebWindowEvent> events = new LinkedList<>();
    webClient.addWebWindowListener(new WebWindowListener() {

        @Override
        public void webWindowOpened(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowContentChanged(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowClosed(final WebWindowEvent event) {
            events.add(event);
        }
    });
    secondPage.getHtmlElementById("button").click();
    final List<WebWindowEvent> expectedEvents = Arrays.asList(new WebWindowEvent[] { new WebWindowEvent(secondWindow, WebWindowEvent.CLOSE, secondPage, null) });
    assertEquals(expectedEvents, events);
    assertEquals(1, webClient.getWebWindows().size());
    assertEquals(firstWindow, webClient.getCurrentWindow());
    assertEquals(Collections.EMPTY_LIST, 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) LinkedList(java.util.LinkedList) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) WebWindowListener(com.gargoylesoftware.htmlunit.WebWindowListener) WebWindowEvent(com.gargoylesoftware.htmlunit.WebWindowEvent) Test(org.junit.Test)

Example 53 with CollectingAlertHandler

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

the class WindowTest method openWindow_base.

/**
 * @throws Exception if the test fails
 */
@Test
public void openWindow_base() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final String firstContent = "<html><head><title>First</title><base target='MyNewWindow'></head><body>\n" + "<form name='form1'>\n" + "  <a id='link' href='" + URL_SECOND + "'>Click me</a>\n" + "</form>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body>\n" + "<script>alert(self.name)</script>\n" + "</body></html>";
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
    assertEquals(firstWebWindow, firstWebWindow.getTopWindow());
    final HtmlAnchor anchor = firstPage.getHtmlElementById("link");
    final HtmlPage secondPage = anchor.click();
    assertEquals("Second", secondPage.getTitleText());
    assertNotSame(firstPage, secondPage);
    final WebWindow secondWebWindow = secondPage.getEnclosingWindow();
    assertNotSame(firstWebWindow, secondWebWindow);
    assertEquals("MyNewWindow", secondWebWindow.getName());
    assertEquals(secondWebWindow, secondWebWindow.getTopWindow());
    assertEquals(new String[] { "MyNewWindow" }, collectedAlerts);
}
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) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Example 54 with CollectingAlertHandler

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

the class LocationTest method testGetVariousAttributes.

private void testGetVariousAttributes(final String url, final String[] expectedAlerts) throws Exception {
    final WebClient client = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final String content = "<html><head><title>First</title><script>\n" + "function doTest() {\n" + "  var location = document.location;\n" + "  alert(location.hash);\n" + "  alert(location.host);\n" + "  alert(location.hostname);\n" + "  alert(location.href);\n" + "  alert(location.pathname);\n" + "  alert(location.port);\n" + "  alert(location.protocol);\n" + "  alert(location.search);\n" + "}\n</script></head>\n" + "<body onload='doTest()'></body></html>";
    webConnection.setDefaultResponse(content);
    client.setWebConnection(webConnection);
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    // Try page with only a server name
    client.getPage(url);
    assertEquals(expectedAlerts, collectedAlerts);
}
Also used : ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 55 with CollectingAlertHandler

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

the class LocationTest method locationWithTarget.

/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("c")
public void locationWithTarget() throws Exception {
    final WebClient client = getWebClient();
    final List<String> alerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(alerts));
    final URL url = getClass().getResource("LocationTest_locationWithTarget_a.html");
    assertNotNull(url);
    final HtmlPage a = client.getPage(url);
    final HtmlPage c = (HtmlPage) a.getFrameByName("c").getEnclosedPage();
    c.getHtmlElementById("anchor").click();
    assertEquals(getExpectedAlerts(), alerts);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) 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)

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