node.js - 使用 Spectron 和 Travis 测试 Electron 应用程序

标签 node.js unit-testing automated-tests electron travis-ci

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

执行此操作的正确方法是什么?

最佳答案

如果您不具体说明发生了什么,就很难回答您的问题。但下面是我解决这个问题的方法,希望对你有帮助。

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

.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

utils.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;
};

.e2e.js-文件

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

  it('should run', function () {
  });
});

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

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

utils.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;
};

.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));
  });
});

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

将您的 AWS S3 凭证添加到您的 Travis 环境变量中,read here for more information .

将以下内容添加到.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

它的作用是

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

关于node.js - 使用 Spectron 和 Travis 测试 Electron 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42813924/

相关文章:

ruby-on-rails - Rails 测试和 Mocha : How to stub specific model - conditional any_instance?

.net - ASP.NET 单元测试 - WatiN 和 Windows 7/Internet Explorer 8

javascript - Cypress cucumber ^ ParseError : Unexpected token

automated-tests - 使用 Cypress 定期检索和比较元素的样式属性

nunit - 动态测试用例

node.js - 无法在 Lambda 函数的模块外部使用 import 语句

unit-testing - 如何使用 jasmine 模拟 Angular 服务?

node.js - 将 Node.js Socket.io 托管为 Azure 应用程序

javascript - Sequelize 关联不与模型定义同步

node.js - 如何为自定义过滤器 [Bootstrap、JQuery] 配置 Symfony2 和 Assetic 设置?