问题描述
如何让JSObject或JSContext从Java运行applet?
How to get a JSObject or JSContext to run an applet from Java?
我正在尝试自动化某些程序,其中包含一些链接在网页中点击然后通过一个applet,所以我做的是通过Java发送一些HTTPRequest,直到我得到一个带有标签的HTML,通过JSoup,我提取所有的参数和代码库,等等。因为我想运行applet ,我使用ClassLoader加载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.
问题是这个applet有一些javascript交互使用浏览器,所以在某些时候它会执行JSObject.getWindow(applet)来获取文档并进行js调用,这里就是我被困住的地方。我知道我必须能够提供一个应该实现JSContext的AppletContext,并且能够为这个JSObject提供它是浏览器提供它的窗口。但有可能嘲笑这样的事情吗?
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. 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. But is it possible to mock such a thing?
推荐答案
有一个偷偷摸摸的伎俩,首先创建一个接口
扩展 AppletContext
和 JSContext
There is a sneaky trick, first create an interface
that extends AppletContext
and JSContext
private interface JSAwareAppletContext extends AppletContext, JSContext {
}
然后以某种方式模拟你有一个实例
Then mock that somehow so you have an instance
final JSAwareAppletContext myAppletContext = //mock
现在你可以在 JSAwareAppletContext
上模拟实时连接内容并返回如果来自你的 AppletStub
。
Now you can mock the live connect stuff on the JSAwareAppletContext
and return if from your AppletStub
.
例如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);
这篇关于获取JSObject或JSContext来运行applet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!