Search in sources :

Example 1 with DialogWindow

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

the class WindowTest method showModalDialog.

/**
 * Basic test for the <tt>showModalDialog</tt> method. See bug #703.
 * @throws Exception if an error occurs
 */
@Test
@Alerts(DEFAULT = { "undefined", "Jane", "Smith", "sdg", "finished" }, CHROME = "not available", EDGE = "not available", FF = "not available", FF_ESR = "not available")
public void showModalDialog() throws Exception {
    final String html1 = "<html><head><script>\n" + "  function test() {\n" + "    if (!window.showModalDialog) {alert('not available'); return; }\n" + "    alert(window.returnValue);\n" + "    var o = new Object();\n" + "    o.firstName = 'Jane';\n" + "    o.lastName = 'Smith';\n" + "    var ret = showModalDialog('myDialog.html', o, 'dialogHeight:300px; dialogLeft:200px;');\n" + "    alert(ret);\n" + "    alert('finished');\n" + "  }\n" + "</script></head><body>\n" + "  <button onclick='test()' id='b'>Test</button>\n" + "</body></html>";
    final String html2 = "<html><head><script>\n" + "  var o = window.dialogArguments;\n" + "  alert(o.firstName);\n" + "  alert(o.lastName);\n" + "  window.returnValue = 'sdg';\n" + "</script></head>\n" + "<body>foo</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 (getExpectedAlerts().length > 1) {
        final DialogWindow dialog = (DialogWindow) dialogPage.getEnclosingWindow();
        dialog.close();
    }
    assertEquals(getExpectedAlerts(), actual);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) DialogWindow(com.gargoylesoftware.htmlunit.DialogWindow) URL(java.net.URL) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 2 with DialogWindow

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

the class HTMLCollectionFrames method showModelessDialog.

/**
 * Creates a modeless dialog box that displays the specified HTML document.
 * @param url the URL of the document to load and display
 * @param arguments object to be made available via <tt>window.dialogArguments</tt> in the dialog window
 * @param features string that specifies the window ornaments for the dialog window
 * @return a reference to the new window object created for the modeless dialog
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536761.aspx">MSDN Documentation</a>
 */
@JsxFunction(IE)
public Object showModelessDialog(final String url, final Object arguments, final String features) {
    final WebWindow webWindow = getWebWindow();
    final WebClient client = webWindow.getWebClient();
    try {
        final URL completeUrl = ((HtmlPage) getDomNodeOrDie()).getFullyQualifiedUrl(url);
        final DialogWindow dialog = client.openDialogWindow(completeUrl, webWindow, arguments);
        return dialog.getScriptableObject();
    } catch (final IOException e) {
        throw Context.throwAsScriptRuntimeEx(e);
    }
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) IOException(java.io.IOException) WebClient(com.gargoylesoftware.htmlunit.WebClient) DialogWindow(com.gargoylesoftware.htmlunit.DialogWindow) URL(java.net.URL) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 3 with DialogWindow

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

the class HTMLCollectionFrames method showModalDialog.

/**
 * Creates a modal dialog box that displays the specified HTML document.
 * @param url the URL of the document to load and display
 * @param arguments object to be made available via <tt>window.dialogArguments</tt> in the dialog window
 * @param features string that specifies the window ornaments for the dialog window
 * @return the value of the {@code returnValue} property as set by the modal dialog's window
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536759.aspx">MSDN Documentation</a>
 * @see <a href="https://developer.mozilla.org/en/DOM/window.showModalDialog">Mozilla Documentation</a>
 */
@JsxFunction(IE)
public Object showModalDialog(final String url, final Object arguments, final String features) {
    final WebWindow webWindow = getWebWindow();
    final WebClient client = webWindow.getWebClient();
    try {
        final URL completeUrl = ((HtmlPage) getDomNodeOrDie()).getFullyQualifiedUrl(url);
        final DialogWindow dialog = client.openDialogWindow(completeUrl, webWindow, arguments);
        // TODO: Theoretically, we shouldn't return until the dialog window has been close()'ed...
        // But we have to return so that the window can be close()'ed...
        // Maybe we can use Rhino's continuation support to save state and restart when
        // the dialog window is close()'ed? Would only work in interpreted mode, though.
        final ScriptableObject jsDialog = dialog.getScriptableObject();
        return jsDialog.get("returnValue", jsDialog);
    } catch (final IOException e) {
        throw Context.throwAsScriptRuntimeEx(e);
    }
}
Also used : ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) IOException(java.io.IOException) WebClient(com.gargoylesoftware.htmlunit.WebClient) DialogWindow(com.gargoylesoftware.htmlunit.DialogWindow) URL(java.net.URL) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

DialogWindow (com.gargoylesoftware.htmlunit.DialogWindow)3 WebClient (com.gargoylesoftware.htmlunit.WebClient)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 URL (java.net.URL)3 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)2 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)2 IOException (java.io.IOException)2 CollectingAlertHandler (com.gargoylesoftware.htmlunit.CollectingAlertHandler)1 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 Alerts (com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)1 ArrayList (java.util.ArrayList)1 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)1 Test (org.junit.Test)1