0

我正在尝试使用 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.

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

4

1 回答 1

0

您是否尝试过增加摩卡咖啡的超时时间?

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

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

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

于 2019-10-16T19:45:35.093 回答