【问题标题】:Timeout error with Nightwatch-Cucumber and NightwatchNightwatch-Cucumber 和 Nightwatch 超时错误
【发布时间】:2017-07-06 08:35:51
【问题描述】:

当前行为 我使用 Nightwatch-Cucumber 和 PageObject Pattern,我得到了一个意想不到的Error: function timed out after 60000 milliseconds

预期/期望的行为 所有 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 的测试过程)没有结束,浏览器窗口也没有关闭。所以我必须用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

我希望你能帮助我:)

谢谢, 马丁

【问题讨论】:

    标签: javascript node.js cucumber nightwatch.js cucumberjs


    【解决方案1】:

    我尝试了更多调试。也许这是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 Command 替换 Expect API 方法:

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-04-08
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多