Alex is a Software Engineer working on Android development tools, especially Android Studio, at Google. His interests include Java, API design, OOP, IDEs and testing. Alex spends some of his spare time working on Open Source, blogging, writing technical articles, and speaking at international conferences. The opinions expressed here represent his own and not those of his employer. Alex is a DZone MVB and is not an employee of DZone and has posted 49 posts at DZone. You can read more from them at their website. View Full User Profile

Testing JavaFX UIs - Part 3 of ?: Node-based Robot and Fixtures (with Demo)

01.13.2009
| 6971 views |
  • submit to reddit

After I learned how to launch a compiled JavaFX UI and a little bit about JavaFX's node hierarchy, I thought I had enough information to start working a JavaFX-specific node-based Robot and fixtures (similar to the ones in FEST-Swing.)

I did little bit of progress this past weekend. Please note that I'm working with JavaFX's "desktop profile" only (at least for now.)

The following are some of the classes I added to the project:

  • JavaFXRobot: simulates a user clicking on a JavaFX node.
  • NodeFinder: knows how to find nodes in a hierarchy given some search criteria.
  • NodeMatcher: provides the search criteria to NodeFinder.
  • SwingButtonNodeMatcher: matches nodes that have a JButton attached to them. Matching can be done by type, node id, or button text.
  • TextBoxMatcher: matches nodes that have a JavaFX text box attached to them. Matching can be done by type or node id.
  • FrameFixture: knows how to look up nodes in a JavaFX UI.
  • SwingButtonFixture: simulates user input on and verifies state of a Swing button.
  • TextBoxFixture: verifies state of a JavaFX text box.

 

In a previous post, I wrote a pretty long (and hacky) functional test for Jim Weaver's calculator demo (you can find the test here.) It took more than 90 lines just to verify that the calculator's text box is updated correctly when the user clicks on a button (!)

With the new JavaFxRobot and node fixtures, the test is reduced to the following:

@Test public class CalculatorTest {
private JavaFxRobot robot;
private FrameFixture calculator;

@BeforeMethod public void setUp() {
robot = BasicJavaFxRobot.robotWithNewAwtHierarchy();
JFrame calculatorFrame = launch(Calculator.class);
calculator = new FrameFixture(robot, calculatorFrame);
}

@AfterMethod public void tearDown() {
robot.cleanUp();
}

public void shouldUpdateTextBoxWithPressedNumber() {
calculator.swingButton(withText("8")).click();
calculator.textBox().requireText("8");
calculator.swingButton(withText("6")).click();
calculator.textBox().requireText("86");
}
}
 

 

Please click the image to see the test running: (QuickTime format)

 

The node-based fixtures are very similar to the ones in FEST-Swing. The following code listing, using the calculator example, shows some of the ways to look up a node that contains an attached JButton:

calculator.swingButton();  // any node that has a JButton
calculator.swingButton("button7"); // by node id
calculator.swingButton(withText("7")); // by button text
calculator.swingButton(withId("button7").andText("7")); // by node id *and* button text

 

The FEST-JavaFX project is in its very early stages. There is a lot to learn and a lot of work to do. A good starting point would be to finish support for Swing components in JavaFX UIs, since we can apply the lessons we learned while working on FEST-Swing.

I checked-in the code into the project FEST-JavaFX, which is licensed under GPL v2 (with classpath exception,) until we figure out the best license to use (without violating any of the dependencies' licenses, etc.)

Future posts will include how to set up Maven and Eclipse projects that mix Java and JavaFX code (hopefully this week.)

Feedback is always appreciated

From http://www.jroller.com/alexRuiz

Published at DZone with permission of Alex Ruiz, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)