【问题标题】:Spectron with Jest to test basic window open not workingSpectron 与 Jest 测试基本窗口打开不工作
【发布时间】:2019-01-11 12:55:45
【问题描述】:

我正在使用 React 开发一个 Electron 桌面应用程序。尝试为代码的电子端设置测试。 我有一个 Launch.test.js 文件,我正在尝试用 Jest 测试 Spectron。 当我运行测试文件时,它会抛出错误。无法修复错误。

我的文件结构是这样的

node_modules
public
src
 --components
 --tests
   --Launch.test.js
main.js
package.json
README.md

Launch.test.js 的代码如下 -

const Application = require('spectron').Application;
const path = require('path');

let electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', 'electron');

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

const appPath = path.join(__dirname, '..', '..');

const app = new Application({
  path: electronPath,
  args: [appPath],
});

describe('Test Example', () => {
  beforeEach(() => {
  return app.start();
});

afterEach(() => {
   return app.stop();
});

it('opens a window', () => {
  expect.assertions(1);
  return app.client.element('input').getValue().then((j) => {
    console.log('xxxx', j);
    expect(1).toEqual(1);
  });
});

});

当我运行它时,我收到以下错误。

src/tests/Launch.test.js (12.74s)
Test Example
× opens a window (5253ms)

● Test Example › opens a window

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

  at mapper (node_modules/jest-jasmine2/build/queue_runner.js:41:52)

● Test Example › opens a window

An element could not be located on the page using the given search parameters ("input").

  at execute(<Function>, "require") - C:/Users/CKE1LUD/Desktop/repos/leela/node_modules/spectron/lib/api.js:63:26

● Test Example › opens a window

Application not running

  21 |
  22 |   afterEach(() => {
> 23 |     return app.stop();
     |                ^
  24 |   });
  25 |
  26 |   it('opens a window', () => {

  at Application.Object.<anonymous>.Application.stop (node_modules/spectron/lib/application.js:59:48)
  at Object.stop (src/tests/Launch.test.js:23:16)

● Test Example › opens a window

expect.assertions(1)

Expected one assertion to be called but received zero assertion calls.

  25 |
  26 |   it('opens a window', () => {
> 27 |     expect.assertions(1);
     |            ^
  28 |     return app.client.element('input').getValue().then((j) => {
  29 |       console.log('xxxx', j);
  30 |       expect(1).toEqual(1);

  at Object.assertions (src/tests/Launch.test.js:27:12)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        15.316s

【问题讨论】:

  • 你能在哪里解决这个问题?

标签: jestjs spectron


【解决方案1】:

使用jest.setTimeout() 增加等待超时。

有时您在Electron 真正启动之前有很多初始化代码,因此您希望等待比默认值 (5000) 更长的时间。

【讨论】:

  • 我仍然收到错误 ``` { 错误:未知错误:Chrome 无法启动:异常退出。 (未知错误:DevToolsActivePort 文件不存在)(从 chrome 位置 path-to-my-project/node_modules/spectron/lib/launcher.js 启动的进程不再运行,因此 ChromeDriver 假设 Chrome 已崩溃。) ... ```
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 2018-02-26
  • 2013-12-16
  • 1970-01-01
相关资源
最近更新 更多