How to get @tag in step definition from scenario ? - Ruby

旧城冷巷雨未停 提交于 2020-01-05 15:06:21

问题


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:


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


来源:https://stackoverflow.com/questions/47438266/how-to-get-tag-in-step-definition-from-scenario-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!