使用TestFX 4.0.14在Eclipse光子和fx9或fx11 (无关紧要)中工作,TestFX wiki的简单示例测试在should_click_on_button()中失败
Expected: Labeled has text "clicked!"
but: was "click me!"当查看屏幕时,会显示窗格及其包含的按钮,但鼠标移动到其他地方:因此该按钮从未被单击,因此其文本从未更改。
你知道什么地方不对劲/怎么解决吗?
测试代码(为了方便起见都从wiki复制):
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!"));
}
}应用程序代码:
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问题仍然普遍存在。
发布于 2018-10-05 12:37:21
事实证明,这是一个有几个原因的问题:
System.getProperties().put("testfx.robot", "glass")以编程方式设置属性时,必须在测试类启动之前完成这一操作,即在一个由@BeforeClass注释的方法中(而不是在@Before或testApp.init()中)!否则,直接或间接涉及mouseMove的第一个测试就会失败,随后的测试也会失败(这让我感到困惑,因为失败感觉是虚假的;)。示例:
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());
}发布于 2018-10-02 10:15:12
对我来说很管用。
无论如何,您可能会在UI更改发生之前检查它,因为它是在FX应用程序线程中完成的,而不是在正在执行测试的线程中完成的。
使用这一行
WaitForAsyncUtils.waitForFxEvents()在clickOn和verifyThat调用之间。
https://stackoverflow.com/questions/52605298
复制相似问题