Search in sources :

Example 31 with CollectingAlertHandler

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

the class HTMLDocumentWriteTest method writeScriptInManyTimes.

/**
 * @throws Exception if the test fails
 */
@Test
public void writeScriptInManyTimes() throws Exception {
    final String html = "<html><head><title>foo</title>\n" + "<script>\n" + "document.write('<script src=\"script.js\">');\n" + "document.write('<' + '/script>');\n" + "document.write('<script>alert(\"foo2\");</' + 'script>');\n" + "</script>\n" + "</head>\n" + "<body>\n" + "</body></html>";
    final String[] expectedAlerts = { "foo", "foo2" };
    final URL scriptUrl = new URL(URL_FIRST, "script.js");
    final WebClient client = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    client.setWebConnection(webConnection);
    webConnection.setDefaultResponse(html);
    webConnection.setResponse(scriptUrl, "alert('foo');\n", MimeType.APPLICATION_JAVASCRIPT);
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    client.getPage(URL_FIRST);
    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 32 with CollectingAlertHandler

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

the class HTMLDocument2Test method cookie_read.

/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({ "one", "two", "three", "four" })
public void cookie_read() throws Exception {
    final WebClient webClient = getWebClientWithMockWebConnection();
    final String html = "<html><head><title>First</title><script>\n" + "function doTest() {\n" + "  var cookieSet = document.cookie.split('; ');\n" + "  var setSize = cookieSet.length;\n" + "  var crumbs;\n" + "  for (var x = 0; x < setSize; x++) {\n" + "    crumbs = cookieSet[x].split('=');\n" + "    alert(crumbs[0]);\n" + "    alert(crumbs[1]);\n" + "  }\n" + "}\n" + "</script></head><body onload='doTest()'>\n" + "</body></html>";
    final URL url = URL_FIRST;
    getMockWebConnection().setResponse(url, html);
    final CookieManager mgr = webClient.getCookieManager();
    mgr.addCookie(new Cookie(url.getHost(), "one", "two", "/", null, false));
    mgr.addCookie(new Cookie(url.getHost(), "three", "four", "/", null, false));
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final HtmlPage firstPage = webClient.getPage(url);
    assertEquals("First", firstPage.getTitleText());
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
Also used : Cookie(com.gargoylesoftware.htmlunit.util.Cookie) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) CookieManager(com.gargoylesoftware.htmlunit.CookieManager) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 33 with CollectingAlertHandler

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

the class JavaScriptEngineTest method javaScriptEngineCallsForVariableAccess.

/**
 * Test that the JavaScript engine gets called correctly for variable access.
 * @throws Exception if the test fails
 */
@Test
public void javaScriptEngineCallsForVariableAccess() throws Exception {
    final WebClient client = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final String content = "<html><head><title>foo</title><script>\n" + "myDate = 'foo';\n" + "function doUnqualifiedVariableAccess() {\n" + "  alert('unqualified: ' + myDate);\n" + "}\n" + "function doQualifiedVariableAccess() {\n" + "  alert('qualified: ' + window.myDate);\n" + "}\n" + "</script></head><body>\n" + "<p>hello world</p>\n" + "<a id='unqualified' onclick='doUnqualifiedVariableAccess();'>unqualified</a>\n" + "<a id='qualified' onclick='doQualifiedVariableAccess();'>qualified</a>\n" + "</body></html>";
    webConnection.setDefaultResponse(content);
    client.setWebConnection(webConnection);
    final CountingJavaScriptEngine countingJavaScriptEngine = new CountingJavaScriptEngine(client);
    client.setJavaScriptEngine(countingJavaScriptEngine);
    final HtmlPage page = client.getPage(URL_FIRST);
    assertEquals(1, countingJavaScriptEngine.getExecutionCount());
    assertEquals(0, countingJavaScriptEngine.getCallCount());
    page.getHtmlElementById("unqualified").click();
    assertEquals(1, countingJavaScriptEngine.getExecutionCount());
    assertEquals(1, countingJavaScriptEngine.getCallCount());
    page.getHtmlElementById("qualified").click();
    assertEquals(1, countingJavaScriptEngine.getExecutionCount());
    assertEquals(2, countingJavaScriptEngine.getCallCount());
    final String[] expectedAlerts = { "unqualified: foo", "qualified: foo" };
    assertEquals(expectedAlerts, 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 34 with CollectingAlertHandler

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

the class JavaScriptEngineTest method scopeInInactivePage.

/**
 * If a reference has been hold on a page and the page is not
 * anymore the one contained in "its" window, JavaScript should not be executed.
 * (see {@link com.gargoylesoftware.htmlunit.javascript.host.WindowConcurrencyTest#cleanSetTimeout}).
 * @throws Exception if the test fails
 */
@Test
public void scopeInInactivePage() throws Exception {
    final String firstContent = "<html><head>\n" + "<script>\n" + "var foo = 'foo';\n" + "</script>\n" + "</head>\n" + "<body>\n" + "  <a href='page2.html'>to page 2</a>\n" + "  <div id='testdiv' onclick='alert(foo)'>foo</div>\n" + "</body>\n" + "</html>";
    final WebClient client = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setDefaultResponse("<html></html>");
    webConnection.setResponse(URL_FIRST, firstContent);
    client.setWebConnection(webConnection);
    final String[] expectedAlerts = { "foo" };
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlElement div = page.getHtmlElementById("testdiv");
    div.click();
    assertEquals(expectedAlerts, collectedAlerts);
    collectedAlerts.clear();
    page.getAnchors().get(0).click();
    // ignore response, and click in the page again that is not "active" anymore
    div.click();
    assertEquals(Collections.emptyList(), collectedAlerts);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 35 with CollectingAlertHandler

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

the class JavaScriptEngineTest method externalScriptWithNewLineBeforeClosingScriptTag.

/**
 * Regression test for https://sourceforge.net/tracker/?func=detail&atid=448266&aid=1552746&group_id=47038.
 * @throws Exception if the test fails
 */
@Test
public void externalScriptWithNewLineBeforeClosingScriptTag() throws Exception {
    final WebClient client = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final String htmlContent = "<html><head><title>foo</title>\n" + "</head><body>\n" + // \n between opening and closing tag is important
    "<script src='test.js'>\n</script>\n" + "</body></html>";
    final String jsContent = "function externalMethod() {\n" + "    alert('Got to external method');\n" + "}\n" + "externalMethod();\n";
    webConnection.setResponse(URL_FIRST, htmlContent);
    webConnection.setDefaultResponse(jsContent, 200, "OK", "text/javascript");
    client.setWebConnection(webConnection);
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    client.getPage(URL_FIRST);
    assertEquals(new String[] { "Got to external method" }, 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)

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