【问题标题】:How can I get CucumberJS to recognizing my Scenario Outline parameters in a step definition?如何让 CucumberJS 在步骤定义中识别我的场景大纲参数?
【发布时间】:2019-03-29 07:31:39
【问题描述】:

使用 CucumberJS 我正在尝试为 UI 测试实现场景大纲。 Cucumber 没有正确识别或传递我的论点。这就是我所拥有的。

test.feature

Scenario Outline: User with permissions can Import Payment files with any file format
    Given a <username> has logged into site
        Examples:
            |username      |fileName    |
            |administrator |test1.csv   |
            |userA         |step2.csv   |

test_step.js

Given('a {string} has logged into site', async function (username) {
    console.log('username = ' + username);
    return this.userLogin(username);
});

世界.js

'use strict';
const { setWorldConstructor } = require('cucumber');

class testApp {
    // Write metrics data object to JSON file
    async userLogin(username) {
        await this.navigateLoginPage();
    }
}

setWorldConstructor(testApp);

现在当我运行它时,我得到以下信息:

 Warnings:

1) Scenario: User with permissions can Import Payment files with any file format # features/importPaymentFile.feature:28
   ? Given a administrator has logged into site
       Undefined. Implement with the following snippet:

         Given('a administrator has logged into site', function () {
           // Write code here that turns the phrase above into concrete actions
           return 'pending';
         });


2) Scenario: User with permissions can Import Payment files with any file format # features/importPaymentFile.feature:29
   ? Given a administrator has logged into site
       Undefined. Implement with the following snippet:

         Given('a userA has logged into site', function () {
           // Write code here that turns the phrase above into concrete actions
           return 'pending';
             });

所以现在我很困惑。看起来我的参数被正确读取,但在步骤定义中无法识别它们。

谁能告诉我应该如何实现场景大纲的参数?

更新 #3 - 最终更新 所以它对我有用:

test.feature

Scenario Outline: User with permissions can Import Payment files with any file format
    Given a "<username>" has logged into site and uploads "<fileName>"
        Examples:
            |username      |fileName    |
            |administrator |test1.csv   |
            |userA         |step2.csv   |

test_step.js

Given('a {string} has logged into site and uploads {string}', async function (username, fileName) {
    console.log('username = ' + username);
    console.log('fileName = ' + fileName);
    return this.userLogin(username);
});

世界.js

'use strict';
const { setWorldConstructor } = require('cucumber');

class testApp {
    // Write metrics data object to JSON file
    async userLogin(username) {
        await this.navigateLoginPage();
    }
}

setWorldConstructor(testApp);

结果:

> . ./.env; node app.js "--feature" "importPaymentFile"

username = administrator
filename = oneStepApproval_MediaOcean.csv
.username = operations
filename = twoStepApproval_MediaOceanDan.csv

对不起,如果我要冗长的话。如果被告知,我会配对它:)


更新 #1

我尝试了引用,但这不起作用。在功能文件中为参数加上引号似乎会导致参数无法传递。

test.feature

Scenario Outline: User with permissions can Import Payment files with any file format
    Given a "<username>" has logged into site
        Examples:
            |username      |fileName    |
            |administrator |test1.csv   |
            |userA         |step2.csv   |

产生的错误:

username = 
.username = 
.

2 scenarios (2 passed)
2 steps (2 passed)
0m00.015s
(node:16642) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 7): Error: Protocol error(Emulation.setDeviceMetricsOverride): Session closed. Most likely the page has been closed.
(node:16642) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:16642) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 8): Error: Protocol error (Emulation.setDeviceMetricsOverride): Session closed. Most likely the page has been closed.
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: Timed out while authenticating with server
at Timeout._onTimeout (/Users/huckcarignan/Desktop/sprint26/epay-test-automation/node_modules/imap/lib/Connection.js:139:17)
at ontimeout (timers.js:475:11)
at tryOnTimeout (timers.js:310:5)
at Timer.listOnTimeout (timers.js:270:5)

更新 #2

组合 1:& {字符串}

功能文件:

Given a <username> has logged into Site

步骤定义:

Given('a {string} has logged into Site', async function (username) {
    console.log('username = ' + username);
    return this.userLogin(username);
});

结果:

? Given a administrator has logged into Site
    Undefined. Implement with the following snippet:

      Given('a administrator has logged into Site', function () {
        // Write code here that turns the phrase above into concrete actions
        return 'pending';
      });

组合 2:& ([^"]*)

功能文件:

Given a <username> has logged into Site

步骤定义:

Given('a ([^"]*) has logged into Site', async function (username) {
    console.log('username = ' + username);
    return this.userLogin(username);
});

结果:

? Given a administrator has logged into Site
    Undefined. Implement with the following snippet:

      Given('a administrator has logged into Site', function () {
        // Write code here that turns the phrase above into concrete actions
        return 'pending';
      });

组合 3:"" & "([^"]*)"

功能文件:

Given a "<username>" has logged into Site

步骤定义:

       Given('a "([^"]*)" has logged into Site', async function (username) {
         console.log('username = ' + username);
         return this.userLogin(username);
        });

结果:

? Given a {string} has logged into Site
    Undefined. Implement with the following snippet:

      Given('a administrator has logged into Site', function () {
        // Write code here that turns the phrase above into concrete actions
        return 'pending';
      });                 

组合 4:"" & ([^"]*)

功能文件:

Given a <username> has logged into Site

步骤定义:

Given('a "([^"]*)" has logged into Site', async function (username) {
    console.log('username = ' + username);
       return this.userLogin(username);
    });

结果:

? Given a {string} has logged into Site
    Undefined. Implement with the following snippet:

      Given('a administrator has logged into Site', function () {
        // Write code here that turns the phrase above into concrete actions
        return 'pending';
      });

组合 5:"" & {string} 获胜者 - 有点

功能文件:

Given a <username> has logged into Site

步骤定义:

Given('a "([^"]*)" has logged into Site', async function (string) {
    console.log('username = ' + string);
    return this.userLogin(string);
});

结果:

username = administrator
.

1 scenarios (1 passed)
1 steps (1 passed)
0m01.637s;

Sooooo...这行得通,多个参数按顺序处理 - 我会把我的结果放在最上面

【问题讨论】:

    标签: node.js cucumberjs


    【解决方案1】:

    我使用了正则表达式

    所以对于你的例子:

    Scenario Outline: User with permissions can Import Payment files with any file format
        Given a <username> has logged into site
            Examples:
                |username      |fileName    |
                |administrator |test1.csv   |
                |userA         |step2.csv   |
    

    然后在我会做的代码中:

    Given(/^a (.*) has logged into site$/, async function (username) {
        console.log('username = ' + username);
        return this.userLogin(username);
    });
    

    【讨论】:

      【解决方案2】:
      Scenario Outline: User with permissions can Import Payment files with any file format
      Given a "<username>" has logged into site
          Examples:
              |username      |fileName    |
              |administrator |test1.csv   |
              |userA         |step2.csv   |
      

      在示例中添加引号,"" 可能是 cucumber 期望传入一个字符串参数,而您的测试中并非如此

      【讨论】:

      • 我试过引号,但没有用。我在上面更新了这个。
      • @HuckleberryCarignan 您更新的答案不包含引号。我猜您使用引号运行但只是在此处的响应中出错。看起来它确实有效,并且步骤不再未定义。原来的问题看起来已经解决了,但又是另一个问题。此外,您应该在异步函数上使用 async-await。
      • 噢!是的,我确实用引号运行了它,但没有剪切并粘贴引用的版本。感谢您抓住它。我更新了以上内容以反映这一点。
      • return this.userLogin(username);这应该是return await this.userLogin(username);
      猜你喜欢
      • 2018-07-25
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多