Search in sources :

Example 76 with CollectingAlertHandler

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

the class HtmlPageTest method onunLoadHandler.

/**
 * @throws Exception if the test fails
 */
@Test
public void onunLoadHandler() throws Exception {
    final String htmlContent = "<html><head><title>foo</title>\n" + "</head><body onunload='alert(\"foo\");alert(\"bar\")'>\n" + "</body></html>";
    final String[] expectedAlerts = { "foo", "bar" };
    final List<String> collectedAlerts = new ArrayList<>();
    final WebClient client = getWebClient();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, htmlContent);
    client.setWebConnection(conn);
    client.getPage(URL_FIRST);
    client.getPage(URL_FIRST);
    assertEquals(expectedAlerts, collectedAlerts);
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 77 with CollectingAlertHandler

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

the class HtmlObjectTest method classid.

/**
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = "undefined", IE = "false")
public void classid() throws Exception {
    if (getBrowserVersion().isIE() && !ActiveXObjectTest.isJacobInstalled()) {
        return;
    }
    final String html = "<html><head>\n" + // Window Media Player CLASSID
    "<object id='wm' classid='clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6'></object>\n" + "<script>\n" + "  function test() {\n" + "    alert(document.all.wm.fullScreen);\n" + "  }\n" + "</script>\n" + "</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);
    client.getPage(URL_FIRST);
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test) ActiveXObjectTest(com.gargoylesoftware.htmlunit.javascript.host.ActiveXObjectTest) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 78 with CollectingAlertHandler

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

the class HtmlScriptTest method scriptCloneDoesNotReloadScript.

/**
 * Verifies that cloned script nodes do not reload or re-execute their content (bug 1954869).
 * @throws Exception if an error occurs
 */
@Test
@Alerts("loaded")
public void scriptCloneDoesNotReloadScript() throws Exception {
    final String html = "<html><body><script src='" + URL_SECOND + "'></script></body></html>";
    final String js = "alert('loaded')";
    final WebClient client = getWebClient();
    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, html);
    conn.setResponse(URL_SECOND, js, MimeType.APPLICATION_JAVASCRIPT);
    client.setWebConnection(conn);
    final List<String> actual = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(actual));
    final HtmlPage page = client.getPage(URL_FIRST);
    assertEquals(2, conn.getRequestCount());
    page.cloneNode(true);
    assertEquals(2, conn.getRequestCount());
    assertEquals(getExpectedAlerts(), actual);
}
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) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 79 with CollectingAlertHandler

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

the class HtmlScriptTest method whitespaceInSrc.

/**
 * Verifies that we're lenient about whitespace before and after URLs in the "src" attribute.
 * @throws Exception if an error occurs
 */
@Test
@Alerts("ok")
public void whitespaceInSrc() throws Exception {
    final String html = "<html><head><script src=' " + URL_SECOND + " '></script></head><body>abc</body></html>";
    final String js = "alert('ok')";
    final WebClient client = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, html);
    webConnection.setResponse(URL_SECOND, js);
    client.setWebConnection(webConnection);
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    client.getPage(URL_FIRST);
    assertEquals(getExpectedAlerts(), 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) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 80 with CollectingAlertHandler

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

the class GeolocationTest method getCurrentPosition.

private void getCurrentPosition(final boolean geolocationEnabled) throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/test", GetCurrentPositionTestServlet.class);
    servlets.put("/browserLocation", BrowserLocationServlet.class);
    startWebServer("./", new String[0], servlets);
    Geolocation.setProviderUrl(URL_FIRST + "browserLocation");
    final WebClient client = getWebClient();
    if (geolocationEnabled) {
        client.getOptions().setGeolocationEnabled(true);
    }
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    client.getPage(URL_FIRST + "test");
    client.waitForBackgroundJavaScript(2000);
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) 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