The power and flexibility of FEST-Swing come from the fixtures in
this package. Although you can use the {@link org.fest.swing.core.BasicRobot}
directly, it is too
low-level and requires, in our opinion, too much code. FEST fixtures can simplify creation and maintenance of functional
GUI tests by:
The following example shows how easy is to use FEST fixtures. The test verifies that an error message is displayed if the user enters her username but forgets to enter her password.
private {@link org.fest.swing.fixture.FrameFixture} window;
@BeforeMethod public void setUp() {
window = new FrameFixture(new LoginWindow());
window.show();
}
@AfterMethod public void tearDown() {
window.cleanUp();
}
@Test public void shouldCopyTextInLabelWhenClickingButton() {
window.textBox("username").enterText("some.user");
window.button("login").click();
window.optionPane().requireErrorMessage().requireMessage("Please enter your password");
}
The test uses a {@link org.fest.swing.fixture.FrameFixture}
to launch the GUI to test
(LoginWindow
) and find the GUI components in such window. This is the recommended way to use FEST. We
could also use individual fixtures to simulate user events, but it would result in more code to write and maintain:
private {@link org.fest.swing.core.BasicRobot} robot;
@BeforeMethod public void setUp() {
robot = BasicRobot.robotWithNewAwtHierarchy();
robot.showWindow(new LoginWindow());
}
@AfterMethod public void tearDown() {
robot.cleanUp();
}
@Test public void shouldCopyTextInLabelWhenClickingButton() {
new {@link org.fest.swing.fixture.JTextComponentFixture}(robot, "username").enterText("some.user");
new {@link org.fest.swing.fixture.JButtonFixture}(robot, "login").click();
new {@link org.fest.swing.fixture.JOptionPaneFixture}(robot).requireErrorMessage().requireMessage("Please enter your password");
}
Note: It is very important to clean up resources used by FEST (keyboard, mouse and opened windows) after
each test; otherwise, the FEST robot will keep control of them and can make your computer pretty much unusable. To clean
up resources call the method 'cleanUp' from {@link org.fest.swing.core.BasicRobot}
,
{@link org.fest.swing.fixture.FrameFixture}
or {@link org.fest.swing.fixture.DialogFixture}
.
Each fixture has the name of the GUI component it can control plus the word "Fixture" at the end. For example,
{@link org.fest.swing.fixture.JButtonFixture}
can simulate user events on
{@link javax.swing.JButton}
s.