Java Code Examples for com.gargoylesoftware.htmlunit.ScriptResult#getNewPage()

The following examples show how to use com.gargoylesoftware.htmlunit.ScriptResult#getNewPage() . 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: HtmlInput.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Executes the onchange script code for this element if this is appropriate.
 * This means that the element must have an onchange script, script must be enabled
 * and the change in the element must not have been triggered by a script.
 *
 * @param htmlElement the element that contains the onchange attribute
 * @return the page that occupies this window after this method completes (may or
 *         may not be the same as the original page)
 */
static Page executeOnChangeHandlerIfAppropriate(final HtmlElement htmlElement) {
    final SgmlPage page = htmlElement.getPage();

    final AbstractJavaScriptEngine<?> engine = htmlElement.getPage().getWebClient().getJavaScriptEngine();
    if (engine.isScriptRunning()) {
        return page;
    }
    final ScriptResult scriptResult = htmlElement.fireEvent(Event.TYPE_CHANGE);

    if (page.getWebClient().containsWebWindow(page.getEnclosingWindow())) {
        // may be itself or a newly loaded one
        return page.getEnclosingWindow().getEnclosedPage();
    }

    if (scriptResult != null) {
        // current window doesn't exist anymore
        return scriptResult.getNewPage();
    }

    return page;
}
 
Example 2
Source File: HtmlRadioButtonInput.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the {@code checked} attribute.
 *
 * @param isChecked true if this element is to be selected
 * @return the page that occupies this window after setting checked status
 * It may be the same window or it may be a freshly loaded one.
 */
@Override
public Page setChecked(final boolean isChecked) {
    Page page = getPage();

    final boolean changed = isChecked() != isChecked;
    checkedState_ = isChecked;
    if (isChecked) {
        final HtmlForm form = getEnclosingForm();
        if (form != null) {
            form.setCheckedRadioButton(this);
        }
        else if (page != null && page.isHtmlPage()) {
            setCheckedForPage((HtmlPage) page);
        }
    }

    if (changed) {
        final ScriptResult scriptResult = fireEvent(Event.TYPE_CHANGE);
        if (scriptResult != null) {
            page = scriptResult.getNewPage();
        }
    }
    return page;
}
 
Example 3
Source File: HtmlForm.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Resets this form to its initial values, returning the page contained by this form's window after the
 * reset. Note that the returned page may or may not be the same as the original page, based on JavaScript
 * event handlers, etc.
 *
 * @return the page contained by this form's window after the reset
 */
public Page reset() {
    final SgmlPage htmlPage = getPage();
    final ScriptResult scriptResult = fireEvent(Event.TYPE_RESET);
    if (ScriptResult.isFalse(scriptResult)) {
        return scriptResult.getNewPage();
    }

    for (final HtmlElement next : getHtmlElementDescendants()) {
        if (next instanceof SubmittableElement) {
            ((SubmittableElement) next).reset();
        }
    }

    return htmlPage;
}
 
Example 4
Source File: DomElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Simulates the specified mouse event, returning the page which this element's window contains after the event.
 * The returned page may or may not be the same as the original page, depending on JavaScript event handlers, etc.
 *
 * @param eventType the mouse event type to simulate
 * @param shiftKey {@code true} if SHIFT is pressed during the mouse event
 * @param ctrlKey {@code true} if CTRL is pressed during the mouse event
 * @param altKey {@code true} if ALT is pressed during the mouse event
 * @param button the button code, must be {@link MouseEvent#BUTTON_LEFT}, {@link MouseEvent#BUTTON_MIDDLE}
 *        or {@link MouseEvent#BUTTON_RIGHT}
 * @return the page which this element's window contains after the event
 */
private Page doMouseEvent(final String eventType, final boolean shiftKey, final boolean ctrlKey,
    final boolean altKey, final int button) {
    final SgmlPage page = getPage();

    final ScriptResult scriptResult;
    final Event event;
    if (MouseEvent.TYPE_CONTEXT_MENU.equals(eventType)
            && getPage().getWebClient().getBrowserVersion().hasFeature(EVENT_ONCLICK_USES_POINTEREVENT)) {
        event = new PointerEvent(this, eventType, shiftKey, ctrlKey, altKey, button);
    }
    else {
        event = new MouseEvent(this, eventType, shiftKey, ctrlKey, altKey, button);
    }
    scriptResult = fireEvent(event);

    final Page currentPage;
    if (scriptResult == null) {
        currentPage = page;
    }
    else {
        currentPage = scriptResult.getNewPage();
    }

    final boolean mouseOver = !MouseEvent.TYPE_MOUSE_OUT.equals(eventType);
    if (mouseOver_ != mouseOver) {
        mouseOver_ = mouseOver;

        final SimpleScriptable scriptable = getScriptableObject();
        scriptable.getWindow().clearComputedStyles();
    }

    return currentPage;
}
 
Example 5
Source File: DomElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Simulates double-clicking on this element, returning the page in the window that has the focus
 * after the element has been clicked. Note that the returned page may or may not be the same
 * as the original page, depending on the type of element being clicked, the presence of JavaScript
 * action listeners, etc. Note also that {@link #click(boolean, boolean, boolean)} is automatically
 * called first.
 *
 * @param shiftKey {@code true} if SHIFT is pressed during the double-click
 * @param ctrlKey {@code true} if CTRL is pressed during the double-click
 * @param altKey {@code true} if ALT is pressed during the double-click
 * @param <P> the page type
 * @return the page that occupies this element's window after the element has been double-clicked
 * @exception IOException if an IO error occurs
 */
@SuppressWarnings("unchecked")
public <P extends Page> P dblClick(final boolean shiftKey, final boolean ctrlKey, final boolean altKey)
    throws IOException {
    if (this instanceof DisabledElement && ((DisabledElement) this).isDisabled()) {
        return (P) getPage();
    }

    // call click event first
    P clickPage = click(shiftKey, ctrlKey, altKey);
    if (clickPage != getPage()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("dblClick() is ignored, as click() loaded a different page.");
        }
        return clickPage;
    }

    // call click event a second time
    clickPage = click(shiftKey, ctrlKey, altKey);
    if (clickPage != getPage()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("dblClick() is ignored, as click() loaded a different page.");
        }
        return clickPage;
    }

    final Event event;
    if (getPage().getWebClient().getBrowserVersion().hasFeature(EVENT_ONCLICK_USES_POINTEREVENT)) {
        event = new PointerEvent(this, MouseEvent.TYPE_DBL_CLICK, shiftKey, ctrlKey, altKey,
                MouseEvent.BUTTON_LEFT);
    }
    else {
        event = new MouseEvent(this, MouseEvent.TYPE_DBL_CLICK, shiftKey, ctrlKey, altKey,
                MouseEvent.BUTTON_LEFT);
    }
    final ScriptResult scriptResult = fireEvent(event);
    if (scriptResult == null) {
        return clickPage;
    }
    return (P) scriptResult.getNewPage();
}
 
Example 6
Source File: HtmlForm.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * <p>Submits this form to the server. If <tt>submitElement</tt> is {@code null}, then
 * the submission is treated as if it was triggered by JavaScript, and the <tt>onsubmit</tt>
 * handler will not be executed.</p>
 *
 * <p><b>IMPORTANT:</b> Using this method directly is not the preferred way of submitting forms.
 * Most consumers should emulate the user's actions instead, probably by using something like
 * {@link HtmlElement#click()} or {@link HtmlElement#dblClick()}.</p>
 *
 * @param submitElement the element that caused the submit to occur
 * @return a new page that reflects the results of this submission
 */
Page submit(final SubmittableElement submitElement) {
    final HtmlPage htmlPage = (HtmlPage) getPage();
    final WebClient webClient = htmlPage.getWebClient();
    if (webClient.getOptions().isJavaScriptEnabled()) {
        if (submitElement != null) {
            isPreventDefault_ = false;
            if (!areChildrenValid()) {
                return htmlPage;
            }
            final ScriptResult scriptResult = fireEvent(Event.TYPE_SUBMIT);
            if (isPreventDefault_) {
                // null means 'nothing executed'
                if (scriptResult == null) {
                    return htmlPage;
                }
                return scriptResult.getNewPage();
            }
        }

        final String action = getActionAttribute().trim();
        if (StringUtils.startsWithIgnoreCase(action, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
            return htmlPage.executeJavaScript(action, "Form action", getStartLineNumber()).getNewPage();
        }
    }
    else {
        if (StringUtils.startsWithIgnoreCase(getActionAttribute(), JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
            // The action is JavaScript but JavaScript isn't enabled.
            // Return the current page.
            return htmlPage;
        }
    }

    //html5 attribute's support
    if (submitElement != null) {
        updateHtml5Attributes(submitElement);
    }

    final WebRequest request = getWebRequest(submitElement);
    final String target = htmlPage.getResolvedTarget(getTargetAttribute());

    final WebWindow webWindow = htmlPage.getEnclosingWindow();
    /** Calling form.submit() twice forces double download. */
    final boolean checkHash =
            !webClient.getBrowserVersion().hasFeature(FORM_SUBMISSION_DOWNLOWDS_ALSO_IF_ONLY_HASH_CHANGED);
    webClient.download(webWindow, target, request, checkHash, false, "JS form.submit()");
    return htmlPage;
}