使用 Spectron 和 travis 测试电子应用程序

raf*_*eru 0 automated-tests unit-testing node.js travis-ci electron

我正在尝试使用 spectron测试我的电子应用程序https://github.com/rafaelleru/torrent_player,我尝试在https://github.com/electron/spectron ant 中设置示例测试,它通过我的本地电脑但在 travis 中,我不知道如何设置要测试的 bin 文件,也不知道如何告诉 travis 生成 bin 文件。

这样做的正确方法是什么?

kon*_*ten 5

如果您不具体说明会发生什么,则很难回答您的问题。但下面是我解决它的方法,我希望它可以有所帮助。

这就是我让 Spectron 在 Travis(仅限 Linux)中运行我的 Electron 应用程序的方式:

.travis.yml

os:
  - linux

language: node_js

node_js:
  - "7.7"

before_script:
  - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export DISPLAY=:99.0; fi
  - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh -e /etc/init.d/xvfb start; fi
  - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sleep 3; fi

script:
  - node --version
  - npm --version
  - npm install
  - npm run e2e

cache:
  directories:
    - node_modules

notifications:
  email:
    on_success: never
    on_failure: change
Run Code Online (Sandbox Code Playgroud)

实用程序.js

const electron = require('electron');

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

  return this.app.start();
};

const afterEach = function () {
  if (this.app && this.app.isRunning()) {
    return this.app.stop();
  }
  return undefined;
};
Run Code Online (Sandbox Code Playgroud)

.e2e.js -文件

describe('test case', function () {
  beforeEach(testUtils.beforeEach);
  afterEach(testUtils.afterEach);

  it('should run', function () {
  });
});
Run Code Online (Sandbox Code Playgroud)

如何存储 Spectron 测试失败的屏幕截图

如果您的测试正在运行,但由于某种原因它们失败了,那么从 Electron 应用程序获取屏幕截图会很有帮助。

实用程序.js

const fs = require('fs');
const saveErrorShot = function (e) {
  const filename = `errorShot-${this.test.parent.title}-${this.test.title}-${new Date().toISOString()}.png`
    .replace(/\s/g, '_')
    .replace(/:/g, '');

  this.app.browserWindow.capturePage().then(imageBuffer => {
    fs.writeFile(filename, imageBuffer, error => {
      if (error) throw error;

      console.info(`Screenshot saved: ${process.cwd()}/${filename}`);
    });
  });

  throw e;
};
Run Code Online (Sandbox Code Playgroud)

.e2e.js -文件

describe('test case', function () {
  it('should store a screenshot', function () {

    return this.app.client
      .then(() => this.app.client.getText('.non-existing-element')
      .catch(testUtils.saveErrorShot.bind(this));
  });
});
Run Code Online (Sandbox Code Playgroud)

让 Travis 将您的工件发送到 Amazon S3 存储桶

将您的 AWS S3 凭证添加到您的 Travis 环境变量,阅读此处了解更多信息

将以下内容添加到.travis.yml

after_failure:
  - curl -sL https://raw.githubusercontent.com/travis-ci/artifacts/master/install | bash
  - artifacts upload $(git ls-files -o | grep -Fv -e node_modules)
  - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then artifacts upload $(ls /home/travis/.npm/_logs/); fi
Run Code Online (Sandbox Code Playgroud)

它的作用是它

  1. 下载并安装travis-artifacts
  2. 上传存储库中所有未跟踪的文件,但不包括所有节点模块。可能你会有一些你想要排除的其他文件,然后只需附加-e unnecessary_folder -e unnecesarry_file.
  3. 从 上传 npm 日志/home/travis/.npm/_logs。如果您运行的是 MacOS,文件将出现在另一个文件夹中。