TestFx入门 [英] Getting started with TestFx

查看:393
本文介绍了TestFx入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Oracle的JavaFx HelloWorld应用程序时遇到一些麻烦:

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:

TestFx junit test:

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

nodeUnderTest 应该是什么?是为了这个例子?

What should nodeUnderTest be for this example?

推荐答案

TestFx是一个单元测试框架,因此它旨在抓取部分GUI实现和测试在那。这要求您首先提供这些部件,并通过使用ID标记它们来测试目标(按钮等)。

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()为以下测试程序提供了根目录。 GUI测试。在上面的示例中,StackPane根可能是候选者......但这要求您将其用于测试以允许:

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

所以应用程序有要修改以实现getRoot(),返回StackPane及其内容以进行测试,不需要start()方法。

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.
}

这篇关于TestFx入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆