com.gargoylesoftware.htmlunit.WebClient.getJavaScriptEngine()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 JavaScript  
字(11.8k)|赞(0)|评价(0)|浏览(176)

本文整理了Java中com.gargoylesoftware.htmlunit.WebClient.getJavaScriptEngine()方法的一些代码示例,展示了WebClient.getJavaScriptEngine()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebClient.getJavaScriptEngine()方法的具体详情如下:
包路径:com.gargoylesoftware.htmlunit.WebClient
类名称:WebClient
方法名:getJavaScriptEngine

WebClient.getJavaScriptEngine介绍

[英]This method is intended for testing only - use at your own risk.
[中]本方法仅供测试使用,使用风险自负。

代码示例

代码示例来源:origin: org.jvnet.hudson/htmlunit

public void execute() throws Exception {
    client.getJavaScriptEngine().getContextFactory().call(new ContextAction() {
      public Object run(final Context cx) {
        cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, startingScope);
        doSend(cx);
        return null;
      }
    });
  }
});

代码示例来源:origin: org.jenkins-ci/htmlunit

public void execute() throws Exception {
    client.getJavaScriptEngine().getContextFactory().call(new ContextAction() {
      public Object run(final Context cx) {
        cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, startingScope);
        doSend(cx);
        return null;
      }
    });
  }
});

代码示例来源:origin: jenkinsci/jenkins-test-harness

/**
 * Wait for all async JavaScript tasks associated with the supplied {@link WebClient} instance
 * to complete.
 * @param webClient The {@link WebClient} instance.
 * @param timeout The timeout in milliseconds.                  
 */
public static void waitForJSExec(WebClient webClient, long timeout) {
  webClient.getJavaScriptEngine().processPostponedActions();
  webClient.waitForBackgroundJavaScript(timeout);
}

代码示例来源:origin: net.sourceforge.htmlunit/htmlunit

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);
}

代码示例来源:origin: net.sourceforge.htmlunit/htmlunit

private void callFunction(final Function function, final Object[] args) {
    if (function == null) {
      return;
    }
    final Scriptable scope = function.getParentScope();
    final JavaScriptEngine engine
      = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();
    engine.callFunction(containingPage_, function, scope, WebSocket.this, args);
  }
}

代码示例来源:origin: HtmlUnit/htmlunit

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);
}

代码示例来源:origin: HtmlUnit/htmlunit

private void callFunction(final Function function, final Object[] args) {
    if (function == null) {
      return;
    }
    final Scriptable scope = function.getParentScope();
    final JavaScriptEngine engine
      = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();
    engine.callFunction(containingPage_, function, scope, WebSocket.this, args);
  }
}

代码示例来源:origin: net.sourceforge.htmlunit/htmlunit

/**
 * Invokes the onerror handler if one has been set.
 * @param context the context within which the onerror handler is to be invoked;
 *                if {@code null}, the current thread's context is used.
 */
private void processError(Context context) {
  if (errorHandler_ != null) {
    final Scriptable scope = errorHandler_.getParentScope();
    final JavaScriptEngine jsEngine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();
    final Object[] params = new Event[] {new ProgressEvent(this, Event.TYPE_ERROR)};
    if (LOG.isDebugEnabled()) {
      LOG.debug("Calling onerror handler");
    }
    jsEngine.callFunction(containingPage_, errorHandler_, this, scope, params);
    if (LOG.isDebugEnabled()) {
      if (context == null) {
        context = Context.getCurrentContext();
      }
      LOG.debug("onerror handler: " + context.decompileFunction(errorHandler_, 4));
      LOG.debug("Calling onerror handler done.");
    }
  }
}

代码示例来源:origin: org.jvnet.hudson/htmlunit

/**
 * Invokes the onerror handler if one has been set.
 * @param context the context within which the onerror handler is to be invoked;
 *                if <tt>null</tt>, the current thread's context is used.
 */
private void processError(Context context) {
  if (errorHandler_ != null && !getBrowserVersion().isIE()) {
    if (context == null) {
      context = Context.getCurrentContext();
    }
    final Scriptable scope = errorHandler_.getParentScope();
    final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine();
    LOG.debug("Calling onerror handler");
    jsEngine.callFunction(containingPage_, errorHandler_, context, this, scope, ArrayUtils.EMPTY_OBJECT_ARRAY);
    LOG.debug("onerror handler: " + context.decompileFunction(errorHandler_, 4));
    LOG.debug("Calling onerror handler done.");
  }
}

代码示例来源:origin: org.jenkins-ci/htmlunit

/**
 * Invokes the onerror handler if one has been set.
 * @param context the context within which the onerror handler is to be invoked;
 *                if <tt>null</tt>, the current thread's context is used.
 */
private void processError(Context context) {
  if (errorHandler_ != null && !getBrowserVersion().isIE()) {
    if (context == null) {
      context = Context.getCurrentContext();
    }
    final Scriptable scope = errorHandler_.getParentScope();
    final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine();
    LOG.debug("Calling onerror handler");
    jsEngine.callFunction(containingPage_, errorHandler_, context, this, scope, ArrayUtils.EMPTY_OBJECT_ARRAY);
    LOG.debug("onerror handler: " + context.decompileFunction(errorHandler_, 4));
    LOG.debug("Calling onerror handler done.");
  }
}

代码示例来源:origin: net.disy.htmlunit/htmlunit

/**
 * Invokes the onerror handler if one has been set.
 * @param context the context within which the onerror handler is to be invoked;
 *                if <tt>null</tt>, the current thread's context is used.
 */
private void processError(Context context) {
  if (errorHandler_ != null && !getBrowserVersion().isIE()) {
    if (context == null) {
      context = Context.getCurrentContext();
    }
    final Scriptable scope = errorHandler_.getParentScope();
    final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine();
    LOG.debug("Calling onerror handler");
    jsEngine.callFunction(containingPage_, errorHandler_, context, this, scope, ArrayUtils.EMPTY_OBJECT_ARRAY);
    LOG.debug("onerror handler: " + context.decompileFunction(errorHandler_, 4));
    LOG.debug("Calling onerror handler done.");
  }
}

代码示例来源:origin: net.sourceforge.htmlunit/htmlunit

private void scheduleImplicitClose() {
  if (!closePostponedAction_) {
    closePostponedAction_ = true;
    final HtmlPage page = (HtmlPage) getDomNodeOrDie();
    final WebWindow enclosingWindow = page.getEnclosingWindow();
    page.getWebClient().getJavaScriptEngine().addPostponedAction(new PostponedAction(page) {
      @Override
      public void execute() throws Exception {
        if (writeBuilder_.length() != 0) {
          close();
        }
        closePostponedAction_ = false;
      }
      @Override
      public boolean isStillAlive() {
        return !enclosingWindow.isClosed();
      }
    });
  }
}

代码示例来源:origin: net.sourceforge.htmlunit/htmlunit

/**
   * Performs the download and calls the callback method.
   */
  @Override
  public void run() {
    final Scriptable scope = callback_.getParentScope();
    final WebRequest request = new WebRequest(url_);
    try {
      final WebResponse webResponse = client_.loadWebResponse(request);
      final String content = webResponse.getContentAsString();
      if (LOG.isDebugEnabled()) {
        LOG.debug("Downloaded content: " + StringUtils.abbreviate(content, 512));
      }
      final Object[] args = new Object[] {content};
      final ContextFactory cf = ((JavaScriptEngine) client_.getJavaScriptEngine()).getContextFactory();
      cf.call(cx -> {
        callback_.call(cx, scope, scope, args);
        return null;
      });
    }
    catch (final IOException e) {
      LOG.error("Behavior #default#download: Cannot download " + url_, e);
    }
  }
}

代码示例来源:origin: HtmlUnit/htmlunit

private void scheduleImplicitClose() {
  if (!closePostponedAction_) {
    closePostponedAction_ = true;
    final HtmlPage page = (HtmlPage) getDomNodeOrDie();
    final WebWindow enclosingWindow = page.getEnclosingWindow();
    page.getWebClient().getJavaScriptEngine().addPostponedAction(new PostponedAction(page) {
      @Override
      public void execute() throws Exception {
        if (writeBuilder_.length() != 0) {
          close();
        }
        closePostponedAction_ = false;
      }
      @Override
      public boolean isStillAlive() {
        return !enclosingWindow.isClosed();
      }
    });
  }
}

代码示例来源:origin: org.seleniumhq.selenium/selenium-htmlunit-driver

private Object[] convertScriptArgs(HtmlPage page, final Object[] args) {
 final Scriptable scope = (Scriptable) page.getEnclosingWindow().getScriptObject();
 final Object[] parameters = new Object[args.length];
 final ContextAction action = new ContextAction() {
  @Override
  public Object run(final Context context) {
   for (int i = 0; i < args.length; i++) {
    parameters[i] = parseArgumentIntoJavascriptParameter(context, scope, args[i]);
   }
   return null;
  }
 };
 getWebClient().getJavaScriptEngine().getContextFactory().call(action);
 return parameters;
}

代码示例来源:origin: net.sourceforge.htmlunit/htmlunit

private void fire(final Event evt) {
  evt.setTarget(WebSocket.this);
  evt.setParentScope(getParentScope());
  evt.setPrototype(getPrototype(evt.getClass()));
  final JavaScriptEngine engine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();
  engine.getContextFactory().call(cx -> {
    executeEventLocally(evt);
    return null;
  });
}

代码示例来源:origin: HtmlUnit/htmlunit

private void fire(final Event evt) {
  evt.setTarget(WebSocket.this);
  evt.setParentScope(getParentScope());
  evt.setPrototype(getPrototype(evt.getClass()));
  final JavaScriptEngine engine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();
  engine.getContextFactory().call(cx -> {
    executeEventLocally(evt);
    return null;
  });
}

代码示例来源:origin: net.sourceforge.htmlunit/htmlunit

/**
 * Submits the form (at the end of the current script execution).
 */
@JsxFunction
public void submit() {
  final HtmlPage page = (HtmlPage) getDomNodeOrDie().getPage();
  final WebClient webClient = page.getWebClient();
  final String action = getHtmlForm().getActionAttribute().trim();
  if (StringUtils.startsWithIgnoreCase(action, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
    final String js = action.substring(JavaScriptURLConnection.JAVASCRIPT_PREFIX.length());
    webClient.getJavaScriptEngine().execute(page, js, "Form action", 0);
  }
  else {
    // download should be done ASAP, response will be loaded into a window later
    final WebRequest request = getHtmlForm().getWebRequest(null);
    final String target = page.getResolvedTarget(getTarget());
    final boolean forceDownload = webClient.getBrowserVersion().hasFeature(JS_FORM_SUBMIT_FORCES_DOWNLOAD);
    final boolean checkHash =
        !webClient.getBrowserVersion().hasFeature(FORM_SUBMISSION_DOWNLOWDS_ALSO_IF_ONLY_HASH_CHANGED);
    webClient.download(page.getEnclosingWindow(),
          target, request, checkHash, forceDownload, "JS form.submit()");
  }
}

代码示例来源:origin: net.sourceforge.htmlunit/htmlunit

void messagePosted(final Object message) {
  final MessageEvent event = new MessageEvent();
  event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin_, "", owningWindow_, null);
  event.setParentScope(owningWindow_);
  event.setPrototype(owningWindow_.getPrototype(event.getClass()));
  final JavaScriptEngine jsEngine =
      (JavaScriptEngine) owningWindow_.getWebWindow().getWebClient().getJavaScriptEngine();
  final ContextAction<Object> action = new ContextAction<Object>() {
    @Override
    public Object run(final Context cx) {
      return executeEvent(cx, event);
    }
  };
  final ContextFactory cf = jsEngine.getContextFactory();
  final JavaScriptJob job = new WorkerJob(cf, action, "messagePosted: " + Context.toString(message));
  final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
  owningWindow_.getWebWindow().getJobManager().addJob(job, page);
}

代码示例来源:origin: HtmlUnit/htmlunit

void messagePosted(final Object message) {
  final MessageEvent event = new MessageEvent();
  event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin_, "", owningWindow_, null);
  event.setParentScope(owningWindow_);
  event.setPrototype(owningWindow_.getPrototype(event.getClass()));
  final JavaScriptEngine jsEngine =
      (JavaScriptEngine) owningWindow_.getWebWindow().getWebClient().getJavaScriptEngine();
  final ContextAction<Object> action = new ContextAction<Object>() {
    @Override
    public Object run(final Context cx) {
      return executeEvent(cx, event);
    }
  };
  final ContextFactory cf = jsEngine.getContextFactory();
  final JavaScriptJob job = new WorkerJob(cf, action, "messagePosted: " + Context.toString(message));
  final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
  owningWindow_.getWebWindow().getJobManager().addJob(job, page);
}

相关文章

微信公众号

WebClient类方法