【问题标题】:How to access client window javascript global variable with Spectron如何使用 Spectron 访问客户端窗口 javascript 全局变量
【发布时间】:2018-11-25 22:52:53
【问题描述】:

我正在尝试使用 Spectron 测试 Electron 应用程序。 但我无法测试客户端窗口 javascript 全局变量。 这是我的简化代码。 请帮我。 谢谢。

index.html

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>MY ELECTRON</title>
        <script type="text/javascript" src="script.js"></script>
    <link href="./style.css" rel="stylesheet">
</head>
<body>
</body>
</html>

script.js

let mode;
function onload_func(){
    mode = 'normal';
}
window.onload = onload_func;

spec.js

const Application = require('spectron').Application
const assert = require('assert')
const electronPath = require('electron')
const path = require('path')
let app;
describe('Application launch', function () {
  this.timeout(10000)
  beforeEach(function () {
    app = new Application({
      path: electronPath,
      args: [path.join(__dirname, '../src')]
    })
    return app.start()
  })
  afterEach(function () {
    if (app && app.isRunning()) {
      return app.stop()
    }
  })
    it('initial mode',function(){
        assert.equal(app.client.mode,'normal');
    })
})

【问题讨论】:

    标签: javascript electron spectron


    【解决方案1】:

    我不确定它是否能解决您的特定测试,但 app.browserWindow 应该可以解决问题,因为正如他们所说:

    它为您提供对当前 BrowserWindow 的访问并包含所有 API。

    注意它是require('electron').remote.getCurrentWindow()的别名

    阅读更多:https://github.com/electron/spectron#browserwindow

    【讨论】:

    • 我试过了,但它不起作用。 AssertionError [ERR_ASSERTION]: undefined == 'normal'
    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 2012-06-10
    • 2017-04-04
    • 2020-06-14
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多