首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的TestFX示例失败

简单的TestFX示例失败
EN

Stack Overflow用户
提问于 2018-10-02 09:08:22
回答 2查看 4.7K关注 0票数 7

使用TestFX 4.0.14在Eclipse光子和fx9或fx11 (无关紧要)中工作,TestFX wiki的简单示例测试should_click_on_button()中失败

代码语言:javascript
复制
Expected: Labeled has text "clicked!"
         but: was "click me!"

当查看屏幕时,会显示窗格及其包含的按钮,但鼠标移动到其他地方:因此该按钮从未被单击,因此其文本从未更改。

你知道什么地方不对劲/怎么解决吗?

测试代码(为了方便起见都从wiki复制):

代码语言:javascript
复制
import org.junit.Test;
import org.testfx.framework.junit.ApplicationTest;

import static org.testfx.api.FxAssert.*;
import static org.testfx.matcher.control.LabeledMatchers.*;

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * Simple testfx example from testfx wiki:
 * https://github.com/TestFX/TestFX/wiki/Getting-Started
 * 
 */
public class ClickApplicationTest extends ApplicationTest {
    @Override
    public void start(Stage stage) {
        Parent sceneRoot = new ClickApplication.ClickPane();
        Scene scene = new Scene(sceneRoot, 100, 100);
        stage.setScene(scene);
        stage.show();
    }

    @Test
    public void should_contain_button() {
        // expect:
        verifyThat(".button", hasText("click me!"));
    }

    @Test
    public void should_click_on_button() {
        // when:
        clickOn(".button");

        // then:
        verifyThat(".button", hasText("clicked!"));
    }


}

应用程序代码:

代码语言:javascript
复制
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * Simple testfx example from testfx wiki:
 * https://github.com/TestFX/TestFX/wiki/Getting-Started
 * 
 */
public class ClickApplication extends Application {
    // application for acceptance tests.
    @Override public void start(Stage stage) {
        Parent sceneRoot = new ClickPane();
        Scene scene = new Scene(sceneRoot, 100, 100);
        stage.setScene(scene);
        stage.show();
    }

    // scene object for unit tests
    public static class ClickPane extends StackPane {
        public ClickPane() {
            super();
            Button button = new Button("click me!");
            button.setOnAction(actionEvent -> button.setText("clicked!"));
            getChildren().add(button);
        }
    }
}

更新:

TestFX中发现一个可能匹配的未决问题。它提到了一个可能是原因的核心fx错误 --但似乎并非如此:它在fx11中是固定的(验证了核心错误报告中的代码通过了),但是testfx问题仍然普遍存在。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-05 12:37:21

事实证明,这是一个有几个原因的问题:

  • 在最基本的是一个核心错误 -机器人不能正确移动的胜利(只有10?)HiDPI屏幕,所以并不是每个人都会经历失败
  • 核心错误是在fx11中修复的(在本机win代码中),因此现在的公共fx机器人运行良好
  • 最新版本(4.0.14)中的TestFX机器人相关代码尚未更新为fx11,回购版本为
  • 默认情况下,TestFX使用仍然失败的AWTRobot (至少在TestFX的上下文中,核心bug报告中的示例通过)
  • 要使TestFX测试在win HiDPI屏幕上可靠运行,我们需要强制使用fx机器人,方法是将系统属性testfx.robot设置为玻璃。
  • 最后一个怪癖:在测试代码中通过System.getProperties().put("testfx.robot", "glass")以编程方式设置属性时,必须在测试类启动之前完成这一操作,即在一个由@BeforeClass注释的方法中(而不是在@Before或testApp.init()中)!否则,直接或间接涉及mouseMove的第一个测试就会失败,随后的测试也会失败(这让我感到困惑,因为失败感觉是虚假的;)。

示例:

代码语言:javascript
复制
public class ClickTest extends ApplicationTest {

    @Test
    public void testClick() {
        verifyThat(".button", hasText("click me!"));
        clickOn(".button");
        verifyThat(".button", hasText("clicked!"));
    }

    /**
     * Use glass robot.
     * Note: this must happen once before the class is loaded. Otherwise,
     * the very first test run uses the awt robot
     */
    @BeforeClass
    public static void config() throws Exception {
        System.getProperties().put("testfx.robot", "glass");
    }

    @Override
    public void start(Stage stage) {
        Parent sceneRoot = new ClickPane();
        Scene scene = new Scene(sceneRoot, 100, 100);
        stage.setScene(scene);
        stage.show();
    }

    // scene object for unit tests
    public static class ClickPane extends StackPane {
        public ClickPane() {
            super();
            Button button = new Button("click me!");
            button.setOnAction(actionEvent -> button.setText("clicked!"));
            getChildren().add(button);
        }
    }

    @SuppressWarnings("unused")
    private static final Logger LOG = Logger
            .getLogger(ClickTest.class.getName());
}
票数 3
EN

Stack Overflow用户

发布于 2018-10-02 10:15:12

对我来说很管用。

无论如何,您可能会在UI更改发生之前检查它,因为它是在FX应用程序线程中完成的,而不是在正在执行测试的线程中完成的。

使用这一行

代码语言:javascript
复制
WaitForAsyncUtils.waitForFxEvents()

clickOnverifyThat调用之间。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52605298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档