How to get @tag in step definition from scenario ?

2019-07-25 14:40发布

How to get tags from scenario into step definition in Ruby?

**@TAGS**
Scenario: Showing information of the scenario
When I execute any scenario

Now my step definition is this:

And(/^When I execute any scenario$/) do |page|
  How to get tag = @TAGS in step definition..
end

1条回答
神经病院院长
2楼-- · 2019-07-25 15:23

Not a direct solution, but hooks (Before, After, AfterStep, etc) can be set to run for specific tags, which allows you to set instance variables that are accessible in the scenario

Before('@my_tag') do  # will only run if the test has @my_tag tag
   @my_tag = true  # This instance variable will be accessible in the test
end

You could also use the fact that Before hooks get the scenario passed to them and use that to set an instance variable to the tag names

Before do |scenario|
  @tags = scenario.source_tag_names # @tags instance variable accessible in test
end
查看更多
登录 后发表回答