Headless testing with JavaFx and TestFx
Asked Answered
F

3

12

I have a simple JavaFx application (Java 8) that has a unit test using TestFx. However, when the test is run, the application window starts up and the mouse is moved to do whatever action is in my test. Can these tests be run in a way where the application doesn't popup and I can still use my mouse for other things as the automated build and tests are running?

Farman answered 10/12, 2014 at 14:23 Comment(1)
The latest version (4.0.1-alpha) mentions in the readme file that headless testing is possible but I can't see any detail on how to do it. Can anyone help out here? You can see the readme here: github.com/TestFX/TestFX/blob/master/README.mdComplexion
C
5

Update:

I found this blog post that provides the solution for me to this problem. As the author suggests, you need to add the following dependency to your build:

testRuntime 'org.testfx:openjfx-monocle:1.8.0_20'

Then you will need to include the following somewhere before you call registerPrimaryStage(), in my case in a method marked with @BeforeClass as 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 its useful to include System.setProperty("java.awt.headless", "true") to ensure that you're not relying on anything from the AWT (in my case I had a call to get the size of the screen that was causing problems). I also followed the blog author's advice 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're 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 before you run your tests:

$ Xvfb :99 &>/dev/null &
$ export DISPLAY=:99

If you launch your tests in the same console TestFX will use the frame buffer instead of your main display. Thus the tests will run but you won't be bothered with windows opening and the mouse pointer being moved around.

Complexion answered 10/2, 2016 at 8:17 Comment(2)
This does not work. My tests don't run in headless mode even if I remove the if.Clock
The update answer didn't work for me... but the Xvfb solution did work. Clever stuff.Phyllode
S
1

I would agree with KDK for using Monocle, since it does work as charm with Jenkins. I couldn't have reliable result from Xvfb on Jenkins. Below is the steps I took and works for me.

Prepare Monocle

You want to download Monocle from Monocle Github. It looks there is api change, so you would want to edit MonocleView.java with adding below method after download. I'm not sure what I should put in the method, but found it just works without implementing it.

@Override
protected int _getNativeFrameBuffer(long ptr) {
    // TODO Auto-generated method stub
    return 0;
}

Install Monocle

Build the Monocle jar and put the jar into your JRE (under jre/lib/ext path)

Run Monocle with Glass lib

Below is my maven command used in jenkins, you will have interest on java runtime option portion.

$ mvn clean install -Dtestfx.robot=glass -Dglass.platform=Monocle -Dmonocle.platform=Headless -Dprism.order=sw
Sachsen answered 18/2, 2016 at 17:6 Comment(1)
can you let me know the revision of Monocle? also did you put the jar under Jenkins's JRE?Sachsen
C
-1

Yes, it is possible to perform headless testing of JavaFx2 applications. You will need Monocle(part of OpenJFX). More details here: https://github.com/TestFX/Monocle

Choice answered 8/1, 2015 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.