Search code examples
javajavafxtestfx

What is fx:id of JavaFX Alert OK Button?


I use TestFX framework for test my javaFX application. I test my application like that :

@Test
public void shouldClickOnDeletMarkerButton() {
    FxRobot bot = new FxRobot();
    bot.robotContext();
    bot.clickOn("#deleteMarkerButton");
    bot.clickOn("javafx.scene.control.Alert.CANCEL_BUTTON"); //This doesn't work.
}

I would like him to click on the OK button of JavaFX Alert Dialogs, but I have not found a fx:id.

What is fx:id of JavaFX Alert OK Button ?

EDIT : I solve my problem, FxRobot know "read", it is enough make this :

@Test
public void shouldClickOnDeletMarkerButtonWhenAnyMarkerAsBeenCreated() {
    bot.robotContext();
    bot.clickOn("#deleteMarkerButton");
    bot.clickOn("OK"); //Target text / Target Button / ...
}

Solution

  • <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    
    <BorderPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
       <Button text="Click me!" fx:id="button" onAction="#clickAction" BorderPane.alignment="CENTER"/>
    </BorderPane>
    

    Assigning an fx:id value to an element, as shown in the code for the Button control, creates a variable in the document's namespace, which you can refer to from elsewhere in the code.