javatestingjavafxtestfx

press(KeyCode.Enter) does not work more than once TestFX?


I am using junit5 Testfx with jdk 11. I have the following test

@Test
    void should_contain_button_with_text(FxRobot robot) {
            robot.clickOn("#newCol").write("Done");
            robot.press(KeyCode.ENTER);
            robot.clickOn("#newCard").write("Cleaning");
            press(KeyCode.ENTER);
            robot.sleep(2000);
    }

Both newCol and newCard are textfields.

Expected result should be the robot first goes to the newCol and after pressing enter it should go to newCard and do the same.

However, it goes to newCol and presses enter fine but on the second press it does not work. Why is this the case. Can the press(KeyCode) only be pressed once?

If someone can please help me out I would really appreciate it.


Solution

  • as @Slaw suggested, you first have to release the key in order to use it again.

     robot.press(KeyCode.ENTER).release(KeyCode.ENTER);
    

    The above did the trick. Same goes for any other key actions.