Testing on Headless CI Systems (Travis CI, Jenkins)无头CI系统测试(Travis CI,Jenkins)
Being based on Chromium, Electron requires a display driver to function. 基于铬,Electron需要一个显示驱动器来运行。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. 如果Chromium找不到显示驱动程序,Electron将无法启动,因此无论您如何运行它们,都无法执行任何测试。Testing Electron-based apps on Travis, CircleCI, Jenkins or similar Systems requires therefore a little bit of configuration. 因此,在Travis、CircleCI、Jenkins或类似系统上测试基于Electron的应用程序需要一些配置。In essence, we need to use a virtual display driver.本质上,我们需要使用虚拟显示驱动程序。
Configuring the Virtual Display Server配置虚拟显示服务器
First, install Xvfb. 首先,安装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.它是一个虚拟帧缓冲区,实现X11显示服务器协议-它在内存中执行所有图形操作,而不显示任何屏幕输出,这正是我们所需要的。
Then, create a virtual Xvfb screen and export an environment variable called DISPLAY that points to it. 然后,创建一个虚拟Xvfb屏幕,并导出一个名为DISPLAY的指向它的环境变量。Chromium in Electron will automatically look for Electron中的Chromium将自动查找$DISPLAY
, so no further configuration of your app is required. $DISPLAY
,因此不需要进一步配置应用程序。This step can be automated with Anaïs Betts' xvfb-maybe: Prepend your test commands with 这一步可以用Anaïs Betts的xvfb实现自动化:用xvfb准备测试命令,如果当前系统需要,这个小工具将自动配置xvfb。xvfb-maybe
and the little tool will automatically configure Xvfb, if required by the current system. On Windows or macOS, it will do nothing.在Windows或macOS上,它什么都不会做。
## 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
On Travis, your 在Travis身上,你的.travis.yml
should look roughly like this:.travis.yml
应该大致如下:
addons:
apt:
packages:
- xvfb
install:
- export DISPLAY=':99.0'
- Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
Jenkins
For Jenkins, a Xvfb plugin is available.对于Jenkins,可以使用Xvfb插件。
CircleCI
CircleCI is awesome and has Xvfb and CircleCI非常棒,已经设置了Xvfb和$DISPLAY
already set up, so no further configuration is required.$DISPLAY
,因此不需要进一步配置。
AppVeyor
AppVeyor runs on Windows, supporting Selenium, Chromium, Electron and similar tools out of the box - no configuration is required.AppVeyor在Windows上运行,支持Selenium、Chromium、Electron和开箱即用的类似工具,无需配置。