当前行为
我使用Nightwatch-Cucumber和PageObject模式,我得到了一个未预见到的Error: function timed out after 60000 milliseconds
。
预期/期望的行为 所有Nightwatch-Cucumber检查(如可见性检查)都必须失败,并且不会出现超时问题。
重现问题 作为前提条件,我在timeout.js中全局设置默认超时(60秒):
const {defineSupportCode} = require('cucumber');
defineSupportCode(({setDefaultTimeout}) => {
setDefaultTimeout(60 * 1000);
});
...我在waitForConditionTimeout
中为Nightwatch设置了waitForConditionPollInterval
和nightwatch.conf.js
:
test_settings: {
default: {
globals : {
"waitForConditionTimeout": 30000,
"waitForConditionPollInterval": 500
},
现在我有一个必须失败的黄瓜测试。所以,我想测试testframework的正确行为:
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();
中的可见性检查必须失败。现在我希望"waitForConditionTimeout":30000
中定义的nightwatch.conf.js
将在这种情况下使用,并且可见性检查将在30秒后失败,但是我在60秒后得到超时错误,如何在{{1}中定义与timeout.js
。
另外,我的测试运行(通过setDefaultTimeout(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$
和Nightwatch
的超时问题?
我的环境:
Nightwatch-Cucumber
我希望你能帮助我:)。
谢谢, 马丁
答案 0 :(得分:0)
我尝试了多一点调试。也许这是Expect
的错误?
我将全局超时设置为20秒:
const {defineSupportCode} = require('cucumber');
defineSupportCode(({setDefaultTimeout}) => {
setDefaultTimeout(20 * 1000);
});
现在我有以下Expect
支票:
client.expect.element(myElement).text.to.contain(myText).after(10000);
在我的情况下,在DOM中找不到myElement
来产生错误。但是现在我收到错误消息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
,我可以在一行命令中完成。