Testing Applets with FEST-Swing
FEST-Swing provides its own "applet viewer" to support GUI testing of Java applets. Testing an applet involves three steps: start the applet, the actual test and resource cleanup.
Starting the applet
There are two ways to start an applet:
1. Using an AppletLauncher to start an AppletViewer
The class AppletLauncher provides a fluent interface for launching an applet:
AppletViewer viewer = AppletLauncher.applet("org.fest.swing.applet.MyApplet").start(); // or AppletViewer viewer = AppletLauncher.applet(MyApplet.class).start(); // or AppletViewer viewer = AppletLauncher.applet(new MyApplet()).start();
In addition, we can pass parameters to the applet to start. The parameters to pass are the same that are specified in the HTML "param" tag:
AppletViewer viewer = AppletLauncher.applet(new MyApplet()) .withParameters( name("bgcolor").value("blue"), name("color").value("red"), name("pause").value("200") ) .start(); // or Map<String, String> parameters = new HashMap<String, String>(); parameters.put("bgcolor", "blue"); parameters.put("color", "red"); parameters.put("pause", "200"); AppletViewer viewer = AppletLauncher.applet(new MyApplet()).withParameters(parameters).start();
2. Starting an AppletViewer directly
AppletLauncher provides a simple and compact API to start applets in an AppletViewer. The created AppletViewer uses basic implementations of AppletStub and AppletContext though. If you need to test an applet with more advanced implementations of AppletStub or AppletContext, you can use AppletViewer directly, passing your own AppletStub (which will return your own AppletContext.)
AppletViewer viewer = new AppletLauncher(new MyApplet(), new MyAppletStub());
Testing the applet
Once we have an AppletViewer up and running the applet to test, we simply can "wrap" the AppletViewer with a FrameFixture and start testing!
The following example tests an applet using TestNG:
private AppletViewer viewer; private FrameFixture applet; @BeforeMethod public void setUp() { viewer = // get the viewer using AppletLauncher or create a new AppletViewer applet = new FrameFixture(viewer); applet.show(); } @Test public void shouldChangeLabelOnButtonClick() { applet.button("ok").click(); applet.label("text").requireText("Hello"); } @AfterMethod public void tearDown() { viewer.unloadApplet(); applet.cleanUp(); }
Cleaning up resources
As our previous example showed, in addition to call cleanUp we need to unload the applet by calling unloadApplet in the AppletViewer used in our test.
