cumber.js步骤定义在第一步中因超时错误而失败

时间:2018-09-26 08:12:37

标签: node.js webdriver-io cucumberjs

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

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)

1 个答案:

答案 0 :(得分:0)

可能是一个问题...

从我所看到的来看,您正错误地使用了正则表达式

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

让我知道我是否还能再提供帮助。