示例标签中的黄瓜钩子

时间:2018-04-13 13:34:43

标签: cucumber hook

如何在挂钩之前读取黄瓜中示例的标记名称。

@feature_tag 功能:功能描述

@outline_tag   场景大纲:大纲说明     给出方案详情

@example_tag
Examples:
  |num_1  | num_2  | result |
  | 1        |   1       |   2     |

我想打印" @ example_tag"在输出中。

使用java代码

@Before
public void beforeScenario (ScenarioOutline ScenarioOutline) {
     examples = (Examples) ScenarioOutline.getExamples(); 
     for(Tag tag : examples.getTags()){
       System.out.println("Example Tags: " + tag.getName());
    }
}

但是错误是

"挂钩失败:StepDefinitions.beforeScenario(ScenarioOutline) 消息:cucumber.runtime.CucumberException:当一个钩子声明一个参数时,它必须是cucumber.api.Scenario类型。 public void stepDefinitions.beforeScenario(gherkin.ast.ScenarioOutline)"

感谢您的回复,但此代码正在打印" @ outline_tag"而不是" @ example_tag"。那是我的问题。我想" @ example_tag"打印。

@feature_tag 
Feature: Feature description

@outline_tag 
Scenario Outline: Outline description Given scenario details

@example_tag
Examples:
  |num_1  | num_2  | result |
  | 1     |   1    |   2    |

1 个答案:

答案 0 :(得分:1)

"之前"块在场景大纲之前不会运行。它在场景之前运行。场景大纲分为多个场景,这意味着您只想抓住生成的场景:

@Before
public void beforeScenario(Scenario scenario) {
    for(String tag : scenario.getSourceTagNames()){
        System.out.println("Example Tags: " + tag);
    }
}

场景大纲中的每个场景都会在它运行之前点击。

@stuff @stuff1
Feature: Stuff

Scenario Outline: More stuff
  When some step

  @somegabage
  Examples:
    | provider |
    | 123      |
    | 123567   | 

输出:

Example Tags: @stuff
Example Tags: @stuff1
Example Tags: @stuff2
Example Tags: @somegabageExample Tags: @stuff
Example Tags: @stuff1
Example Tags: @stuff2
Example Tags: @somegabage
相关问题