使用Cucumber Examples关键字时步骤定义仍显示Undefined问题
问题:Cucumber无法识别带多参数的Step定义
Feature文件中的测试场景
Scenario Outline: User can navigate to expected url by selecting the associated link Given Application configured for testing And on the Test page screen When the user selects link for <pageName> - <dataTestId> Then the user navigates to the expected url: <urlPathName> Examples: | pageName | dataTestId | urlPathName | | Test Page | passwordTestLink | passwordTest | | Custom Test | customTestLink | customTest |
错误信息
? When the user selects link for Custom Test - customTestLink
Undefined. Implement with the following snippet:Here is an example of one of the steps which is not being found:
When('the user selects link for Custom Test - customTestLink', async function () {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
当前的Step定义
When('the user selects link for {string} - {string}', async function( dataTestId:string) { administrationPage = new AdministrationPage(fixture.page); await administrationPage.clickLink(dataTestId); })
解决方案
问题核心是参数数量与顺序不匹配:
- Feature文件的
When步骤包含两个占位符<pageName>和<dataTestId>,对应Step定义里的两个{string}占位符,但你的函数只接收了一个参数dataTestId,Cucumber无法完成参数匹配。 - 必须保证Step定义的参数顺序和Feature中占位符的顺序完全一致。
修正后的Step定义:
When('the user selects link for {string} - {string}', async function(pageName:string, dataTestId:string) { administrationPage = new AdministrationPage(fixture.page); await administrationPage.clickLink(dataTestId); })
注:即使pageName参数在业务逻辑中用不到,也必须声明它来匹配参数数量,否则Cucumber会判定步骤不匹配。
另外,同步检查Then步骤的定义是否存在同样的参数匹配问题,确保其参数数量、顺序与Feature中的<urlPathName>占位符对应。
内容的提问来源于stack exchange,提问作者Kieran M
相关产品推荐
相关产品推荐

