com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory. 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: NodeList.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Calls the {@code callback} given in parameter once for each value pair in the list, in insertion order.
 * @param callback function to execute for each element
 */
@JsxFunction({CHROME, FF, FF68, FF60})
public void forEach(final Object callback) {
    final List<DomNode> nodes = getElements();

    final WebClient client = getWindow().getWebWindow().getWebClient();
    final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();

    final ContextAction<Object> contextAction = new ContextAction<Object>() {
        @Override
        public Object run(final Context cx) {
            final Function function = (Function) callback;
            final Scriptable scope = getParentScope();
            for (int i = 0; i < nodes.size(); i++) {
                function.call(cx, scope, NodeList.this, new Object[] {
                        nodes.get(i).getScriptableObject(), i, NodeList.this});
            }
            return null;
        }
    };
    cf.call(contextAction);
}
 
Example #2
Source File: DomElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Fires the event on the element. Nothing is done if JavaScript is disabled.
 * @param event the event to fire
 * @return the execution result, or {@code null} if nothing is executed
 */
public ScriptResult fireEvent(final Event event) {
    final WebClient client = getPage().getWebClient();
    if (!client.isJavaScriptEnabled()) {
        return null;
    }

    if (!handles(event)) {
        return null;
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Firing " + event);
    }

    final EventTarget jsElt = getScriptableObject();
    final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
    final ScriptResult result = cf.callSecured(cx -> jsElt.fireEvent(event), getHtmlPageOrNull());
    if (event.isAborted(result)) {
        preventDefault();
    }
    return result;
}
 
Example #3
Source File: PopStateEvent.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new event instance.
 *
 * @param target the event target
 * @param type the event type
 * @param state the state object
 */
public PopStateEvent(final EventTarget target, final String type, final Object state) {
    super(target, type);
    if (state instanceof NativeObject && getBrowserVersion().hasFeature(JS_POP_STATE_EVENT_CLONE_STATE)) {
        final NativeObject old = (NativeObject) state;
        final NativeObject newState = new NativeObject();

        final WebClient client = getWindow().getWebWindow().getWebClient();
        final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();

        final ContextAction<Object> contextAction = new ContextAction<Object>() {
            @Override
            public Object run(final Context cx) {
                for (final Object o : ScriptableObject.getPropertyIds(old)) {
                    final String property = Context.toString(o);
                    newState.defineProperty(property, ScriptableObject.getProperty(old, property),
                            ScriptableObject.EMPTY);
                }
                return null;
            }
        };
        cf.call(contextAction);
        state_ = newState;
    }
    else {
        state_ = state;
    }
}
 
Example #4
Source File: WebClientUtils.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Attaches a visual (GUI) debugger to the specified client.
 * @param client the client to which the visual debugger is to be attached
 * @see <a href="http://www.mozilla.org/rhino/debugger.html">Mozilla Rhino Debugger Documentation</a>
 */
public static void attachVisualDebugger(final WebClient client) {
    final ScopeProvider sp = null;
    final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
    final Main main = Main.mainEmbedded(cf, sp, "HtmlUnit JavaScript Debugger");
    main.getDebugFrame().setExtendedState(Frame.MAXIMIZED_BOTH);

    final SourceProvider sourceProvider = new SourceProvider() {
        @Override
        public String getSource(final DebuggableScript script) {
            String sourceName = script.getSourceName();
            if (sourceName.endsWith("(eval)") || sourceName.endsWith("(Function)")) {
                return null; // script is result of eval call. Rhino already knows the source and we don't
            }
            if (sourceName.startsWith("script in ")) {
                sourceName = StringUtils.substringBetween(sourceName, "script in ", " from");
                for (final WebWindow ww : client.getWebWindows()) {
                    final WebResponse wr = ww.getEnclosedPage().getWebResponse();
                    if (sourceName.equals(wr.getWebRequest().getUrl().toString())) {
                        return wr.getContentAsString();
                    }
                }
            }
            return null;
        }
    };
    main.setSourceProvider(sourceProvider);
}
 
Example #5
Source File: JenkinsRule.java    From jenkins-test-harness with MIT License 3 votes vote down vote up
/**
 * Starts an interactive JavaScript debugger, and break at the next JavaScript execution.
 *
 * <p>
 * This is useful during debugging a test so that you can step execute and inspect state of JavaScript.
 * This will launch a Swing GUI, and the method returns immediately.
 *
 * <p>
 * Note that installing a debugger appears to make an execution of JavaScript substantially slower.
 *
 * <p>
 * TODO: because each script block evaluation in HtmlUnit is done in a separate Rhino context,
 * if you step over from one script block, the debugger fails to kick in on the beginning of the next script
 * block.
 * This makes it difficult to set a break point on arbitrary script block in the HTML page. We need to fix this
 * by tweaking {@link org.mozilla.javascript.tools.debugger.Dim.StackFrame#onLineChange(Context, int)}.
 */
public Dim interactiveJavaScriptDebugger() {
    Global global = new Global();
    HtmlUnitContextFactory cf = ((JavaScriptEngine)getJavaScriptEngine()).getContextFactory();
    global.init(cf);

    Dim dim = org.mozilla.javascript.tools.debugger.Main.mainEmbedded(cf, global, "Rhino debugger: " + testDescription.getDisplayName());

    // break on exceptions. this catch most of the errors
    dim.setBreakOnExceptions(true);

    return dim;
}
 
Example #6
Source File: HudsonTestCase.java    From jenkins-test-harness with MIT License 3 votes vote down vote up
/**
 * Starts an interactive JavaScript debugger, and break at the next JavaScript execution.
 *
 * <p>
 * This is useful during debugging a test so that you can step execute and inspect state of JavaScript.
 * This will launch a Swing GUI, and the method returns immediately.
 *
 * <p>
 * Note that installing a debugger appears to make an execution of JavaScript substantially slower.
 *
 * <p>
 * TODO: because each script block evaluation in HtmlUnit is done in a separate Rhino context,
 * if you step over from one script block, the debugger fails to kick in on the beginning of the next script block.
 * This makes it difficult to set a break point on arbitrary script block in the HTML page. We need to fix this
 * by tweaking {@link Dim.StackFrame#onLineChange(Context, int)}.
 */
public Dim interactiveJavaScriptDebugger() {
    Global global = new Global();
    HtmlUnitContextFactory cf = ((JavaScriptEngine)getJavaScriptEngine()).getContextFactory();
    global.init(cf);

    Dim dim = org.mozilla.javascript.tools.debugger.Main.mainEmbedded(cf, global, "Rhino debugger: " + getName());

    // break on exceptions. this catch most of the errors
    dim.setBreakOnExceptions(true);

    return dim;
}