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?
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