Nightwatch-Cucumber 和 Nightwatch 的超时错误

Timeout error with Nightwatch-Cucumber and Nightwatch

当前行为 我使用 Nightwatch-Cucumber 和 PageObject Pattern 并得到一个未预料的 Error: function timed out after 60000 milliseconds.

Expected/desired 行为 所有 Nightwatch-Cucumber 检查(如可见性检查)都必须失败并且不会发生超时问题。

问题重现 作为先决条件,我在 timeout.js:

中全局设置默认超时(60 秒)
const {defineSupportCode} = require('cucumber');

defineSupportCode(({setDefaultTimeout}) => {
  setDefaultTimeout(60 * 1000);
});

...我在 nightwatch.conf.js 中为 Nightwatch 设置了 waitForConditionTimeoutwaitForConditionPollInterval:

  test_settings: {
    default: {
      globals : {
        "waitForConditionTimeout": 30000,
        "waitForConditionPollInterval": 500
      },

现在我有一个必须失败的 Cucumber 测试。所以,我想测试测试框架的正确行为:

Feature: only a test feature

  Scenario: only a test Scenario
    #first step should pass
    Given a user is on a details page with id "123"
    #second step should fail
    Then user is on the first page of the booking funnel

下面是两个步骤的定义:

const {client} = require('nightwatch-cucumber');
const {defineSupportCode} = require('cucumber');

const detailsPage = client.page.detailsPageView();
const bookingPage = client.page.bookingStepOnePageView();

defineSupportCode(({Given, When, Then}) => {

  Given(/^a user is on a details page with id "([^"]*)"$/, (id) => {
    return detailsPage.openUrlWithId(client, id);
  });

  Then(/^user is on the first page of the booking funnel$/, () => {
    return bookingPage.checkFirstStepOfBookingFunnel(client);
  });
});

这是第一个黄瓜步骤 (detailsPageView.js) 的页面对象函数:

module.exports = {
  elements: {},
  commands: [{
    openUrlWithId(client, id) {
      return client
        .url('http://test.com?id=' + id);
    }
  }]
};

...以及第二个黄瓜步骤 (bookingStepOnePageView) 的页面对象函数:

const offerSummary = 'div[id="offerSummary"]';

module.exports = {
  elements: {},
  commands: [{
    checkFirstStepOfBookingFunnel(client) {
      client.expect.element(offerSummary).to.be.visible.after();
      return client;
    },
  }]
};

现在,如果我要 运行 我的测试,我预计第二个黄瓜步骤会失败,因为预订渠道的第一页未加载且不存在。因此,预订页面对象函数 client.expect.element(offerSummary).to.be.visible.after(); 中的可见性检查必须失败。现在我希望在 nightwatch.conf.js 中定义的 "waitForConditionTimeout":30000 将在这种情况下使用并且可见性检查将在 30 秒后失败,但是我在 60 秒后收到超时错误 timeout.jssetDefaultTimeout(60*1000).

此外,我的测试 运行(通过 nightwatch --env chrome 进行的测试过程)没有结束,浏览器 window 没有关闭。所以我必须用 ctrl + c.

手动结束 运行 (进程)

在这里你可以看到输出:

grme:e2e-web-tests GRme$ npm run test-chrome

> e2e-web-tests@0.0.2 test-chrome /Users/GRme/projects/myProject/e2e-web-tests
> nightwatch --env chrome

Starting selenium server... started - PID:  29642
Feature: only a test feature

  @run
  Scenario: only a test Scenario
  ✔ Given a user is on a details page with id "123"
  ✖ Then user is on the first page of the booking funnel

Failures:

1) Scenario: only a test Scenario - features/testFeature.feature:4
   Step: Then user is on the first page of the booking funnel - features/testFeature.feature:6
   Step Definition: features/step_definitions/bookingFunnelStepDefinition.js:33
   Message:
     Error: function timed out after 60000 milliseconds
         at Timeout._onTimeout (/Users/GRme/projects/myProject/e2e-web-tests/node_modules/cucumber/lib/user_code_runner.js:91:22)
         at ontimeout (timers.js:488:11)
         at tryOnTimeout (timers.js:323:5)
         at Timer.listOnTimeout (timers.js:283:5)

1 scenario (1 failed)
2 steps (1 failed, 1 passed)
1m09.648s
^C
grme:e2e-web-tests GRme$

在最后一行你可以看到 ^C 作为我手动停止的测试进程。

特别是当我想执行一个可能包含两个黄瓜测试的测试套件时。第一个测试是我解释的,第二个是我期望 pass 的测试。在这种情况下,两个测试对我来说都会失败,因为在第二个测试中,第一个测试 (client.expect.element(offerSummary).to.be.visible.after();) 的可见性检查将失败,我不知道为什么。

这是我的两个测试的控制台输出(第二个必须通过!):

grme:e2e-web-tests GRme$ npm run test-chrome

> e2e-web-tests@0.0.2 test-chrome /Users/GRme/projects/myProject/e2e-web-tests
> nightwatch --env chrome

Starting selenium server... started - PID:  29691
Feature: only a test feature

  @run
  Scenario: only a test Scenario
  ✔ Given a user is on a details page with id "123"
  ✖ Then user is on the first page of the booking funnel

  @run
  Scenario: only a test Scenario 2
  ✖ Given a user is on a details page with id "123"

Failures:

1) Scenario: only a test Scenario - features/testFeature.feature:4
   Step: Then user is on the first page of the booking funnel - features/testFeature.feature:6
   Step Definition: features/step_definitions/bookingFunnelStepDefinition.js:33
   Message:
     Error: function timed out after 60000 milliseconds
         at Timeout._onTimeout (/Users/GRme/projects/myProject/e2e-web-tests/node_modules/cucumber/lib/user_code_runner.js:91:22)
         at ontimeout (timers.js:488:11)
         at tryOnTimeout (timers.js:323:5)
         at Timer.listOnTimeout (timers.js:283:5)

2) Scenario: only a test Scenario 2 - features/testFeature.feature:9
   Step: Given a user is on a details page with id "123" - features/testFeature.feature:10
   Step Definition: features/step_definitions/detailStepDefinition.js:12
   Message:
     Expected element <div[id="offerSummary"]> to be visible - element was not found - Expected "visible" but got: "not found"
         at Page.checkFirstStepOfBookingFunnel (/Users/GRme/projects/myProject/e2e-web-tests/pageobjects/bookingStepOnePageView.js:49:21)
         at World.Then (/Users/GRme/projects/myProject/e2e-web-tests/features/step_definitions/bookingFunnelStepDefinition.js:34:24)

2 scenarios (2 failed)
3 steps (2 failed, 1 passed)
1m11.448s
^C
grme:e2e-web-tests GRme$

也许我的测试会失败,最糟糕的是我的测试过程没有结束或继续进行下一个黄瓜测试。

那么,如何解决NightwatchNightwatch-Cucumber的超时问题呢?

我的环境:

Mac OS X 10.12.5
Chrome Browser 59.0.3071.115
npm 5.0.4
cucumber@2.3.1
nightwatch@0.9.16
nightwatch-cucumber@7.1.10
v8.0.0

希望你能帮助我:)

谢谢, 马丁

我尝试了更多的调试。也许这是 Expect?

的错误

我将全局超时设置为 20 秒:

const {defineSupportCode} = require('cucumber');

defineSupportCode(({setDefaultTimeout}) => {
  setDefaultTimeout(20 * 1000);
});

现在我有以下 Expect 检查:

client.expect.element(myElement).text.to.contain(myText).after(10000);

在我的例子中 myElement 无法在 DOM 中找到以产生错误。但是现在我收到错误消息 Error: function timed out after 20000 milliseconds,但我预计测试会在 10 秒后失败,因为在 DOM.

中找不到元素 myElement

之后我尝试用 Nightwatch 命令替换 Expect API 方法:

client
  .waitForElementVisible(arrivalDepartureDate, 10000)
  .assert.containsText(myElement, myText);

现在我在 10 秒后得到了正确的错误 ERROR: Unable to locate element: ".Grid__colM3___EyfpA" using: css selector

放弃 Expect 是一种解决方法,在我的例子中,现在我有两个命令来检查元素及其文本。使用 Expect 我可以在一行命令中完成。