How to get a JSObject or JSContext to run an applet from Java?
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.
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?
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
Now you can mock the live connect stuff on the JSAwareAppletContext
and return if from your AppletStub
.
For example with 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);
来源:https://stackoverflow.com/questions/19012136/get-a-jsobject-or-jscontext-to-run-an-applet