【问题标题】:Spectron with mocha and chai does not work带有 mocha 和 chai 的 Spectron 不起作用
【发布时间】:2019-10-11 10:42:04
【问题描述】:

我正在尝试使用 Spectron 为我们的 Electron 应用程序编写测试,但我遇到了设置问题。我使用带有 chai 的经典设置。我有一个包含设置代码的文件:

const path = require("path");
const { Application } = require("spectron");

module.exports = {
  initialiseSpectron: () => {
    let electronPath = path.join(__dirname, "../../node_modules", ".bin", "electron");

    if (process.platform == "win32") {
      electronPath += ".cmd";
    }

    return new Application({
      path: electronPath,
      args: [path.join(__dirname, "../index.ts"), path.join(__dirname, "../../package.json")],
      env: {
        ELECTRON_ENABLE_LOGGING: true,
        ELECTRON_ENABLE_STACK_DUMPING: true,
        NODE_ENV: "development"
      },
      startTimeout: 10000,
      chromeDriverLogPath: "../chromedriverlog.txt"
    });
  },
  sleep: time => new Promise(resolve => setTimeout(resolve, time))
};

然后是测试本身:

const chaiAsPromised = require("chai-as-promised");
const chai = require("chai");
chai.should();
chai.use(chaiAsPromised);

const testHelper = require("./initialise");
const app = testHelper.initialiseSpectron();

// Setup Promises and start Application
before(() => app.start());

// Tear down App after Tests are finished
after(() => {
  if (app && app.isRunning()) {
    return app.stop();
  }
});

describe("Login", () => {
  it("opens a window", function() {
    return app.client
      .waitUntilWindowLoaded()
      .getWindowCount()
      .should.eventually.equal(1);
  });

  it("tests the title", () =>
    app.client
      .waitUntilWindowLoaded()
      .getTitle()
      .should.eventually.equal("VIPFY"));
});

我的问题是我总是得到这个错误:

 1) "before all" hook in "{root}"

  0 passing (2s)
  1 failing

  1) "before all" hook in "{root}":
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

所以看起来应用程序没有启动。但事实并非如此。应用程序窗口打开,但测试似乎无法识别。我已经尝试过使用各种语法更改路径。但没有任何效果。我错过了什么?

【问题讨论】:

  • 好吧,问题好像是我们用的是旧版的electron,和spectron的版本不兼容

标签: testing electron mocha.js chai spectron


【解决方案1】:

您是否尝试过增加 mocha 的超时时间?

有时我第一次失败,然后第二次尝试。

在此处查看使用 Electron 6 的工作示例:

https://github.com/florin05/electron-spectron-example

【讨论】:

  • 至此,我基本上什么都试过了。在我下次尝试之前需要一些时间。
猜你喜欢
  • 1970-01-01
  • 2016-06-04
  • 2023-03-04
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 2017-12-06
相关资源
最近更新 更多