Search in sources :

Example 41 with CollectingAlertHandler

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

the class LocationTest method fileUrlFormat.

/**
 * @throws Exception if an error occurs
 */
@Test
public void fileUrlFormat() throws Exception {
    final URL url = getClass().getResource("LocationTest_fileUrlFormat.html");
    assertNotNull(url);
    final WebClient client = getWebClient();
    final List<String> alerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(alerts));
    client.getPage(url);
    assertEquals(1, alerts.size());
    assertTrue(alerts.get(0).startsWith("file:///"));
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) Test(org.junit.Test)

Example 42 with CollectingAlertHandler

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

the class LocationTest method href.

/**
 * @throws Exception if the test fails
 */
@Test
public void href() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final String content = "<html><head><title>First</title><script>\n" + "function test() {\n" + "  alert(window.location.href);\n" + "}\n" + "</script></head><body onload='test()'>\n" + "</body></html>";
    webConnection.setResponse(new URL("http://myHostName/"), content);
    webClient.setWebConnection(webConnection);
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    webClient.getPage("http://myHostName");
    final String[] expectedAlerts = { "http://myHostName/" };
    assertEquals(expectedAlerts, collectedAlerts);
}
Also used : ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) Test(org.junit.Test)

Example 43 with CollectingAlertHandler

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

the class LocationTest method setHash.

/**
 * Verifies that modifying <tt>window.location.hash</tt> works, but that it doesn't
 * force the page to reload from the server. This is very important for the Dojo
 * unit tests, which will keep reloading themselves if the page gets reloaded.
 *
 * @throws Exception if the test fails
 */
@Test
public void setHash() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection conn = new MockWebConnection();
    final String html = "<html><head><title>Test</title></head><body>\n" + "<a id='a' onclick='alert(location.hash);location.hash=\"b\";alert(location.hash);'>go</a>\n" + "<h2 id='b'>...</h2></body></html>";
    conn.setResponse(URL_FIRST, html);
    webClient.setWebConnection(conn);
    final List<String> actual = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(actual));
    final HtmlPage page = webClient.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a");
    final HtmlPage page2 = anchor.click();
    // Verify that it worked.
    final String[] expected = new String[] { "", "#b" };
    assertEquals(expected, actual);
    // Verify that we didn't reload the page.
    assertTrue(page == page2);
    assertEquals(URL_FIRST, conn.getLastWebRequest().getUrl());
}
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)

Example 44 with CollectingAlertHandler

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

the class ActiveXObjectTest method method.

/**
 * @throws Exception if the test fails
 */
@Test
public void method() throws Exception {
    if (!getBrowserVersion().isIE()) {
        return;
    }
    if (!isJacobInstalled()) {
        return;
    }
    final String html = "<html><head>\n" + "<script>\n" + "  function test() {\n" + "    try {\n" + "      var ie = new ActiveXObject('InternetExplorer.Application');\n" + "      ie.PutProperty('Hello', 'There');\n" + "      document.tile = ie.GetProperty('Hello'));\n" + "    } catch(e) {document.title = '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("There", 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 45 with CollectingAlertHandler

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

the class Event3Test method testEventBubblingReturns.

private void testEventBubblingReturns(final String onclick1, final String onclick2, final String onclick3, final boolean changesPage) throws Exception {
    final String html1 = "<html><head><title>First</title></head><body>\n" + "<div onclick='alert(\"d\"); " + onclick1 + "'>\n" + "<span onclick='alert(\"s\"); " + onclick2 + "'>\n" + "<a href='" + URL_SECOND + "' id='a' onclick='alert(\"a\"); " + onclick3 + "'>go</a>\n" + "</span>\n" + "</div>\n" + "</body></html>";
    final String html2 = "<html><head><title>Second</title></head><body></body></html>";
    final WebClient client = getWebClientWithMockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setResponse(URL_FIRST, html1);
    webConnection.setResponse(URL_SECOND, html2);
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a");
    final HtmlPage secondPage = anchor.click();
    assertEquals(new String[] { "a", "s", "d" }, collectedAlerts);
    if (changesPage) {
        assertNotSame(page, secondPage);
    } else {
        assertSame(page, secondPage);
    }
}
Also used : HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) 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)

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