use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method showModalDialogWithButton.
/**
* Test for the <tt>showModalDialog</tt> method.
* This tests blocking until the window gets closed.
* Can not currently be tested with WebDriver
* https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/284
*
* To fix this, we need to allow user to interact with the opened dialog before showModalDialog() returns
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = { "undefined", "result", "finished" }, CHROME = { "undefined", "not available" }, EDGE = { "undefined", "not available" }, FF = { "undefined", "not available" }, FF_ESR = { "undefined", "not available" })
@NotYetImplemented(IE)
public void showModalDialogWithButton() throws Exception {
final String html1 = "<html><head>\n" + " <script>\n" + " function test() {\n" + " alert(window.returnValue);\n" + " if (!window.showModalDialog) {alert('not available'); return; }\n" + " var res = showModalDialog('myDialog.html', null, 'dialogHeight:300px; dialogLeft:200px;');\n" + " alert(res);\n" + " alert('finished');\n" + " }\n" + " </script>\n" + "</head>\n" + "<body>\n" + " <button onclick='test()' id='openDlg'>Test</button>\n" + "</body></html>";
final String html2 = "<html><head>\n" + "</head>\n" + "<body>\n" + " <button id='closeDlg' onclick='window.returnValue = \"result\"; window.close();'></button>\n" + "</body>\n" + "</html>";
final WebClient client = getWebClient();
final List<String> actual = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(actual));
final MockWebConnection conn = new MockWebConnection();
conn.setResponse(URL_FIRST, html1);
conn.setResponse(new URL(URL_FIRST, "myDialog.html"), html2);
client.setWebConnection(conn);
final HtmlPage page = getWebClient().getPage(URL_FIRST);
final HtmlElement button = page.getHtmlElementById("openDlg");
button.click();
// TODO: <button id='closeDlg'> should be clicked
assertEquals(getExpectedAlerts(), actual);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method showModelessDialog.
/**
* Basic test for the <tt>showModelessDialog</tt> method. See bug #703.
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = "", IE = { "[object Window]", "a" })
public void showModelessDialog() throws Exception {
final String html1 = "<html><head><script>\n" + " var userName = '';\n" + " function test() {\n" + " if (window.showModelessDialog) {\n" + " var newWindow = showModelessDialog('myDialog.html', window, 'status:false');\n" + " alert(newWindow);\n" + " }\n" + " }\n" + " function update() { alert(userName); }\n" + "</script></head><body>\n" + " <input type='button' id='b' value='Test' onclick='test()'>\n" + "</body></html>";
final String html2 = "<html><head><script>\n" + "function update() {\n" + " var w = dialogArguments;\n" + " w.userName = document.getElementById('name').value;\n" + " w.update();\n" + "}\n" + "</script></head><body>\n" + " Name: <input id='name'><input value='OK' id='b' type='button' onclick='update()'>\n" + "</body></html>";
final WebClient client = getWebClient();
final List<String> actual = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(actual));
final MockWebConnection conn = new MockWebConnection();
conn.setResponse(URL_FIRST, html1);
conn.setResponse(new URL(URL_FIRST, "myDialog.html"), html2);
client.setWebConnection(conn);
final HtmlPage page = client.getPage(URL_FIRST);
final HtmlElement button = page.getHtmlElementById("b");
final HtmlPage dialogPage = button.click();
if (!dialogPage.getUrl().equals(URL_FIRST)) {
final HtmlInput input = dialogPage.getHtmlElementById("name");
input.setValueAttribute("a");
final HtmlButtonInput button2 = (HtmlButtonInput) dialogPage.getHtmlElementById("b");
button2.click();
assertEquals(getExpectedAlerts(), actual);
}
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method openWindow_blank.
/**
* _blank is a magic name. If we call open(url, '_blank') then a new
* window must be loaded.
* @throws Exception if the test fails
*/
@Test
public void openWindow_blank() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final String firstContent = "<html><head><title>First</title></head><body>\n" + " <iframe name='secondFrame' id='secondFrame' src='" + URL_SECOND + "'></iframe>\n" + "</body></html>";
final String secondContent = "<html><head><title>Second</title></head><body>\n" + " <a id='link' " + "onClick='window.open(\"" + URL_THIRD + "\", \"_blank\").focus(); '>\n" + "Click me</a>\n" + "</body></html>";
final String thirdContent = "<html><head><title>Third</title></head><body>\n" + "</body></html>";
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 firstWindow = firstPage.getEnclosingWindow();
final HtmlInlineFrame secondFrame = firstPage.getHtmlElementById("secondFrame");
final HtmlPage secondPage = (HtmlPage) secondFrame.getEnclosedPage();
assertEquals("Second", secondPage.getTitleText());
try {
assertEquals(secondFrame.getEnclosedWindow(), webClient.getWebWindowByName("secondFrame"));
// Expected path
} catch (final WebWindowNotFoundException exception) {
fail("Expected secondFrame would be found before click.");
}
final HtmlAnchor anchor = secondPage.getHtmlElementById("link");
final HtmlPage thirdPage = anchor.click();
assertEquals("Third", thirdPage.getTitleText());
final WebWindow thirdWindow = thirdPage.getEnclosingWindow();
assertNotSame(firstWindow, thirdWindow);
assertEquals("", thirdWindow.getName());
assertEquals(thirdWindow, thirdWindow.getTopWindow());
try {
assertEquals(secondFrame.getEnclosedWindow(), webClient.getWebWindowByName("secondFrame"));
// Expected path
} catch (final WebWindowNotFoundException exception) {
fail("Expected secondFrame would be found after click.");
}
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method viewportSetters.
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts({ "10", "20", "30", "40" })
public void viewportSetters() throws Exception {
final String html = "<html>\n" + "<head></head>\n" + "<body>\n" + "<script>\n" + "alert(window.innerWidth);\n" + "alert(window.innerHeight);\n" + "alert(window.outerWidth);\n" + "alert(window.outerHeight);\n" + "</script>\n" + "</body>\n" + "</html>";
final List<String> collectedAlerts = new ArrayList<>();
final WebClient client = getWebClient();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(html);
client.setWebConnection(webConnection);
final WebWindow topLevelWindow = client.getTopLevelWindows().get(0);
topLevelWindow.setInnerWidth(10);
topLevelWindow.setInnerHeight(20);
topLevelWindow.setOuterWidth(30);
topLevelWindow.setOuterHeight(40);
client.getPage(URL_FIRST);
assertEquals(getExpectedAlerts(), collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method openWindow.
/**
* @throws Exception if the test fails
*/
@Test
public void openWindow() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final String firstContent = "<html><head><title>First</title></head><body>\n" + "<form name='form1'>\n" + " <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"MyNewWindow\").focus(); " + "return false;'>Click me</a>\n" + "</form>\n" + "</body></html>";
final String secondContent = "<html><head><title>Second</title></head><body>\n" + "<script>alert(self.name)</script>\n" + "</body></html>";
final List<WebWindowEvent> events = new LinkedList<>();
webClient.addWebWindowListener(new WebWindowListener() {
@Override
public void webWindowOpened(final WebWindowEvent event) {
events.add(event);
}
@Override
public void webWindowContentChanged(final WebWindowEvent event) {
events.add(event);
}
@Override
public void webWindowClosed(final WebWindowEvent event) {
events.add(event);
}
});
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponse(URL_SECOND, secondContent);
webClient.setWebConnection(webConnection);
final HtmlPage firstPage = webClient.getPage(URL_FIRST);
assertEquals("First", firstPage.getTitleText());
final HtmlAnchor anchor = firstPage.getHtmlElementById("link");
final HtmlPage secondPage = anchor.click();
assertNotSame(firstPage, secondPage);
// Expecting contentChanged, opened, contentChanged
assertEquals(3, events.size());
final WebWindow firstWebWindow = (WebWindow) events.get(0).getSource();
final WebWindow secondWebWindow = (WebWindow) events.get(2).getSource();
assertSame(webClient.getCurrentWindow(), secondWebWindow);
assertEquals("MyNewWindow", secondWebWindow.getName());
assertEquals("First", ((HtmlPage) firstWebWindow.getEnclosedPage()).getTitleText());
assertEquals("Second", ((HtmlPage) secondWebWindow.getEnclosedPage()).getTitleText());
final WebWindowEvent changedEvent = events.get(2);
assertNull(changedEvent.getOldPage());
assertEquals("Second", ((HtmlPage) changedEvent.getNewPage()).getTitleText());
assertEquals(new String[] { "MyNewWindow" }, collectedAlerts);
}
Aggregations