Reputation: 1436
Environment
Running test headless, with Xvfb
Xvfb :99 -screen 0 1024x768x24 +extension RANDR &
export DISPLAY=':99.0'
Setup
git cloned
the electron-quick-start repo.electron-packager . MyApp --platform=linux --arch=x64 --prune
(Script in package.json)node test_app.js
Output
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
Additional Notes
Everything seems to be working, since Main window visibility test returns true, but Spectron doesn't seem able to query elements of the HTML as I expected.
Why do I get: An element could not be located on the page using the given search
?
Also, title is empty. Found out by removing getText
test, and assert for title fails, saying " " does not equal "Hello World!"
I've also confirmed the app runs by building for Mac OS and checking it, but what I want to do is run headless tests for my CI setup.
Snippets
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 screenshot
npm start
Output
>[email protected] start /home/vagrant/electron-quick-start
>electron .
Xlib: extension "RANDR" missing on display ":99.0".
Xlib: extension "RANDR" missing on display ":99.0".
Generated screenshot
xwd -root -silent > grab.xwd
convert grab.xwd grab.jpg
Upvotes: 3
Views: 1285
Reputation: 3708
For me the problem was the server had an invalid SSL certificate and chrome does not automatically accept it in headless mode (it does in non-headless).
More info here: https://groups.google.com/a/chromium.org/forum/#!topic/headless-dev/eiudRsYdc3A
Upvotes: 0
Reputation: 1436
I changed my test_app.js to use arrow functions, perhaps I fixed some subtle bug in the process, but now it works!
Output
Running app...
Main window is visible: true
Checking text...
Text: Does this work?
Checking the windows title...
Title: Hello World!
Stopping the application
New 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()
})
Upvotes: 1