如何让 CucumberJS 在步骤定义中识别我的方案大纲参数?



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

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';
});

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

谁能给我一些关于我应该如何实现方案大纲参数的见解?

UPATE #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: & {string}

功能文件:

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}THE WINNER - sort of

功能文件:

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;

所以。。。这有效,多个参数按顺序处理 - 我将把我的结果放在最顶部

我使用了正则表达式

所以对于你的例子:

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);
});
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 期望传入一个字符串参数,而在您的测试中

并非如此

最新更新