我有一个有按钮的场景(scene1) 当我点击按钮时,场景变为scene2 scene2也有一个按钮。当我点击它时,场景变为场景1 如何使用JemmyFX或TestFX在JavaFX2中测试此行为?
答案 0 :(得分:1)
这是一个非常简单的应用程序示例,其中包含由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:设置jemmyfx在此处描述:JemmyFx jar location
NB2:这里没有对场景变化进行具体验证,我们假设找到不同文字的按钮就足够了验证