Pull the scenario diagram (or read the tag) from the cucumber step

If I have a script that starts as follows:

@my-tag Scenario Outline: Admin user changes email Given I register a random email address 

...

Is it possible to read either the text of the script scheme or @my-tag in the definition of a single step? For example, in step I register a random email address I would like to print debugging information if it works under a given script or tag value.

+9
source share
2 answers

You cannot access this information directly from the step definition. If you need information, you will need to capture it in time to hook.

Cucumber v3 +

The following will capture the function name, script / schema name, and tag list before capturing begins. Please note that this is a solution for Cucumber v3.0 +. For earlier versions, see End of 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, a function 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 | 

In increments defined as:

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

They will give the results:

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

You can then check the @scenario_name or @scenario_tags name for your conditional logic.

Cucumber v2

For cucumber v2, the required hook is more complex:

 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"] 
+23
source

What if I want to read a variable from Example ??

  Ex: @test Scenario Outline: I have an email Given I have an <email> Examples: | email | | mobile_draft@email.com | Before('@test') do ====> print the email from Example here end 

thanks

0
source

Source: https://habr.com/ru/post/952231/


All Articles