如何获取testfx以在测试中找到根的fx:id(根是自定义控件)

时间:2019-07-16 07:07:08

标签: java testing javafx testfx

我知道我加载应用程序的方式可能很奇怪,但我不明白为什么testfx套件为根窗格返回EmptyNodeQueryException

org.testfx.service.query.EmptyNodeQueryException: there is no node in the scene-graph matching the query: NodeQuery: from nodes: [MainPane[id=mainPane, styleClass=root]],
lookup by function: "org.testfx.util.NodeQueryUtils$$Lambda$245/968514068@57829d67",
lookup by selector: "#removeScriptButton"

我的某些视图组件(不是全部,大多数没有附加的fxml文档)是通过以下方法制成的:

https://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm

尽管我没有在构造函数中使用fxml加载器,但在实例化后从父类调用的'createView()'方法中拥有它

根类看起来像这样

public class MainPane extends BorderPane implements etc{

    //declarations ...

    public MainPane(String fxmlName) {
        this.fxmlName = fxmlName;
    }

    @Override
    public void createView() {

        fxmlLoader.assign(this, fxmlName); //<-----------

        this.setCenter(mainScrollPane);
        this.setTop(mainMenuBar);
    }
}

加载程序:

public class ExtendedFxmlLoader implements IFxmlLoader {
    @Override
    public void assign(Parent parent, String fileName) {

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/" + fileName));
        fxmlLoader.setRoot(parent);
        fxmlLoader.setController(parent);

        try {
            fxmlLoader.load();
            int bp =234;
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }
}

根目录的fxml文档

<fx:root fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="128.0" minWidth="128.0" prefHeight="720.0" prefWidth="1280.0" type="javafx.scene.layout.BorderPane" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <opaqueInsets>
      <Insets />
   </opaqueInsets>
</fx:root>

测试失败(用于另一个组件)

public class RemoveScriptButtonTest extends ApplicationTest{
    private Button button;
    private RemoveScriptButton removeScriptButton;

    @Before
    public void setUpApp() throws Exception {
        ApplicationTest.launch(App.class);
    }

    @Override
    public void start(Stage stage) {
        stage.show();
    }

    @Test
    public void just_click(){

        removeScriptButton = new RemoveScriptButton(new ExtendedFxmlLoader(), "removeScriptButton.fxml");
        removeScriptButton.createView();

        button = find("#removeScriptButton");
        clickOn(button);
    }

    public <T extends Node> T find(final String query) {
        /** TestFX provides many operations to retrieve elements from the loaded GUI. */
        return lookup(query).query();
    }

    @After
    public void tearDown() throws TimeoutException {
        FxToolkit.hideStage();
        release(new KeyCode[] {});
        release(new MouseButton[] {});
    }
}

RemoveScriptButton也是一个自定义控件,例如MainPane

所以我不确定这里发生了什么,我是否应该尝试将根目录更改为实现Initializable的普通控制器类,然后以标准方式加载它,例如

Parent root = FXMLLoader.load(getClass().getResource("mainPane.fxml"));

0 个答案:

没有答案