1

I have a scene (scene1) that has a button.
When I click on the button, the scene changes to scene2.
scene2 also has a button. When I click it the scene changes to scene 1.
How do I test this behavior in JavaFX2 using JemmyFX or TestFX?

gontard
  • 28,720
  • 11
  • 94
  • 117
damat-perdigannat
  • 5,780
  • 1
  • 17
  • 33

1 Answers1

2

Here is very simple example of the application with two different panes tested by JemmyFX.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.jemmy.fx.SceneDock;
import org.jemmy.fx.control.LabeledDock;
import org.jemmy.resources.StringComparePolicy;

public class TwoScenes extends Application {

    StackPane root1 = new StackPane();
    StackPane root2 = new StackPane();
    Scene scene;

    @Override
    public void start(Stage primaryStage) {
        Button btn1 = new Button("Goto Page 2");
        btn1.setOnAction((e) -> {
            scene.setRoot(root2);
        });

        root1.getChildren().add(btn1);

        Button btn2 = new Button("Return to Page 1");
        btn2.setOnAction((e) -> {
            scene.setRoot(root1);
        });

        root2.getChildren().add(btn2);

        scene = new Scene(root1, 300, 250);

        primaryStage.setTitle("Two Scenes");
        primaryStage.setScene(scene);
        primaryStage.show();

        // for simplicity of the example let's run test directly from an app
        runTest();
    }

    private void runTest() {
        // tests should be run in other thread
        new Thread(() -> {
            // find scene
            SceneDock sd = new SceneDock(); 
            // find button with specified text, and if it's found -- click it
            new LabeledDock(sd.asParent(), "Goto Page 2", StringComparePolicy.EXACT).mouse().click();
            // find button 2 and click it 
            new LabeledDock(sd.asParent(), "Return to Page 1", StringComparePolicy.EXACT).mouse().click();
            // verify we returned to root1 (by checking first button is present)
            new LabeledDock(sd.asParent(), "Goto Page 2", StringComparePolicy.EXACT)
        }).start();
    }
}

NB1: setting up jemmyfx is described here: JemmyFx jar location

NB2: there is no specific validation for scene changes here, we assume that finding button with different text is enough validation

Community
  • 1
  • 1
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • Hi @SergeyGrinev, this is Ok. However, I want to work with something like `primaryStage.setScene(scene2);` on the `setOnAction` part. Can you post an answer for this also? – damat-perdigannat Mar 13 '14 at 03:09
  • `SceneDock sd = new SceneDock();` will find currently active scene, just call it once again before looking for 2nd scene items (button2 in the above example) – Sergey Grinev Mar 13 '14 at 08:00
  • Ok, got it. Thanks. Marking this as an answer already because the scenario has been explained on your comment. – damat-perdigannat Mar 14 '14 at 02:37