How to test multiple scenes in TestFX

Viewed 2025

I wrote a unit test for ArithmeticProblem class in TestFX.

public class ArithmeticProblemTextFx extends TestFxBase {

    @Test(expected = FxRobotException.class)
    public void fxIdNotExist() {
        clickOn("#test123");
    }

    @Test
    public void allComponentsShouldHaveRightText() {
        verifyThat("#jfxButtonCheck", hasText("PRÜFEN"));
        verifyThat("#jfxButtonNextArithmeticProblem", hasText("NÄCHSTE AUFGABE"));
        verifyThat("#jfxButtonSave", hasText("SPEICHERN"));
        verifyThat("#jfxTextFieldResult", hasText(""));
    }

    @Test
    public void checkTextFieldResult() {
        JFXTextField jfxTextFieldResult = find("#jfxTextFieldResult");
        JFXButton jfxButtonCheck = find("#jfxButtonCheck");

        jfxTextFieldResult.setText("5");
        assertFalse(
                "Der Button ist darf NICHT disable sein, da eine Zahl in TextFieldResult steht.",
                jfxButtonCheck.isDisable()
        );

        jfxTextFieldResult.setText("g");
        assertTrue(
                "Der Button muss disable sein, da KEINE Zahl in TextFieldResult steht.",
                jfxButtonCheck.isDisable()
        );
    }

    @Test
    public void checkJfxButtonSaveClick() {

        clickOn("#jfxButtonSave");

        verifyThat("#labelTotalResults", hasText("0"));
        verifyThat("#labelNumberOfRightResults", hasText("0"));
        verifyThat("#labelAmountOfRightResults", hasText("(0%)"));
        verifyThat("#labelNumberOfWrongResults", hasText("0"));
        verifyThat("#labelAmountOfWrongResults", hasText("(0%)"));
        verifyThat("#labelTimePerArithmeticProblem", hasText(""));
        verifyThat("#labelMark", hasText("-"));
        verifyThat("#labelRightResult", hasText(""));
        verifyThat("#jfxTextFieldResult", hasText(""));
    }

    @Test
    public void checkJfxButtonNextArithmeticProblemClick() {

        clickOn("#jfxButtonNextArithmeticProblem");

        verifyThat("#labelRightResult", hasText(""));
        verifyThat("#jfxTextFieldResult", hasText(""));
        verifyThat("#labelTimePerArithmeticProblem", hasText(""));
    }
}

Here is the code of "TestFxBase"

public class TestFxBase extends ApplicationTest {

    @Before
    public void setUpClass() throws Exception {
        ApplicationTest.launch(Main.class);
    }

    @After
    public void afterEachTest() throws TimeoutException {
        FxToolkit.hideStage();
        release(new KeyCode[]{});
        release(new MouseButton[]{});
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.show();
    }

    public <T extends Node> T find(final String query) {
        return (T) lookup(query).queryAll().iterator().next();
    }
}

When I load the Main.class it only show the ArithmeticProblem-Scene. Here is how it looks.

enter image description here

So I can only test the ArithmeticProblem.class, because I do not know how I can load other scenes. In my menu I have got some other points like the evaluation or settings, how can I load this scenes?

1 Answers
Related