Working with TestFX 4.0.14 in Eclipse photon and fx9 or fx11 (doesn't matter), the simple example test from the TestFX wiki fails in should_click_on_button()
with
Expected: Labeled has text "clicked!"
but: was "click me!"
When looking at the screen, the pane and its contained button is shown, but the mouse moves to someplace else: so the button is never clicked and consequently its text never changed.
Any idea what's wrong/how to fix?
The test code (all copied from the wiki for convenience):
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!"));
}
}
The application code:
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);
}
}
}
Update:
Found an open issue in TestFX that might match. It mentions a core fx bug that might be the reason - but doesn't seem to be: it's fixed in fx11 (verified that the code in the core bug report passes), but the testfx issue prevails ..
答案 0 :(得分:1)
对我有用。
无论如何,您可能会在UI更改发生之前检查它,因为它是在FX Application线程中完成的,而不是在执行测试的线程中完成的。
使用此行
WaitForAsyncUtils.waitForFxEvents()
在clickOn
和verifyThat
调用之间。
答案 1 :(得分:1)
事实证明是有问题的原因有几个:
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());
}