1

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 / ...
}
KaluNight
  • 55
  • 1
  • 9

2 Answers2

3

@KaluNight, you solution is ok but works only for English localization.

Better to work with ids. Indeed Alert doesn't define fx:id for own buttons, but we can do it:

Button okButton = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
okButton.setId("my custom id");
Dmytro Maslenko
  • 2,247
  • 9
  • 16
1
<?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.

Akila
  • 1,258
  • 2
  • 16
  • 25