Update:
I found this blog post that gives me a solution to this problem. As the author suggests, you need to add the following dependency to your assembly:
testRuntime 'org.testfx:openjfx-monocle:1.8.0_20'
Then you will need to include the following somewhere before calling registerPrimaryStage() , in my case, in the method marked @BeforeClass , since I am using JUnit:
System.setProperty("testfx.robot", "glass"); System.setProperty("testfx.headless", "true"); System.setProperty("prism.order", "sw"); System.setProperty("prism.text", "t2k");
I would also add that it is useful to include System.setProperty("java.awt.headless", "true") to ensure that you do not rely on anything from AWT (in my case, they called me to find out the screen size, which caused problems). I also followed the advice of the blog author to add a switch to turn headless mode on and off. This gives the final method as follows:
@BeforeClass public static void setupSpec() throws Exception { if (Boolean.getBoolean("headless")) { System.setProperty("testfx.robot", "glass"); System.setProperty("testfx.headless", "true"); System.setProperty("prism.order", "sw"); System.setProperty("prism.text", "t2k"); System.setProperty("java.awt.headless", "true"); } registerPrimaryStage(); }
You can see the solution in context here
Original answer:
If you are using Linux, you can use xvfb for this. On a Debian based system, you can install xvfb as follows:
$ sudo apt-get install xvfb
With xvfb installed, run the following tests before running the tests:
$ Xvfb :99 &>/dev/null & $ export DISPLAY=:99
If you run your tests in the same console, TestFX will use a frame buffer instead of your main display. This way the tests will run, but you will not worry about opening windows and moving the mouse pointer.