Search in sources :

Example 6 with CollectingAlertHandler

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

the class HtmlAnchor2Test method clickShiftJavascript.

/**
 * @throws Exception if the test fails
 */
@Test
public void clickShiftJavascript() throws Exception {
    final String first = "<html><head><title>First</title></head><body>\n" + "  <a href='javascript: window.location=\"" + URL_SECOND + "\"' id='a2'>link to foo2</a>\n" + "</body></html>";
    final String second = "<html><head><title>Second</title></head><body></body></html>";
    final WebClient client = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, first);
    webConnection.setResponse(URL_SECOND, second);
    client.setWebConnection(webConnection);
    assertEquals(1, getWebClient().getTopLevelWindows().size());
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");
    final HtmlPage secondPage = anchor.click(true, false, false);
    assertEquals(2, getWebClient().getTopLevelWindows().size());
    assertEquals("Second", secondPage.getTitleText());
}
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 7 with CollectingAlertHandler

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

the class ClickableElementTest method onClickPageTest.

/**
 * Full page driver for onClick tests.
 *
 * @param htmlContent HTML fragment for body of page with clickable element identified by clickId ID attribute
 * @param numClicks number of times to click element
 * @param expectedAlerts array of expected popup values
 * @param exceptionOnError
 * @throws Exception if the test fails
 */
private void onClickPageTest(final String htmlContent, final int numClicks, final boolean exceptionOnError) throws Exception {
    final WebClient client = getWebClientWithMockWebConnection();
    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setDefaultResponse(htmlContent);
    client.getOptions().setThrowExceptionOnScriptError(exceptionOnError);
    final List<String> collectedAlerts = new ArrayList<>();
    final CollectingAlertHandler alertHandler = new CollectingAlertHandler(collectedAlerts);
    client.setAlertHandler(alertHandler);
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlElement clickable = page.getHtmlElementById("clickId");
    for (int i = 0; i < numClicks; i++) {
        clickable.click();
    }
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
Also used : ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 8 with CollectingAlertHandler

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

the class HtmlScriptTest method addEventListener_error.

private void addEventListener_error(final boolean throwOnFailingStatusCode) throws Exception {
    final URL fourOhFour = new URL(URL_FIRST, "/404");
    final String html = "<html><head>\n" + "<script>\n" + "  function test() {\n" + "    var s1 = document.createElement('script');\n" + "    s1.text = 'var foo';\n" + "    s1.addEventListener('error', function() {alert('a')}, false);\n" + "    document.body.insertBefore(s1, document.body.firstChild);\n" + "    \n" + "    var s2 = document.createElement('script');\n" + "    s2.text = 'varrrr foo';\n" + "    s2.addEventListener('error', function() {alert('b')}, false);\n" + "    document.body.insertBefore(s2, document.body.firstChild);\n" + "    \n" + "    var s3 = document.createElement('script');\n" + "    s3.src = '//:';\n" + "    s3.addEventListener('error', function() {alert('c')}, false);\n" + "    document.body.insertBefore(s3, document.body.firstChild);\n" + "    \n" + "    var s4 = document.createElement('script');\n" + "    s4.src = '" + URL_SECOND + "';\n" + "    s4.addEventListener('error', function() {alert('d')}, false);\n" + "    document.body.insertBefore(s4, document.body.firstChild);\n" + "    \n" + "    var s5 = document.createElement('script');\n" + "    s5.src = '" + URL_THIRD + "';\n" + "    s5.addEventListener('error', function() {alert('e')}, false);\n" + "    document.body.insertBefore(s5, document.body.firstChild);\n" + "    \n" + "    var s6 = document.createElement('script');\n" + "    s6.src = '" + fourOhFour + "';\n" + "    s6.addEventListener('error', function() {alert('f')}, false);\n" + "    document.body.insertBefore(s6, document.body.firstChild);\n" + "  }\n" + "</script>\n" + "</head>\n" + "<body onload='test()'></body>\n" + "</html>";
    final WebClient client = getWebClient();
    client.getOptions().setThrowExceptionOnFailingStatusCode(throwOnFailingStatusCode);
    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, html);
    conn.setResponse(URL_SECOND, "var foo;", MimeType.APPLICATION_JAVASCRIPT);
    conn.setResponse(URL_THIRD, "varrrr foo;", MimeType.APPLICATION_JAVASCRIPT);
    conn.setResponse(fourOhFour, "", 404, "Missing", MimeType.APPLICATION_JAVASCRIPT, new ArrayList<NameValuePair>());
    client.setWebConnection(conn);
    final List<String> actual = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(actual));
    client.getOptions().setThrowExceptionOnScriptError(false);
    client.getPage(URL_FIRST);
    assertEquals(getExpectedAlerts(), actual);
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL)

Example 9 with CollectingAlertHandler

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

the class HtmlPage2Test method save.

/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("Hello there")
public void save() throws Exception {
    final String html = "<html><head><script src='" + URL_SECOND + "'>\n</script></head></html>";
    final String js = "alert('Hello there')";
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, html);
    webConnection.setResponse(URL_SECOND, js);
    webClient.setWebConnection(webConnection);
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final HtmlPage page = webClient.getPage(URL_FIRST);
    assertEquals(getExpectedAlerts(), collectedAlerts);
    final HtmlScript sript = page.getFirstByXPath("//script");
    assertEquals(URL_SECOND.toString(), sript.getSrcAttribute());
    final File tmpFolder = tmpFolderProvider_.newFolder("hu");
    final File file = new File(tmpFolder, "hu_HtmlPageTest_save.html");
    page.save(file);
    assertTrue(file.exists());
    assertTrue(file.isFile());
    final String content = FileUtils.readFileToString(file, ISO_8859_1);
    assertFalse(content.contains("<script"));
    assertEquals(URL_SECOND.toString(), sript.getSrcAttribute());
}
Also used : ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) File(java.io.File) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 10 with CollectingAlertHandler

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

the class WindowConcurrencyTest method verifyCloseStopsJavaScript.

/**
 * Verifies that when all windows are closed, background JS jobs are stopped (see bug 2127419).
 * @throws Exception if the test fails
 */
@Test
public void verifyCloseStopsJavaScript() throws Exception {
    final String html = "<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 List<String> collectedAlerts = new ArrayList<>();
    client_.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setDefaultResponse(html);
    client_.setWebConnection(webConnection);
    client_.getPage(URL_FIRST);
    client_.close();
    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)

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