Harry
Harry

Reputation: 21

TestFX - Testing if a scene opens when a button's clicked (JavaFX - IntelliJ)

I'm trying to test a Java FX application within IntelliJ and I'm using TestFX however I'm unsure how to test whether a window opens when a button on the interface is clicked. I've tried making a getter to get the primary stage, then assertingTrue that this opens - however this isn't the stage that should appear on the button click anyway.

Any advice/help?

Thanks.

Upvotes: 2

Views: 2197

Answers (1)

ShaunB
ShaunB

Reputation: 1

If you just want the test whether the spawned Window is showing the this should do it (using the Hamcrest matchers):

FxAssert.verifyThat(window("My Window"), WindowMatchers.isShowing());

If you want to actually further interact with or test that window then you should try using the one of the targetWindow(...) methods of FXRobot (from which ApplicationTest and ApplicationRule derive). There are serveral overloads, but the simplest one is where you supply the title of your window:

https://testfx.github.io/TestFX/docs/javadoc/testfx-core/javadoc/org.testfx/org/testfx/api/FxRobot.html#targetWindow(java.lang.String)

This will give you an FXRobot instance for that window's scene to allow you to perform further testing.

Note: this answer refers to TestFX 4.

Upvotes: 0

Related Questions