[英]Pull scenario outline (or read a tag) from within cucumber step
If I have a scenario that starts like this: 如果我有一个像这样开始的场景:
@my-tag
Scenario Outline:
Admin user changes email
Given I register a random email address
... ...
is it possible to read either the scenario outline text or the @my-tag
in an individual step definition? 是否可以在单个步骤定义中读取场景大纲文本或@my-tag
? For example, in the I register a random email address
step I'd like to print debug info if it's running under a given scenario or a tag value. 例如,在I register a random email address
步骤,如果它在给定方案或标签值下运行,我想打印调试信息。
You cannot access that information directly from within a step definition. 您无法直接从步骤定义中访问该信息。 If you need the information, you will have to capture it during a before hook. 如果您需要这些信息,则必须在之前的钩子中捕获它。
Cucumber v3+ 黄瓜v3 +
The following before hook will capture the feature name, scenario/outline name and the list of tags. 钩子之前的以下内容将捕获功能名称,方案/大纲名称和标签列表。 Note that this solution is for Cucumber v3.0+. 请注意,此解决方案适用于Cucumber v3.0 +。 For earlier versions, see the end of the answer. 对于早期版本,请参阅答案的结尾。
Before do |scenario|
# Feature name
@feature_name = scenario.feature.name
# Scenario name
@scenario_name = scenario.name
# Tags (as an array)
@scenario_tags = scenario.source_tag_names
end
As an example, the feature file: 例如,功能文件:
@feature_tag
Feature: Feature description
@regular_scenario_tag
Scenario: Scenario description
Given scenario details
@outline_tag
Scenario Outline: Outline description
Given scenario details
Examples:
|num_1 | num_2 | result |
| 1 | 1 | 2 |
With step defined as: 步骤定义为:
Given /scenario details/ do
p @feature_name
p @scenario_name
p @scenario_tags
end
Will give the results: 会给出结果:
"Feature description"
"Scenario description"
["@feature_tag", "@regular_scenario_tag"]
"Feature description"
"Outline description, Examples (#1)"
["@feature_tag", "@outline_tag"]
You could then check the @scenario_name or @scenario_tags for your conditional logic. 然后,您可以检查@scenario_name或@scenario_tags以获取条件逻辑。
Cucumber v2 黄瓜v2
For Cucumber v2, the required hook is a more complicated: 对于Cucumber v2,所需的钩子更复杂:
Before do |scenario|
# Feature name
case scenario
when Cucumber::Ast::Scenario
@feature_name = scenario.feature.name
when Cucumber::Ast::OutlineTable::ExampleRow
@feature_name = scenario.scenario_outline.feature.name
end
# Scenario name
case scenario
when Cucumber::Ast::Scenario
@scenario_name = scenario.name
when Cucumber::Ast::OutlineTable::ExampleRow
@scenario_name = scenario.scenario_outline.name
end
# Tags (as an array)
@scenario_tags = scenario.source_tag_names
end
The output is slightly different: 输出略有不同:
"Feature description"
"Scenario description"
["@regular_scenario_tag", "@feature_tag"]
"Feature description"
"Outline description"
["@outline_tag", "@feature_tag"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.