【发布时间】:2016-08-17 16:48:00
【问题描述】:
环境
- Mac OSX 10.11.5 正在运行 Vagrant 1.8.4 正在运行 Cent OS 7
- 节点 v6.4.0
- Npm v3.10.3
- 电子预建 ^1.2.0
- 电子封装程序 ^7.6.0
- Spectron v3.3.0
无头运行测试,Xvfb
Xvfb :99 -screen 0 1024x768x24 +extension RANDR &export DISPLAY=':99.0'
设置
- 我有
git clonedelectron-quick-start 回购。 - 然后通过
electron-packager . MyApp --platform=linux --arch=x64 --prune(package.json 中的脚本)构建它 - 然后运行测试:
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
附加说明
似乎一切正常,因为主窗口可见性测试返回 true,但 Spectron 似乎无法像我预期的那样查询 HTML 的元素。
为什么我会得到:An element could not be located on the page using the given search?
另外,标题为空。通过删除getText测试发现,并且断言标题失败,说“”不等于"Hello World!"
我还通过为 Mac OS 构建并检查它来确认应用程序运行,但我想做的是为我的 CI 设置运行无头测试。
片段
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
输出
>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.xwdconvert grab.xwd grab.jpg
【问题讨论】:
-
在我运行测试时尝试
Xvfb :99 -screen 0 1024x768x24 +extension RANDR &摆脱Xlib: extension "RANDR" missing on display ":99.0".。它摆脱了消息,但结果相同。
标签: node.js linux testing electron