gpt4 book ai didi

node.js - cucumber.js 步骤定义因第一步超时错误而失败

转载 作者:太空宇宙 更新时间:2023-11-04 01:42:18 25 4
gpt4 key购买 nike

我有一个场景,我必须登录网站并执行一些验证,下面是我的功能文件-

Feature:
As a developer
I need to validate
The login functionality and Home Page of common-core workflow

Scenario: Check title and number of apps on Home Page
Given I login into myWebsite
Then I expect that the title of the page is "Welcome to My WebSite"
And Number of apps is 7
Then Number of apps in left drawer has count 8

步骤定义文件如下-

Given(/^I login into myWebsite/, function() {
loginPage.login();
});

Then(/^I expect that the title of the page is "Welcome to My WebSite"/,
function() {
homePage.waitForPageLoad();
homePage.homePageTitle.should.be.equal("Welcome to My WebSite");
});

Then(/^Number of apps is (\d+)/, function(count) {
expect(homePage.allApps.length).to.be.equal(7);
});

Then(/^Number of apps in left drawer has count (\d+)/, function(count) {
expect(homePage.navDrawerLeftArray.length).to.be.equal(count);
});

我使用页面对象模型来获取 Web 元素并使用 Chai.js 进行评估。

当我执行上述步骤时,出现错误 -

[chrome #0-0]     Check title and number of apps on Home Page
[chrome #0-0] 1) I login into myWebsite product
[chrome #0-0] !! I expect that the title of the page is "Welcome to My
WebSite"
[chrome #0-0] !! Number of apps is 7
[chrome #0-0] !! Number of apps in left drawer has count 8
[chrome #0-0]
[chrome #0-0]
[chrome #0-0] 3 pending (59s)
[chrome #0-0] 1 failing
[chrome #0-0]
[chrome #0-0] 1) Check title and number of apps on Home Page6 I login into
myWebsite:
[chrome #0-0] function timed out, ensure the promise resolves within 30000
milliseconds
[chrome #0-0] Error: function timed out, ensure the promise resolves within
30000 milliseconds
[chrome #0-0] at Timeout._onTimeout
(Project_Path\user_code_runner.js:93:22)
[chrome #0-0] at ontimeout (timers.js:498:11)
[chrome #0-0] at tryOnTimeout (timers.js:323:5)
[chrome #0-0] at Timer.listOnTimeout (timers.js:290:5)

最佳答案

有一件事可能是问题所在......

从我看来,你错误地使用了正则表达式

Then I expect that the title of the page is "Welcome to My WebSite"

来自您的功能文件是正确的,但是当您将其放回到步骤定义中时,请执行类似的操作而不是您正在执行的操作

Then("I expect that the title of the page is {string}", 
function => {

这会将字符串“Welcome to My WebSite”从您的功能文件传递到您的步骤定义文件中,我假设如果您从原始测试中删除正则表达式,这可能会允许它通过,目前我认为由于您使用正则表达式而对运行哪个测试感到困惑。

还要在您的测试中添加类似的内容,以确保页面在元素加载之前不会尝试加载脚本。

driver.wait(until.elementIsVisible(pageElement), 10000)

最后,您不会处理未处理的 promise ,这将使代码以奇怪的方式工作,下面是我处理 promise 的示例,我将发布一个步骤定义和一个匹配的功能行,以便您知道我是如何做事情的。

步骤定义:

   Given("I open the page with the url {string} and route {string}", (url, route, next) => {
driver.get(`${url}${route}`).then(() => {
next();
}).catch(ex => {
console.log(ex);
});
});

以及要匹配的功能线..

Given I open the page with the url "http://localhost:3000" and route "/login"

希望这些至少能让您的测试达到更好的标准,您使用 Cucumber 的原因是将数据从功能文件传递到步骤定义文件中,以便对多个功能/场景使用相同的步骤定义。

如果我能提供更多帮助,请告诉我。

关于node.js - cucumber.js 步骤定义因第一步超时错误而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52512999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com