使用 JavaFx 和 TestFx 进行无头测试 [英] Headless testing with JavaFx and TestFx

查看:27
本文介绍了使用 JavaFx 和 TestFx 进行无头测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 JavaFx 应用程序 (Java 8),它有一个使用 TestFx 的单元测试.但是,当测试运行时,应用程序窗口会启动,并且鼠标会移动以执行我的测试中的任何操作.这些测试能否以不弹出应用程序的方式运行,并且我仍然可以在自动化构建和测试运行时将鼠标用于其他操作?

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?

推荐答案

更新:

我发现这篇 博客文章为我提供了解决方案问题.正如作者所建议的,您需要在构建中添加以下依赖项:

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'

然后,您需要在调用 registerPrimaryStage() 之前在某处包含以下内容,在我的情况下,在我使用 JUnit 时标记为 @BeforeClass 的方法中:

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");

我还要补充一点,包含 System.setProperty("java.awt.headless", "true") 以确保您不依赖 AWT 中的任何内容(在在我的情况下,我接到了一个电话,以获取导致问题的屏幕大小).我还按照博客作者的建议添加了一个开关来打开和关闭无头模式.这给出了最终的方法如下:

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

原答案:

如果您使用的是 Linux,则可以使用 xvfb.在基于 Debian 的系统上,您可以按如下方式安装 xvfb:

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

安装 xvfb 后,在运行测试之前运行以下命令:

With xvfb installed, run the following before you run your tests:

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

如果您在同一控制台中启动测试,TestFX 将使用帧缓冲区而不是主显示器.因此,测试将运行,但您不会被窗口打开和鼠标指针四处移动所困扰.

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.

这篇关于使用 JavaFx 和 TestFx 进行无头测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆