DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Testing JavaFX UIs - Part 3 of ?: Node-based Robot and Fixtures (with Demo)

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

Alex Ruiz user avatar by
Alex Ruiz
·
Jan. 13, 09 · Interview
Like (0)
Save
Tweet
Share
9.29K Views

Join the DZone community and get the full member experience.

Join For Free

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

JavaFX Fixture (tool)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • The Key Assumption of Modern Work Culture
  • The Changing Face of ETL
  • Learning by Doing: An HTTP API With Rust

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: