1

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ta Bla
  • 11
  • 2
  • *"But is it possible to mock such a thing?"* I have *considered* this for [Appleteer](http://pscode.org/appleteer/) using the `ScriptEngine` - but have never actually attempted it. I imagine it would be considerable effort to get it to the point it could load and interpret script written in the page, given we need to emulate the JS `document` object. To get it working with a vast variety of modern scripts & APIs would be all the more difficult. What is the page (URL)? What do the scripts do? – Andrew Thompson Sep 26 '13 at 08:12
  • @AndrewThompson I'd like to give you the page URL but it's an intern system that runs only in the Intranet of the company. The scripts and the applet basically do some checking on the browser, system os and antivirus, but in the end it ends up creating and adding a cookie. What I was trying to do is to mimic some of the behaviour to get the resulting cookie, that way I would be able to automate the process. – Ta Bla Sep 30 '13 at 10:05

1 Answers1

1

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);
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Amm, I'm sorry, I think I expressed myself wrongly, I said mock but I think it was not the right word to use, because what I would like to get is precisely that jsObject, maybe it was possible for it to be generated from the html source or implement it myself like the AppletStub (but no idea how this classes should be) or something else. Again I'm sorry for my bad formulation of the question. – Ta Bla Sep 25 '13 at 22:19