在CentOS 7上的Spectron无头testing不起作用
环境
- 运行 Vagrant 1.8.4的Mac OSX 10.11.5 运行 Cent OS 7
- 节点v6.4.0
- Npm v3.10.3
- 电子预制^ 1.2.0
- 电子包装^ 7.6.0
- Spectron v3.3.0
运行testing无头,与Xvfb
-
Xvfb :99 -screen 0 1024x768x24 +extension RANDR &
-
export DISPLAY=':99.0'
build立
- 我
git cloned
电子快速启动回购。 - 然后通过
electron-packager . MyApp --platform=linux --arch=x64 --prune
build立它electron-packager . MyApp --platform=linux --arch=x64 --prune
electron-packager . MyApp --platform=linux --arch=x64 --prune
(脚本在package.json中) - 然后运行test:
node test_app.js
产量
Running app Main window is visible: true Check text Test failed An element could not be located on the page using the given search parameters. Stopping the application
补充笔记
似乎一切正常,因为主窗口可见性testing返回true,但Spectron似乎不能像我所期望的那样查询HTML的元素。
为什么得到: An element could not be located on the page using the given search
?
另外,标题是空的。 通过删除getText
testing发现,并为标题声明失败,说“”不等于"Hello World!"
我也已经确认应用程序是通过为Mac OS构build并检查而运行的,但是我想要做的是为我的CI设置运行无头testing。
片段
index.html / test_app.js
//A simple test to verify a visible window is opened with a title var Application = require('spectron').Application var assert = require('assert') var app = new Application({ path: '/home/vagrant/electron-quick-start/MyApp-linux-x64/MyApp'}) console.log('Running app') app.start() .then(function () { return app.browserWindow.isVisible() }) .then(function (isVisible) { console.log('Main window is visible: ' + isVisible) }) .then(function () { console.log('Check text') return app.client.getText('#par') }) .then(function (text) { assert.equal(text, "Does this work?") }) .then(function () { console.log('Check the windows title') return app.client.getTitle() }) .then(function (title) { assert.equal(title, 'Hello World!') }) .then(function () { console.log('Stopping the application') return app.stop() }) .catch(function (error) { //Log any failures console.error('Test failed', error.message) console.log('Stopping the application') return app.stop() })
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Hello World!</title> </head> <body> <p id="par">Does this work?</p> </body> <script> require('./renderer.js') </script> </html>
xwd截图
-
npm start
应用程序:npm start
产量
>electron-quick-start@1.0.0 start /home/vagrant/electron-quick-start
>electron .
Xlib: extension "RANDR" missing on display ":99.0".
Xlib: extension "RANDR" missing on display ":99.0".
生成截图
-
xwd -root -silent > grab.xwd
-
convert grab.xwd grab.jpg
我改变了我的test_app.js使用箭头函数,也许我在这个过程中修正了一些微妙的错误,但现在它的工作原理!
产量
Running app...
Main window is visible: true
Checking text...
Text: Does this work?
Checking the windows title...
Title: Hello World!
Stopping the application
新的test_app.js
//A simple test to verify a visible window is opened with a title var Application = require('spectron').Application var assert = require('assert') var app = new Application({ path: '/home/vagrant/electron-quick-start/MyApp-linux-x64/MyApp'}) console.log('Running app...') app.start() .then(() => app.browserWindow.isVisible()) .then((isVisible) => console.log('Main window is visible: ', isVisible)) .then(() => { console.log('Checking text...') return app.client.getText('#par') }) .then((text) => { assert.equal(text, "Does this work?") console.log("Text: ", text) }) .then(() => { console.log('Checking the windows title...') return app.client.getTitle() }) .then((title) => { assert.equal(title, 'Hello World!') console.log("Title: ", title) }) .then(() => { console.log('Stopping the application') return app.stop() }) .catch((error) => { //Log any failures console.error('Test failed: ', error.message) console.log('Stopping the application') return app.stop() })