怎么在TestFX 4中做到这一点?

时间:2016-12-16 16:13:00

标签: javafx testfx

我对testFx 4有疑问。 有一个GUI对象,我想将文本设置为TextField(“#searchField”)。

它在TestFx 3中的工作原理如下:

//Load FXML
@Override
protected Parent getRootNode() {
Parent parent = null;

try {
    FXMLLoader loader = new FXMLLoader(); 
    loader.setResources(ResourceBundle.getBundle(ENTITIES_FIELDS_BUNDLE_PATH, Locale.GERMANY));            
    parent = loader.load(this.getClass().getClassLoader().getResource(SHOW_ALL_LAYOUT_PATH).openStream()); 
} catch (IOException ex) {}

return parent;
}

//Set text into Field and check it's there
@Test
public void setBothnamesAndCheckEnabledSearchButton() {
    TextField searchField = find("#searchField");
    searchField.setText("new text");
    verifyThat("#searchField", hasText("new text"));     
}

所以,它有效,一切都很好。

TestFx 4中100%相同的情况:

@Override
public void start(Stage stage) throws Exception {
    try {
        FXMLLoader loader = new FXMLLoader(); 
        loader.setResources(ResourceBundle.getBundle(ENTITIES_FIELDS_BUNDLE_PATH, Locale.GERMANY));            
        AnchorPane pane  = (AnchorPane)loader.load(this.getClass().getClassLoader().getResource(SHOW_ALL_LAYOUT_PATH).openStream()); 

        Scene scene = new Scene(pane);
        stage.setScene(scene);
    } catch (IOException ex) {}
}

@Test
public void setBothnamesAndCheckEnabledSearchButton() {
    clickOn("#searchField").write("new text");
    verifyThat("#searchField", hasText("new text"));         
}

我总是变成“FxRobotException:查询”#searchField“没有返回节点。所以他没有”看到“相同的TextField ......

我错了什么?我确定这是一件非常愚蠢的事我看不见......有人可以帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

所以我终于找到了答案:我应该在覆盖stage.show()方法结束时调用start()。 testFX找不到任何不可见的组件......

相关问题