Skip to main content

无头 CI 系统测试(Travis CI、Jenkins)

🌐 Testing on Headless CI Systems (Travis CI, Jenkins)

由于基于 Chromium,Electron 需要显示驱动才能运行。如果 Chromium 找不到显示驱动,Electron 将无法启动——因此无论你以何种方式运行测试,它都不会执行。要在 Travis、CircleCI、Jenkins 或类似系统上测试基于 Electron 的应用,就需要进行一些配置。基本上,我们需要使用虚拟显示驱动。

🌐 Being based on Chromium, Electron requires a display driver to function. If Chromium can't find a display driver, Electron will fail to launch - and therefore not execute any of your tests, regardless of how you are running them. Testing Electron-based apps on Travis, CircleCI, Jenkins or similar systems requires therefore a little bit of configuration. In essence, we need to use a virtual display driver.

配置虚拟显示服务器

🌐 Configuring the Virtual Display Server

首先,安装 Xvfb。它是一个虚拟帧缓冲器,实现了 X11 显示服务器协议——它在内存中执行所有图形操作而不显示任何屏幕输出,这正是我们所需要的。

🌐 First, install Xvfb. It's a virtual framebuffer, implementing the X11 display server protocol - it performs all graphical operations in memory without showing any screen output, which is exactly what we need.

然后,创建一个虚拟的 Xvfb 屏幕,并导出一个名为 DISPLAY 的环境变量指向它。在 Electron 中的 Chromium 会自动查找 $DISPLAY,因此无需对你的应用进行进一步配置。这一步可以通过 Anaïs Betts 的 xvfb-maybe 自动补齐:在测试命令前加上 xvfb-maybe,如果当前系统需要,这个小工具会自动配置 Xvfb。在 Windows 或 macOS 上,它不会执行任何操作。

🌐 Then, create a virtual Xvfb screen and export an environment variable called DISPLAY that points to it. Chromium in Electron will automatically look for $DISPLAY, so no further configuration of your app is required. This step can be automated with Anaïs Betts' xvfb-maybe: Prepend your test commands with xvfb-maybe and the little tool will automatically configure Xvfb, if required by the current system. On Windows or macOS, it will do nothing.


## On Windows or macOS, this invokes electron-mocha

## On Linux, if we are in a headless environment, this will be equivalent

## to xvfb-run electron-mocha ./test/*.js

xvfb-maybe electron-mocha ./test/*.js

Travis CI

对于 Travis,请参阅其关于使用 Xvfb 的文档

🌐 For Travis, see its docs on using Xvfb.

Jenkins

对于 Jenkins,有一个 Xvfb 插件可用

🌐 For Jenkins, a Xvfb plugin is available.

CircleCI

CircleCI 非常棒,已经配置好了 Xvfb 和 $DISPLAY,所以不需要额外的配置。

🌐 CircleCI is awesome and has Xvfb and $DISPLAY already set up, so no further configuration is required.

AppVeyor

AppVeyor 运行于 Windows,开箱即支持 Selenium、Chromium、Electron 及类似工具,无需配置。

🌐 AppVeyor runs on Windows, supporting Selenium, Chromium, Electron and similar tools out of the box - no configuration is required.