2013-08-21 41 views
6

如果我有启动这样一个场景:拉场景轮廓(或读取标签)从黄瓜步内

@my-tag 

    Scenario Outline: 
    Admin user changes email 

    Given I register a random email address 

...

是可以读取这两种方案大纲文本或在单个步骤定义中的@my-tag?例如,在I register a random email address步骤中,如果打印调试信息在给定方案或标记值下运行,那么我会打印它。

+0

尼斯问题,谢谢大家 –

回答

20

您无法直接从步骤定义中访问该信息。如果您需要这些信息,您必须在挂钩前捕捉它。

黄瓜V3 +

钩子将捕获功能名称,场景/提纲名称和标签的列表之前以下。请注意,此解决方案适用于Cucumber v3.0 +。对于早期版本,请参阅答案的结尾。

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 

举例来说,该功能文件:

@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  | 

对于步骤定义为:

Given /scenario details/ do 
    p @feature_name 
    p @scenario_name 
    p @scenario_tags 
end 

会给结果:

"Feature description" 
"Scenario description" 
["@feature_tag", "@regular_scenario_tag"] 

"Feature description" 
"Outline description, Examples (#1)" 
["@feature_tag", "@outline_tag"] 

然后,您可以检查@scenario_name或@scenario_tags为你的条件逻辑。

黄瓜V2

黄瓜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 

输出略有不同:

"Feature description" 
"Scenario description" 
["@regular_scenario_tag", "@feature_tag"] 

"Feature description" 
"Outline description" 
["@outline_tag", "@feature_tag"] 
+1

刚添加:scenario.feature.source_tag_names将仅返回[“@feature_tag”] –

+0

您保存了我的时间,谢谢你 –

+0

'未初始化的常量Cucumber :: Ast'。 Ast被弃用,无法追踪如何更换它。 –