use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class JavaScriptEngineTest method scopeOfNewFunctionCalledFormOtherWindow.
/**
* Checks that a dynamically compiled function works in the scope of its birth and not the other window.
* @throws Exception if the test fails
*/
@Test
public void scopeOfNewFunctionCalledFormOtherWindow() throws Exception {
final String firstContent = "<html><head>\n" + "<script>\n" + "var foo = 'foo';\n" + "var test = new Function('alert(foo);');\n" + "</script>\n" + "</head>\n" + "<body onload='test()'>\n" + " <iframe src='page2.html'/>\n" + "</body>\n" + "</html>";
final String secondContent = "<html><head><script>\n" + "var foo = 'foo2';\n" + "parent.test();\n" + "var f = parent.test;\n" + "f();\n" + "</script></head></html>";
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(secondContent);
webConnection.setResponse(URL_FIRST, firstContent);
client.setWebConnection(webConnection);
final String[] expectedAlerts = { "foo", "foo", "foo" };
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
client.getPage(URL_FIRST);
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class ActiveXObjectTest method setProperty.
/**
* @throws Exception if the test fails
*/
@Test
public void setProperty() throws Exception {
if (!getBrowserVersion().isIE()) {
return;
}
if (!isJacobInstalled()) {
return;
}
final String html = "<html><head><title>foo</title><script>\n" + " function test() {\n" + " try {\n" + " var ie = new ActiveXObject('InternetExplorer.Application');\n" + " var full = ie.FullScreen;\n" + " ie.FullScreen = true;\n" + " alert(ie.FullScreen);\n" + " ie.FullScreen = full;\n" + " } catch(e) {alert('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("True", page.getTitleText());
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFrameSetTest method onunload.
/**
* @throws Exception if the test fails
*/
@Test
@NotYetImplemented
public void onunload() throws Exception {
final String mainHtml = "<frameset onunload=\"document.location.href='3.html'\">\n" + "<frame name='f1' src='1.html'/>\n" + "</frameset>";
final String frame1 = "<html><head><title>1</title></head>\n" + "<body><button id='myButton' onclick=\"top.location.href='2.html'\"/></body>\n" + "</html>";
final String html2 = "<html><head><title>2</title></head>\n" + "<body>hello</body>\n" + "</html>";
final String html3 = "<html><head><title>3</title></head>\n" + "<body>hello</body>\n" + "</html>";
final WebClient webClient = getWebClientWithMockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection conn = getMockWebConnection();
conn.setResponse(URL_FIRST, mainHtml);
conn.setResponse(new URL(URL_FIRST, "1.html"), frame1);
conn.setResponse(new URL(URL_FIRST, "2.html"), html2);
conn.setResponse(new URL(URL_FIRST, "3.html"), html3);
final HtmlPage mainPage = webClient.getPage(URL_FIRST);
final HtmlPage framePage = (HtmlPage) mainPage.getFrameByName("f1").getEnclosedPage();
final HtmlPage page = framePage.getHtmlElementById("myButton").click();
assertEquals("3", page.getTitleText());
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HTMLDocument2Test method cookieInLocalFile.
/**
* Verifies that cookies work when working with local files (not remote sites with real domains).
* Required for local testing of Dojo 1.1.1.
* @throws Exception if an error occurs
*/
@Test
@Alerts({ "", "", "blah=bleh" })
public void cookieInLocalFile() throws Exception {
final WebClient client = getWebClient();
final List<String> actual = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(actual));
final URL url = getClass().getResource("HTMLDocumentTest_cookieInLocalFile.html");
client.getPage(url);
assertEquals(getExpectedAlerts(), actual);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HTMLElement3Test method clickHashAnchor.
/**
* @throws Exception if the test fails
*/
@Test
public void clickHashAnchor() throws Exception {
final String html = "<html><head><title>HashAnchor</title></head>\n" + "<body>\n" + " <script language='javascript'>\n" + " function test() {alert('test hash');}\n" + " </script>\n" + " <a onClick='javascript:test();' href='#' name='hash'>Click</a>\n" + "</body>\n" + "</html>";
final String[] expectedAlerts = { "test hash" };
// first use direct load
final List<String> loadCollectedAlerts = new ArrayList<>();
final HtmlPage loadPage = loadPage(html, loadCollectedAlerts);
final HtmlAnchor loadHashAnchor = loadPage.getAnchorByName("hash");
loadHashAnchor.click();
assertEquals(expectedAlerts, loadCollectedAlerts);
// finally try via the client
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, html);
webClient.setWebConnection(webConnection);
final CollectingAlertHandler clientCollectedAlertsHandler = new CollectingAlertHandler();
webClient.setAlertHandler(clientCollectedAlertsHandler);
final HtmlPage clientPage = webClient.getPage(URL_FIRST);
final HtmlAnchor clientHashAnchor = clientPage.getAnchorByName("hash");
clientHashAnchor.click();
assertEquals(expectedAlerts, clientCollectedAlertsHandler.getCollectedAlerts());
}
Aggregations