在组合框/选择框上的特定文本上的TestFX clickOn()

时间:2019-02-12 13:29:50

标签: java javafx junit testfx

我是使用fxrobot(javafx)进行TestFX GUI测试的新手。

我当前的任务是在用组合框创建的下拉菜单上单击一个选项。我找不到任何提及此问题的教程。

真的可以实现在组合框/下拉菜单中选择文本的clickOn()方法吗?有示例如何做吗?

感谢一百万!

1 个答案:

答案 0 :(得分:1)

这是用户在给定组合框中选择给定文本的一种方式示例。

void user_selects_combo_item(String comboBoxId, String itemToSelect) {
    ComboBox<?> actualComboBox = lookupControl(comboBoxId);

    // Find and click only on arrow button. This is important for editable combo-boxes.
    for (Node child : actualComboBox.getChildrenUnmodifiable()) {
        if (child.getStyleClass().contains("arrow-button")) {
            Node arrowRegion = ((Pane) child).getChildren().get(0);
            robot.clickOn(arrowRegion);
            Thread.sleep(100); // try/catch were skipped for shorter code.
            robot.clickOn(itemToSelect);
        }
    }
    Assert.fail("Couldn't find an arrow-button.");
}

private <T extends Node> T lookupControl(String controlId) {
    T actualControl = robot.lookup(controlId).query();
    assertNotNull("Could not find a control by id = " + controlId, actualControl);

    return actualControl;
}