Search in sources :

Example 1 with ScriptResult

use of com.gargoylesoftware.htmlunit.ScriptResult in project code-coverage-api-plugin by jenkinsci.

the class ChartUtil method getChartDataById.

/**
 * Returns a chart's data by its id.
 *
 * @param pageObject
 *         which contains chart
 * @param elementId
 *         of chart
 *
 * @return data as json
 */
public static String getChartDataById(final PageObject pageObject, final String elementId) {
    if (isChartDisplayedByElementId(pageObject, elementId)) {
        Object result = pageObject.executeScript(String.format("delete(window.Array.prototype.toJSON) %n" + "return JSON.stringify(echarts.getInstanceByDom(document.getElementById(\"%s\")).getOption())", elementId));
        ScriptResult scriptResult = new ScriptResult(result);
        return scriptResult.getJavaScriptResult().toString();
    }
    return null;
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) PageObject(org.jenkinsci.test.acceptance.po.PageObject)

Example 2 with ScriptResult

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

the class HtmlPage method executeJavaScriptFunction.

private ScriptResult executeJavaScriptFunction(final Function function, final Scriptable thisObject, final Object[] args, final DomNode htmlElementScope) {
    final JavaScriptEngine engine = (JavaScriptEngine) getWebClient().getJavaScriptEngine();
    final Object result = engine.callFunction(this, function, thisObject, args, htmlElementScope);
    return new ScriptResult(result);
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) AbstractJavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 3 with ScriptResult

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

the class HtmlElement method type.

/**
 * Simulates typing the specified character while this element has focus, returning the page contained
 * by this element's window after typing. Note that it may or may not be the same as the original page,
 * depending on the JavaScript event handlers, etc. Note also that for some elements, typing <tt>'\n'</tt>
 * submits the enclosed form.
 *
 * @param c the character you wish to simulate typing
 * @param lastType is this the last character to type
 * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
 * @exception IOException if an IO error occurs
 */
private Page type(final char c, final boolean lastType) throws IOException {
    if (isDisabledElementAndDisabled()) {
        return getPage();
    }
    // make enclosing window the current one
    getPage().getWebClient().setCurrentWindow(getPage().getEnclosingWindow());
    final HtmlPage page = (HtmlPage) getPage();
    if (page.getFocusedElement() != this) {
        focus();
    }
    final boolean isShiftNeeded = KeyboardEvent.isShiftNeeded(c, shiftPressed_);
    final Event shiftDown;
    final ScriptResult shiftDownResult;
    if (isShiftNeeded) {
        shiftDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, KeyboardEvent.DOM_VK_SHIFT, true, ctrlPressed_, altPressed_);
        shiftDownResult = fireEvent(shiftDown);
    } else {
        shiftDown = null;
        shiftDownResult = null;
    }
    final Event keyDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
    final ScriptResult keyDownResult = fireEvent(keyDown);
    if (!keyDown.isAborted(keyDownResult)) {
        final Event keyPress = new KeyboardEvent(this, Event.TYPE_KEY_PRESS, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
        final ScriptResult keyPressResult = fireEvent(keyPress);
        if ((shiftDown == null || !shiftDown.isAborted(shiftDownResult)) && !keyPress.isAborted(keyPressResult)) {
            doType(c, lastType);
        }
    }
    final WebClient webClient = page.getWebClient();
    if (this instanceof HtmlTextInput || this instanceof HtmlTextArea || this instanceof HtmlTelInput || this instanceof HtmlNumberInput || this instanceof HtmlSearchInput || this instanceof HtmlPasswordInput) {
        fireEvent(new KeyboardEvent(this, Event.TYPE_INPUT, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_));
    }
    HtmlElement eventSource = this;
    if (!isAttachedToPage()) {
        final BrowserVersion browserVersion = page.getWebClient().getBrowserVersion();
        if (browserVersion.hasFeature(HTMLELEMENT_DETACH_ACTIVE_TRIGGERS_NO_KEYUP_EVENT)) {
            eventSource = null;
        } else {
            eventSource = page.getBody();
        }
    }
    if (eventSource != null) {
        final Event keyUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
        eventSource.fireEvent(keyUp);
        if (isShiftNeeded) {
            final Event shiftUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, KeyboardEvent.DOM_VK_SHIFT, false, ctrlPressed_, altPressed_);
            eventSource.fireEvent(shiftUp);
        }
    }
    final HtmlForm form = getEnclosingForm();
    if (form != null && c == '\n' && isSubmittableByEnter()) {
        final HtmlSubmitInput submit = form.getFirstByXPath(".//input[@type='submit']");
        if (submit != null) {
            return submit.click();
        }
        form.submit((SubmittableElement) this);
        webClient.getJavaScriptEngine().processPostponedActions();
    }
    return webClient.getCurrentWindow().getEnclosedPage();
}
Also used : KeyboardEvent(com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent) WebClient(com.gargoylesoftware.htmlunit.WebClient) ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) KeyboardEvent(com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion)

Example 4 with ScriptResult

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

the class HtmlForm method reset.

/**
 * 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 htmlPage.getWebClient().getCurrentWindow().getEnclosedPage();
    }
    for (final HtmlElement next : getHtmlElementDescendants()) {
        if (next instanceof SubmittableElement) {
            ((SubmittableElement) next).reset();
        }
    }
    return htmlPage;
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage)

Example 5 with ScriptResult

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

the class HtmlForm method submit.

/**
 * <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
 */
public void submit(final SubmittableElement submitElement) {
    final HtmlPage htmlPage = (HtmlPage) getPage();
    final WebClient webClient = htmlPage.getWebClient();
    if (webClient.isJavaScriptEnabled()) {
        if (submitElement != null) {
            isPreventDefault_ = false;
            boolean validate = true;
            if (submitElement instanceof HtmlSubmitInput && ((HtmlSubmitInput) submitElement).getAttributeDirect("formnovalidate") != ATTRIBUTE_NOT_DEFINED) {
                validate = false;
            } else if (submitElement instanceof HtmlButton) {
                final HtmlButton htmlButton = (HtmlButton) submitElement;
                if ("submit".equalsIgnoreCase(htmlButton.getType()) && htmlButton.getAttributeDirect("formnovalidate") != ATTRIBUTE_NOT_DEFINED) {
                    validate = false;
                }
            }
            if (validate && getAttributeDirect(ATTRIBUTE_NOVALIDATE) != ATTRIBUTE_NOT_DEFINED) {
                validate = false;
            }
            if (validate && !areChildrenValid()) {
                return;
            }
            final ScriptResult scriptResult = fireEvent(Event.TYPE_SUBMIT);
            if (isPreventDefault_) {
                // null means 'nothing executed'
                if (scriptResult == null) {
                    return;
                }
                return;
            }
        }
        final String action = getActionAttribute().trim();
        if (StringUtils.startsWithIgnoreCase(action, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
            htmlPage.executeJavaScript(action, "Form action", getStartLineNumber());
            return;
        }
    } else {
        if (StringUtils.startsWithIgnoreCase(getActionAttribute(), JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
            // The action is JavaScript but JavaScript isn't enabled.
            return;
        }
    }
    // 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();
    final boolean forceDownload = webClient.getBrowserVersion().hasFeature(JS_FORM_SUBMIT_FORCES_DOWNLOAD);
    // 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, forceDownload, false, "JS form.submit()");
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) WebClient(com.gargoylesoftware.htmlunit.WebClient) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Aggregations

ScriptResult (com.gargoylesoftware.htmlunit.ScriptResult)26 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)8 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)5 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 Page (com.gargoylesoftware.htmlunit.Page)4 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)4 Test (org.junit.Test)4 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)3 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)3 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)2 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)2 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)2 AbstractJavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine)2 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)2 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)2 KeyboardEvent (com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent)2 MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)2 PointerEvent (com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent)2 IOException (java.io.IOException)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2