Search code examples
javajenkinsgradletestfx

How to avoid HeadlessException when running TestFX on Jenkins?


I am writing a TestFX test case to cover a Java desktop application. One of the interactions calls showDocument() to display an html document using the browser. When running the test locally using Gradle, my test works correctly. But when I run it on Jenkins (using a MacOS agent) the test fails.

14:55:10     java.awt.HeadlessException
14:55:10        at java.desktop/java.awt.Desktop.getDesktop(Desktop.java:301)
14:55:10        at javafx.graphics/com.sun.javafx.application.HostServicesDelegate$StandaloneHostService.showDocument(HostServicesDelegate.java:133)
14:55:10        at javafx.graphics/javafx.application.HostServices.showDocument(HostServices.java:115)

The problem is caused when Desktop checks GraphicsEnvironment.isHeadless()

Why is this error happening? I read that a HeadlessException occurs when there is not a GUI to use (Display, Keyboard, Mouse) but my agent indeed has a GUI. This test works correctly on Windows agents but not on MacOS. How can I avoid this error and execute the automated test on Jenkins?


Solution

  • The solution was to change the node launch method on Jenkins. They were connected using SSH and changing it to connect to the master fixed the problem (ssh does not pass a GUI and connect it to the master directly does it).