【问题标题】:Can Spectron, mocha, and chai assert that variables have expected values in an Electron-app?Spectron、mocha 和 chai 可以断言变量在电子应用程序中具有预期值吗?
【发布时间】:2018-03-01 02:08:45
【问题描述】:

我们如何断言嵌入在 HTML 中的 javascript 变量在使用电子构建的应用程序中具有某些预期值?目前的测试框架有spectron、mocha、chai、chai.should()和chai.use(chaiAsPromised)。

我想断言全局变量foo 的值是'foo'。当我尝试foo.should.equal('foo') 我得到ReferenceError: foo is not defined at Context.<anonymous> (test\spec.js:63:28)

以下是重新设计的 spec.js。

const Application = require('spectron').Application
const assert = require('assert')
const electronPath = require('electron') // Require Electron from the binaries included in node_modules.
const path = require('path')
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const should = require('chai').should();

describe('Isolated testbeds house independent suites of tests.', function() {
  this.timeout(30000);

  before(function() {
    this.app = new Application({
      path: electronPath,

      // directory structure:

      //  |__ myProject
      //     |__ ...
      //     |__ main.js
      //     |__ package.json
      //     |__ index.html
      //     |__ ...
      //     |__ test
      //        |__ spec.js  <- You are here! ~ Well you should be.

      args: [path.join(__dirname, '..')]
    })
    return this.app.start()
  });

  after(function() {
    if (this.app && this.app.isRunning()) {
      return this.app.stop()
    }
  });

/* BELOW IS THE TEST IN QUESTION */
  it('should have a given value', function() {
    return Promise.resolve(foo).should.eventually.equal('foo'); // HERE IS THE LINE IN QUESTION
  });

})

【问题讨论】:

    标签: testing electron spectron


    【解决方案1】:

    Spectron 正在“远程控制”您的 Electron 应用程序,并且不在同一个命名空间中。这就是为什么 foo 没有在您的测试脚本中定义的原因。

    如果 foo 在您的 Electron 前端,您可以使用 this.app.client 访问它(如果它在 DOM 中)。 this.app.browserWindowthis.app.webContents 或许能够访问全局变量?

    (我知道executeJavaScript() 不起作用 - 任何返回承诺的函数基本上都不起作用。)

    如果foo 在您的后端,我会在我的问题中显示一个解决方法:Can Spectron call a function in back-end directly?(但我仍在寻找一种不需要我修改代码来测试的方法)

    【讨论】:

      猜你喜欢
      • 2018-06-26
      • 1970-01-01
      • 2012-07-16
      • 2016-05-08
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多