简体   繁体   English

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

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

How to get tags from scenario into step definition in Ruby? 如何从场景中获取标记到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

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

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 hooks将场景传递给他们,并使用它来将实例变量设置为标记名称。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM