use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method prompt_noPromptHandler.
/**
* @throws Exception if the test fails
*/
@Test
public void prompt_noPromptHandler() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
final List<String> collectedPrompts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final String firstContent = "<html><head><title>First</title><script>function doTest() {alert(prompt('foo'))}</script>\n" + "</head><body onload='doTest()'></body></html>";
webConnection.setResponse(URL_FIRST, firstContent);
webClient.setWebConnection(webConnection);
final HtmlPage firstPage = webClient.getPage(URL_FIRST);
assertEquals("First", firstPage.getTitleText());
assertEquals(Collections.EMPTY_LIST, collectedPrompts);
assertEquals(new String[] { "null" }, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method promptWithDefault.
/**
* @throws Exception if the test fails
*/
@Test
public void promptWithDefault() throws Exception {
try (WebClient webClient = getWebClient()) {
try (MockWebConnection webConnection = new MockWebConnection()) {
final List<String> collectedAlerts = new ArrayList<>();
final List<String> collectedPrompts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
webClient.setPromptHandler((page, message, defaultValue) -> {
collectedPrompts.add(message);
collectedPrompts.add(defaultValue);
return defaultValue;
});
final String html = "<html><head><title>First</title>\n" + "<script>function doTest() {alert(prompt('foo', 'some default'))}</script>\n" + "</head><body onload='doTest()'></body></html>";
webConnection.setResponse(URL_FIRST, html);
webClient.setWebConnection(webConnection);
final HtmlPage firstPage = webClient.getPage(URL_FIRST);
assertEquals("First", firstPage.getTitleText());
assertEquals(new String[] { "foo", "some default" }, collectedPrompts);
assertEquals(new String[] { "some default" }, collectedAlerts);
}
}
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method openWindow_blocked.
/**
* Verifies that <tt>window.open</tt> behaves correctly when popups are blocked.
* @throws Exception if an error occurs
*/
@Test
public void openWindow_blocked() throws Exception {
final String html = "<html>\n" + "<head>\n" + "<script>\n" + " var w;\n" + " function test() {\n" + " w = window.open('', 'foo');\n" + " }\n" + "</script>\n" + "</head>\n" + "<body onload='test()'>\n" + "<div id='d' onclick='alert(w)'>test</div>\n" + "</body></html>";
final List<String> actual = new ArrayList<>();
final WebClient client = getWebClient();
client.getOptions().setPopupBlockerEnabled(true);
client.setAlertHandler(new CollectingAlertHandler(actual));
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(html);
client.setWebConnection(webConnection);
final HtmlPage page = client.getPage("http://foo");
page.getHtmlElementById("d").click();
final String[] expected = { "null" };
assertEquals(expected, actual);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method parentAndTop.
/**
* @throws Exception if the test fails
*/
@Test
public void parentAndTop() throws Exception {
final String firstContent = "<html><head><title>First</title></head><body>\n" + " <iframe name='left' src='" + URL_SECOND + "'></iframe>\n" + "</body></html>";
final String secondContent = "<html><head><title>Second</title></head><body>\n" + " <iframe name='innermost' src='" + URL_THIRD + "'></iframe>\n" + "</body></html>";
final String thirdContent = "<html><head><title>Third</title><script>\n" + "function doAlert() {\n" + " alert(parent != this);\n" + " alert(top != this);\n" + " alert(parent != top);\n" + " alert(parent.parent == top);\n" + " alert(parent.frames[0] == this);\n" + " alert(top.frames[0] == parent);\n" + "}\n" + "</script></head>\n" + "<body><a id='clickme' onClick='doAlert()'>foo</a></body></html>";
final WebClient webClient = getWebClient();
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponse(URL_SECOND, secondContent);
webConnection.setResponse(URL_THIRD, thirdContent);
webClient.setWebConnection(webConnection);
final HtmlPage firstPage = webClient.getPage(URL_FIRST);
assertEquals("First", firstPage.getTitleText());
final WebWindow innermostWebWindow = webClient.getWebWindowByName("innermost");
final HtmlPage innermostPage = (HtmlPage) innermostWebWindow.getEnclosedPage();
innermostPage.getHtmlElementById("clickme").click();
assertNotSame(innermostWebWindow.getParentWindow(), innermostWebWindow);
assertNotSame(innermostWebWindow.getTopWindow(), innermostWebWindow);
assertNotSame(innermostWebWindow.getParentWindow(), innermostWebWindow.getTopWindow());
assertSame(innermostWebWindow.getParentWindow().getParentWindow(), innermostWebWindow.getTopWindow());
assertEquals(new String[] { "true", "true", "true", "true", "true", "true" }, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HTMLObjectElementTest method activeXInteraction.
/**
* Simple hack to proof, that a test driver can manipulate
* a activeX mock at runtime.
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {}, IE = { "Javascript called this method!", "ActiveX is still alive" })
public void activeXInteraction() throws Exception {
final String clsid = "clsid:TESTING-CLASS-ID";
final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n" + "<script>\n" + " function test() {\n" + " var obj = document.all.id1;\n" + " if (obj.GetMessage) {\n" + " alert(obj.GetMessage());\n" + " }\n" + " }\n" + "</script>\n" + "</head>\n" + "<body>\n" + " <object id='id1' classid='" + clsid + "'></object>\n" + " <button id='myButton' onClick='test()'>Click Me</button>\n" + "</body></html>";
final WebClient client = getWebClientWithMockWebConnection();
final Map<String, String> activeXObjectMap = new HashMap<>();
activeXObjectMap.put(clsid, "com.gargoylesoftware.htmlunit.javascript.MockActiveXObject");
client.setActiveXObjectMap(activeXObjectMap);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = loadPage(html);
page.getHtmlElementById("myButton").click();
final HtmlElement elem = page.getHtmlElementById("id1");
final HTMLObjectElement jsElem = (HTMLObjectElement) elem.getScriptableObject();
final MockActiveXObject activeX = (MockActiveXObject) jsElem.unwrap();
if (null != activeX) {
activeX.setMessage("ActiveX is still alive");
page.getHtmlElementById("myButton").click();
}
assertEquals(getExpectedAlerts(), collectedAlerts);
}
Aggregations