Aruna Nayagam
Aruna Nayagam

Reputation: 1

cucumber hooks in example tags

How to read the tag name of the example in cucumber before hook.

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

I want to print "@example_tag" in output.

used java code as

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

But getting error as

"Failure in before hook:StepDefinitions.beforeScenario(ScenarioOutline) Message: cucumber.runtime.CucumberException: When a hook declares an argument it must be of type cucumber.api.Scenario. public void stepDefinitions.beforeScenario(gherkin.ast.ScenarioOutline)"


Thank you for the response but this code is printing "@outline_tag" and not the "@example_tag". That was my question. I want "@example_tag" to be printed.

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

Upvotes: 0

Views: 2363

Answers (1)

Tyler MacMillan
Tyler MacMillan

Reputation: 592

The "Before" block does not run before a scenario outline. It runs before a scenario. The scenario outline is broken up into multiple scenarios, meaning you just want to just grab the scenario that is generated:

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

Each scenario from the scenario outline will hit that before it runs.

@stuff @stuff1
Feature: Stuff

Scenario Outline: More stuff
  When some step

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

outputs:

Example Tags: @stuff
Example Tags: @stuff1
Example Tags: @stuff2
Example Tags: @somegabageExample Tags: @stuff
Example Tags: @stuff1
Example Tags: @stuff2
Example Tags: @somegabage

Upvotes: 1

Related Questions