无头 CI 系统测试(Travis CI、Jenkins)
¥Testing on Headless CI Systems (Travis CI, Jenkins)
Electron 基于 Chromium,需要显示驱动程序才能运行。如果 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 executing 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.