【问题标题】:Spectron and electron without exe filesSpectron 和 electron 没有 exe 文件
【发布时间】:2016-05-20 09:35:58
【问题描述】:

我正在尝试使用 Electron 构建一个应用程序。

我需要基于电子环境和使用电子包进行一些单元测试。

这样,我使用 spectron 来模拟我的应用程序。

在文档中,我必须将我的可执行文件所在的路径放入“路径”属性中。我暂时没有可执行文件,我处于开发模式。

这是我根据另一个问题尝试过的:

beforeEach(() => {
    app = new Application({
        path: 'node_modules/.bin/electron'
    });
    app.start().then(res => console.log(res), err => console.log(err));

});

提示上没有出现任何内容,并且以下测试失败,表明我无法在未定义的对象上获取 getWindowCount(显然,该应用未实例化):

 it('should call currentWindow', (done) => {
            app.client.getWindowCount().then((count) => {
                expect(count).to.equals(1);
                done();
            });
        });

有人知道我应该在这条路径中添加什么来使我的测试环境正常工作吗?

PS : 我正在使用 mocha chai 和 sinon。

感谢您的帮助

【问题讨论】:

  • return app.start...; in beforeEach.
  • 您的问题解决了吗?我对 Ember-electron 和 spectron 有类似的看法

标签: javascript electron spectron


【解决方案1】:

起初我是为了测试目的而创建一个可执行文件,但实际上并没有必要。

您可以看到 Spectron 有一个 example test 和一个 global setup

该示例传递了一个名为 args 的选项,而这正是您所缺少的。这就是我正在做的事情:

  var appPath = path.resolve(__dirname, '../'); //require the whole thing
  var electronPath = path.resolve(__dirname, '../node_modules/.bin/electron');

  beforeEach(function() {
    myApp = new Application({
      path: electronPath,
      args: [appPath], // pass args along with path
    });

   return myApp.start().then(function() {
     assert.equal(myApp.isRunning(), true);
     chaiAsPromised.transferPromiseness = myApp.transferPromiseness;
     return myApp;
   });
 });

我的测试位于 ./tests/app-test.js 中。以上对我有用。

【讨论】:

【解决方案2】:

如果您使用 doc 中提到的电子预构建,您还可以为变量路径提供“电子”:

路径 - 必需。电子应用程序可执行文件的字符串路径 发射。注意:如果您想直接使用您的应用程序调用电子 主脚本然后你应该指定路径作为电子通过 electron-prebuilt 并将您的应用程序的主脚本路径指定为第一个 args 数组中的参数。

我认为它看起来像这样:

import electron from 'electron'
import { Application } from 'spectron'

describe('application launch', function () {
  this.timeout(10000)

  beforeEach(function () {
    this.app = new Application({
      path: electron,
      args: ['app']
    })
    return this.app.start()
  })
...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 2017-09-26
    • 2018-01-26
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2020-04-02
    相关资源
    最近更新 更多