首页 > 解决方案 > TestFX clickOn() 在组合框/选择框上的特定文本上

问题描述

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

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

真的可以实现 clickOn() 方法在组合框/下拉菜单中选择文本吗?有一个例子怎么做吗?

太感谢了!

标签: javajavafxjunittestfx

解决方案


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

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;
}

推荐阅读