【问题标题】:Spectron Headless Testing on CentOS 7 not workingCentOS 7 上的 Spectron 无头测试无法正常工作
【发布时间】: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 cloned electron-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

输出

&gt;electron-quick-start@1.0.0 start /home/vagrant/electron-quick-start

&gt;electron .

Xlib: extension "RANDR" missing on display ":99.0".

Xlib: extension "RANDR" missing on display ":99.0".

生成的屏幕截图

  • xwd -root -silent &gt; grab.xwd
  • convert grab.xwd grab.jpg

【问题讨论】:

  • 在我运行测试时尝试Xvfb :99 -screen 0 1024x768x24 +extension RANDR &amp; 摆脱Xlib: extension "RANDR" missing on display ":99.0".。它摆脱了消息,但结果相同。

标签: node.js linux testing electron


【解决方案1】:

我更改了我的 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()
    })

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      • 2018-07-15
      • 1970-01-01
      • 2012-08-09
      • 2014-12-15
      • 2018-11-08
      • 2017-01-24
      相关资源
      最近更新 更多