Java Code Examples for com.gargoylesoftware.htmlunit.WebWindow#getWebClient()

The following examples show how to use com.gargoylesoftware.htmlunit.WebWindow#getWebClient() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Window.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 2
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * 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, FF})
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);
    }
}
 
Example 3
Source File: Window.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 4
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
        final Window jsDialog = (Window) dialog.getScriptableObject();
        return jsDialog;
    }
    catch (final IOException e) {
        throw Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 5
Source File: XMLHttpRequest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Sends the specified content to the server in an HTTP request and receives the response.
 * @param content the body of the message being sent with the request
 */
@JsxFunction
public void send(final Object content) {
    if (webRequest_ == null) {
        return;
    }
    prepareRequest(content);

    final Window w = getWindow();
    final WebWindow ww = w.getWebWindow();
    final WebClient client = ww.getWebClient();
    final AjaxController ajaxController = client.getAjaxController();
    final HtmlPage page = (HtmlPage) ww.getEnclosedPage();
    final boolean synchron = ajaxController.processSynchron(page, webRequest_, async_);
    if (synchron) {
        doSend(Context.getCurrentContext());
    }
    else {
        if (getBrowserVersion().hasFeature(XHR_FIRE_STATE_OPENED_AGAIN_IN_ASYNC_MODE)) {
            // quite strange but IE seems to fire state loading twice
            // in async mode (at least with HTML of the unit tests)
            setState(OPENED, Context.getCurrentContext());
        }

        // Create and start a thread in which to execute the request.
        final Scriptable startingScope = w;
        final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
        final ContextAction<Object> action = new ContextAction<Object>() {
            @Override
            public Object run(final Context cx) {
                // KEY_STARTING_SCOPE maintains a stack of scopes
                @SuppressWarnings("unchecked")
                Deque<Scriptable> stack =
                        (Deque<Scriptable>) cx.getThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE);
                if (null == stack) {
                    stack = new ArrayDeque<>();
                    cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, stack);
                }
                stack.push(startingScope);

                try {
                    doSend(cx);
                }
                finally {
                    stack.pop();
                }
                return null;
            }

            @Override
            public String toString() {
                return "XMLHttpRequest " + webRequest_.getHttpMethod() + " '" + webRequest_.getUrl() + "'";
            }
        };
        final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().
                createJavascriptXMLHttpRequestJob(cf, action);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Starting XMLHttpRequest thread for asynchronous request");
        }
        jobID_ = ww.getJobManager().addJob(job, page);
    }
}