Javascript 如何为js中的每个要素文件指定步骤定义文件? 我的目标
我正在尝试为一个大型应用程序创建一个功能和步骤定义的可伸缩结构,我的第一个尝试是将步骤定义文件链接到功能,以便我可以对不同的步骤定义使用相同的步骤模式 我的代码 我将展示我当前的示例: 我的文件夹结构:Javascript 如何为js中的每个要素文件指定步骤定义文件? 我的目标,javascript,bdd,cucumberjs,Javascript,Bdd,Cucumberjs,我正在尝试为一个大型应用程序创建一个功能和步骤定义的可伸缩结构,我的第一个尝试是将步骤定义文件链接到功能,以便我可以对不同的步骤定义使用相同的步骤模式 我的代码 我将展示我当前的示例: 我的文件夹结构: /features/sample.feature /features/example.feature /features/step_definitions/sample_steps.js /features/step_definitions/example_steps.js /features/
/features/sample.feature
/features/example.feature
/features/step_definitions/sample_steps.js
/features/step_definitions/example_steps.js
/features/step_definitions/common/common_steps.js
在我的示例.功能中
我有:
Scenario: Launching Cucumber
Given I have some step definitions
When I check some step definition with parameter "any"
Then I should see all green "sample"
Scenario: Launching Cucumber
Given I have some step definitions
When I check some step definition with parameter "any"
Then I should see all green "example"
在我的示例功能中,我有:
Scenario: Launching Cucumber
Given I have some step definitions
When I check some step definition with parameter "any"
Then I should see all green "sample"
Scenario: Launching Cucumber
Given I have some step definitions
When I check some step definition with parameter "any"
Then I should see all green "example"
在/common/common_steps.js
文件中定义了给定的和步骤,它们可以正常工作
Then
步骤定义为sample_steps.js
和example_steps.js
,但不同
在我的示例_steps.js中,我有:
Then('I should see all green {stringInDoubleQuotes}', (arg) => {
if (arg !== 'sample') {
throw 'I should see all green when the argument is "sample"';
}
return;
});
Then('I should see all green {stringInDoubleQuotes}', (arg) => {
if (arg !== 'example') {
throw 'I should see all green when the argument is "example"';
}
return;
});
最后,在我的示例_steps.js中,我有:
Then('I should see all green {stringInDoubleQuotes}', (arg) => {
if (arg !== 'sample') {
throw 'I should see all green when the argument is "sample"';
}
return;
});
Then('I should see all green {stringInDoubleQuotes}', (arg) => {
if (arg !== 'example') {
throw 'I should see all green when the argument is "example"';
}
return;
});
错误
我的主要目标是让这里都是绿色的,但当然,它不起作用,我得到了一个明显的错误:
Multiple step definitions match:
I should see all green {stringInDoubleQuotes} - features\step_definitions\example_steps.js:6
I should see all green {stringInDoubleQuotes} - features\step_definitions\sample_steps.js:6
黄瓜
我知道在cucumber jvm中,我们可以指定一个链接特性和步骤定义的glue
属性,这正是我想要的,但是在cucumber js中。Java中的示例:
@RunWith(Cucumber.class)
@Cucumber.Options( glue = { "com.app.stepdefinitions.common", "com.app.stepdefinitions.sample" } )
public class SampleFeature{
}
@RunWith(Cucumber.class)
@Cucumber.Options( glue = { "com.app.stepdefinitions.common", "com.app.stepdefinitions.example" } )
public class ExampleFeature{
}
最后
如何使用cucumber js实现与cucumber jvm相同的功能?您就不能这样做:
Then('I should see all green "sample"', () => {
return;
});
Then('I should see all green "example"', () => {
return;
});
在更大的代码库中,步骤定义的问题是许多步骤非常相似。i、 e:我导航到“页面”——你可以把代码放在一个公共的地方,把URL放在其他地方
就我个人而言,我会这样做:
Then('I should see all green "(.*)"', (arg) => {
// DO SOME COMMON STUFF
arg = arg.toLowerCase();
if(arg === "sample"){
// DO SAMPLE OWN STUFF
}else if(arg === "example"){
// DO EXAMPLE STUFF
}
// CARRY ON WITH COMMON STUFF
});
但这可能会持续很长时间。如果他们做的事情完全不同,那么选择第一种,如果他们做的事情几乎相同,那么选择第二种
实际上,您可能只需要这样做:
Then('I should see all green "(.*)"', (arg) => {
// DO SOME COMMON STUFF
arg = arg.toLowerCase();
var result = runTheseSteps(arg);
// CARRY ON WITH COMMON STUFF
});
编辑
我已经找到了一个可以做到这一点的方法。这将是一个通用函数,这不是您的需求的一部分(对不起),但我相信这可能是让它工作的唯一方法
在你的钩子里:
this.Before(function (scenario, callback) {
var tags = [];
for(var i=0;i<scenario.getTags().length; i++ ){
tags[i] = scenario.getTags()[i].getName();
}
global.scenarioTags = tags.join(", ");
callback();
});
虽然它让您的步骤定义保持通用,但这意味着您可以将任何内容放入双引号中,只要您标记场景,正确的代码就会运行
@sample
Scenario: Launching Cucumber
Given I have some step definitions
When I check some step definition with parameter "any"
Then I should see all green "sample"
@example
Scenario: Launching Cucumber
Given I have some step definitions
When I check some step definition with parameter "any"
Then I should see all green "example"
好。。。我希望避免使用接收参数的超泛型方法,因为这会使步骤过于复杂。你的第一个选择仍然是我现在正在做的。但这让我在功能文件中加入了一些技术词汇来区分步骤…:(参见我的编辑,它可能是通用的,但会做你想做的,直到Cucumber给出一种方法将实际步骤粘合到场景中。