Search in sources :

Example 11 with ScriptResult

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

the class JSObject method eval.

/**
 * Evaluates a JavaScript expression.
 * The expression is a string of JavaScript source code which will be evaluated in the context given by "this".
 *
 * @param expression the JavaScript expression
 * @return result Object
 * @throws JSException in case or error
 */
public Object eval(final String expression) throws JSException {
    if (LOG.isInfoEnabled()) {
        LOG.info("JSObject eval '" + expression + "'");
    }
    final Page page = Window_.getWebWindow().getEnclosedPage();
    if (page instanceof HtmlPage) {
        final HtmlPage htmlPage = (HtmlPage) page;
        final ScriptResult result = htmlPage.executeJavaScript(expression);
        final Object jsResult = result.getJavaScriptResult();
        if (jsResult instanceof ScriptableObject) {
            return new JSObject((ScriptableObject) jsResult);
        }
        if (jsResult instanceof CharSequence) {
            return jsResult.toString();
        }
        return jsResult;
    }
    return null;
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)

Example 12 with ScriptResult

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

the class HtmlRadioButtonInput method setChecked.

/**
 * 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 = page.getEnclosingWindow().getWebClient().getCurrentWindow().getEnclosedPage();
        }
    }
    return page;
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) Page(com.gargoylesoftware.htmlunit.Page)

Example 13 with ScriptResult

use of com.gargoylesoftware.htmlunit.ScriptResult in project jenkins by jenkinsci.

the class BehaviorTest method testSelectorOrdering.

@Test
public void testSelectorOrdering() throws Exception {
    HtmlPage p = j.createWebClient().goTo("self/testSelectorOrdering");
    ScriptResult r = p.executeJavaScript("document.getElementsBySelector('DIV.a')[0].innerHTML");
    assertEquals("initial early counted! generic weevils! late", r.getJavaScriptResult().toString());
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Test(org.junit.Test)

Example 14 with ScriptResult

use of com.gargoylesoftware.htmlunit.ScriptResult in project jenkins by jenkinsci.

the class RenderOnDemandTest method testBehaviour.

/**
 * Makes sure that the behavior rules are applied to newly inserted nodes,
 * even when multiple nodes are added.
 */
@Test
public void testBehaviour() throws Exception {
    HtmlPage p = j.createWebClient().goTo("self/testBehaviour");
    p.executeJavaScript("renderOnDemand(document.getElementsBySelector('.lazy')[0])");
    WebClientUtil.waitForJSExec(p.getWebClient());
    // all AJAX calls complete before the above method returns
    ScriptResult r = p.executeJavaScript("var r=document.getElementsBySelector('DIV.a'); r[0].innerHTML+r[1].innerHTML+r[2].innerHTML");
    WebClientUtil.waitForJSExec(p.getWebClient());
    assertEquals("AlphaBravoCharlie", r.getJavaScriptResult().toString());
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Test(org.junit.Test)

Example 15 with ScriptResult

use of com.gargoylesoftware.htmlunit.ScriptResult in project jenkins by jenkinsci.

the class RenderOnDemandTest method testScript.

/*
    @Test
    public void testMemoryConsumption() throws Exception {
        j.createWebClient().goTo("self/testBehaviour"); // prime caches
        int total = 0;
        for (MemoryAssert.HistogramElement element : MemoryAssert.increasedMemory(new Callable<Void>() {
            @Override public Void call() throws Exception {
                j.createWebClient().goTo("self/testBehaviour");
                return null;
            }
        }, new Filter() {
            @Override public boolean accept(Object obj, Object referredFrom, Field reference) {
                return !obj.getClass().getName().contains("htmlunit");
            }
        })) {
            total += element.byteSize;
            System.out.println(element.className + " ×" + element.instanceCount + ": " + element.byteSize);
        }
        System.out.println("total: " + total);
    }
    */
/**
 * Makes sure that scripts get evaluated.
 */
@Test
public void testScript() throws Exception {
    HtmlPage p = j.createWebClient().goTo("self/testScript");
    assertNull(p.getElementById("loaded"));
    p.getElementById("button").click();
    WebClientUtil.waitForJSExec(p.getWebClient());
    // all AJAX calls complete before the above method returns
    assertNotNull(p.getElementById("loaded"));
    ScriptResult r = p.executeJavaScript("x");
    WebClientUtil.waitForJSExec(p.getWebClient());
    assertEquals("xxx", r.getJavaScriptResult().toString());
    r = p.executeJavaScript("y");
    WebClientUtil.waitForJSExec(p.getWebClient());
    assertEquals("yyy", r.getJavaScriptResult().toString());
// if you want to test this in the browser
/*
        System.out.println("Try http://localhost:"+localPort+"/self/testScript");
        j.interactiveBreak();
        */
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Test(org.junit.Test)

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 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 PageObject (org.jenkinsci.test.acceptance.po.PageObject)2