简体   繁体   English

获取JSObject或JSContext来运行小程序

[英]Get a JSObject or JSContext to run an applet

How to get a JSObject or JSContext to run an applet from Java? 如何从Java获取JSObject或JSContext来运行applet?

I'm trying to automate some procedure that consists in some link clicking in a web and then going through an applet, so what I do is to send some HTTPRequests through Java until I get a HTML with the tag from which, through JSoup, I extract all the parameters and codebase, etc. As I want to run the applet as well, I load the applet class with a ClassLoader, I set a custom stub that can give the parameters that I extracted previously. 我正在尝试使某些过程自动化,该过程包括单击Web的某个链接,然后通过一个applet,所以我要做的是通过Java发送一些HTTPRequest,直到获得带有标签的HTML,然后通过JSoup从中获取提取所有参数和代码库等。当我也要运行applet时,我用ClassLoader加载applet类,我设置了一个自定义存根,可以提供以前提取的参数。

The thing is that this applet has some javascript interaction with the browser, so at some point it does a JSObject.getWindow(applet) to get the document and make the js calls and here is where I'm stuck. 事实是,此applet与浏览器存在一些javascript交互,因此在某个时候它执行JSObject.getWindow(applet)来获取文档并进行js调用,这就是我遇到的问题。 I understand that I have to be able to provide an AppletContext which should implement a JSContext and be able to provide this JSObject that it's the window as the browser would provide it. 我知道我必须能够提供一个AppletContext,它应该实现一个JSContext,并且能够提供该JSObject作为浏览器提供的窗口。 But is it possible to mock such a thing? 但是有可能嘲笑这种事情吗?

There is a sneaky trick, first create an interface that extends AppletContext and JSContext 有一个偷偷摸摸的把戏,首先创建一个扩展AppletContextJSContextinterface

private interface JSAwareAppletContext extends AppletContext, JSContext {
}

Then mock that somehow so you have an instance 然后以某种方式模拟它,以便您有一个实例

final JSAwareAppletContext myAppletContext = //mock

Now you can mock the live connect stuff on the JSAwareAppletContext and return if from your AppletStub . 现在,您可以在JSAwareAppletContext上模拟实时连接内容,并从AppletStub返回。

For example with Mockito: 例如,使用Mockito:

final JSAwareAppletContext appletContext = mock(JSAwareAppletContext.class);
final JSObject jsObject = mock(JSObject.class);
when(appletContext.getJSObject()).thenReturn(jsObject);
final AppletStub appletStub = mock(AppletStub.class);
when(appletStub.getAppletContext()).thenReturn(appletContext);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM