是否可以根据使用的标签(或不使用)在Cucumber步骤中执行不同的操作?

我在整个黄瓜测试过程中使用了相同的步骤。 我想根据调用function是否分配了标记(在本例中为@javascript)进行微小更改。

是否可以在步骤中测试标签的存在和名称以更改行为? (我意识到我可以创建不同的步骤,但这不是很干嘛?)

伪代码来解释我的追求

When /^I sign in as "(.*)\/(.*)"$/ do |email,password| step %{I go to the sign in page} step %{I fill in "user_email" with "#{email}"} step %{I fill in "user_password" with "#{password}"} if tag && tag == "@javascript" step %{I follow "LOG IN"} else step %{I press "LOG IN"} end end 

我通过使用钩子来设置变量来解决这个问题,如果您的标记与驱动程序没有关联,这可能很有用:

 Before('@mobile') do @mobile = true end When /^I go to the homepage$/ do if @mobile visit "m.mysite.com" else visit "www.mysite.com" end end 

我认为你不能轻易地从步骤defs中直接访问标签,但你可以访问当前的驱动程序:

 Capybara.current_driver 

因此,假设您为@javascript标记的方案使用不同的驱动程序,这应该可行。