Search code examples
javajavafxjunittestfx

TestFX clickOn() on particular text on combobox/choicebox


I‘m new to TestFX GUI-testing with fxrobot (javafx).

My current task is about clicking on a choice on a drop down menu created with combobox. I didn‘t find any tutorials mentioning this issue.

Is it really possible to implement the clickOn() method selecting a text in a combobox/drop down menu? Is there an example how to do it?

Thanks a million!


Solution

  • This is an example of a way where a user selects the given text in the given combobox.

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