CentOS 7 上的 Spectron 无头测试不起作用

Spectron Headless Testing on CentOS 7 not working

环境

运行 无头测试,Xvfb

设置

输出

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

补充说明

一切似乎都在工作,因为 Main window 可见性测试 returns 正确,但 Spectron 似乎无法像我预期的那样查询 HTML 的元素。

为什么我得到:An element could not be located on the page using the given search

此外,标题为空。通过删除 getText 测试发现,并断言标题失败,说“”不等于 "Hello World!"

我还通过为 Mac OS 构建并检查它来确认应用程序 运行s,但我想做的是 运行 无头测试我的 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截图

输出

>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".

生成的截图

我将 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()
    })

对我来说,问题是服务器有一个无效的 SSL 证书,chrome 不会在无头模式下自动接受它(在非无头模式下会)。

更多信息在这里:https://groups.google.com/a/chromium.org/forum/#!topic/headless-dev/eiudRsYdc3A