Getting started with TestFx
Asked Answered
C

2

5

I'm having some trouble getting TestFx working with Oracle's JavaFx HelloWorld app:

public class HelloWorld extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

TestFx junit test:

class MyTest extends GuiTest {
  public Parent getRootNode() {
    return nodeUnderTest;
  }

What should nodeUnderTest be for this example?

Corbel answered 9/12, 2014 at 19:22 Comment(0)
D
8

TestFx is a unit test framework, so it is designed to grab parts of your GUI implementation and test on that. That requires you to make these parts available first and test targets (buttons etc) available by tagging them with ids.

getRootNode() provides the root for the following test procedures of GUI testing. In your example above the StackPane root might be a candidate... but that requires that you make that available for testing to allow:

 class MyTest extends GuiTest {
     public Parent getRootNode() {
         HelloWorld app = new HelloWorld();
         return app.getRoot(); // the root StackPane with button
     }
 }

So the app has to be modified to implement the getRoot(), returning the StackPane with its content for testing, not requiring the start() method.

The you are able to run tests on that...

@Test
public void testButtonClick(){
    final Button button = find("#button"); // requires your button to be tagged with setId("button")
    click(button);
    // verify any state change expected on click.
}
Discrepancy answered 10/12, 2014 at 7:47 Comment(1)
Thanks. I moved StackPane creation, button action listeners, etc to the constructor and made the StackPane a private field for the class so it would be available for getRootNode() while leaving any code for the primaryStage in start() and it is working now.Corbel
K
4

There is also a simple way to test your entire application. To make sure that your app is properly initialised and started, it needs to be started by the JavaFX application launcher. Unfortunately, TestFX doesn't support this out of the box (at least I haven't found any way to do this) but you can easily do this by subclassing GuiTest:

public class HelloWorldGuiTest extends GuiTest {
  private static final SettableFuture<Stage> stageFuture = SettableFuture.create();

  protected static class TestHelloWorld extends HelloWorld {
    public TestHelloWorld() {
      super();
    }

    @Override
    public void start(Stage primaryStage) throws IOException {
      super.start(primaryStage);
      stageFuture.set(primaryStage);
    }
  }

  @Before
  @Override
  public void setupStage() throws Throwable {
    assumeTrue(!UserInputDetector.instance.hasDetectedUserInput());

    FXTestUtils.launchApp(TestHelloWorld.class); // You can add start parameters here
    try {
      stage = targetWindow(stageFuture.get(25, TimeUnit.SECONDS));
      FXTestUtils.bringToFront(stage);
    } catch (Exception e) {
      throw new RuntimeException("Unable to show stage", e);
    }
  }

  @Override
  protected Parent getRootNode() {
    return stage.getScene().getRoot();
  }

  // Add your tests here

}
Koehn answered 6/1, 2015 at 16:47 Comment(1)
Could you please provide additional code where you write something in a TextField and read that out again or something like that?Phototube

© 2022 - 2024 — McMap. All rights reserved.