如何从场景的步骤定义中获取@tag? - Ruby

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

如何在Ruby中将场景中的标签获取到步骤定义中?

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

现在我的步骤定义是这样的:

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

不是直接的解决方案,但是可以为特定的标签设置hooks(Before、After、AfterStep等)运行,这样可以设置场景中可以访问的实例变量

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

您还可以利用 Before 挂钩将场景传递给它们并使用它来将实例变量设置为标签名称这一事实

Before do |scenario|
  @tags = scenario.source_tag_names # @tags instance variable accessible in test
end