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;
}
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;
}
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());
}
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());
}
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();
*/
}
Aggregations