【问题标题】:cucumber.js step definition failing with timeout error in first stepcucumber.js 步骤定义失败,第一步出现超时错误
【发布时间】:2018-09-26 08:12:37
【问题描述】:

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

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)

【问题讨论】:

    标签: node.js webdriver-io cucumberjs


    【解决方案1】:

    可能是问题的一件事......

    你用错了正则表达式,无论如何我都可以看到

    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)
    

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

    步骤定义:

       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 的原因是将功能文件中的数据传递到您的步骤定义文件中,以便为多个功能/场景使用相同的步骤定义。

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

    【讨论】:

    • 问题是,'loginPage.login();'之后的代码没有执行任何行。
    • 哦,那么最后一件事..试试这个...Given(/^I login into myWebsite/, function(next) { loginPage.login(); next(); });这将允许程序始终运行下一个,如果您查看我最初发布的代码,您会看到更多用途它的案例。
    • 我向给定的钩子添加了一个回调,如得到一个错误的建议 - 函数使用多个异步接口:回调和承诺使用回调接口:不返回使用承诺接口的承诺:删除函数的最后一个参数
    • @AvinashSalimath 你找到解决方案了吗?我看到你在评论中提到的同样的错误..
    • @FayazMd:是的,我在步骤 def 中使用了正则表达式而不是“欢迎来到我的网站”
    猜你喜欢
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    • 2020-11-28
    • 2014-03-04
    • 1970-01-01
    相关资源
    最近更新 更多