是否有可能知道黄瓜场景正在运行?

时间:2017-01-17 08:28:48

标签: ruby cucumber watir watir-webdriver page-object-gem

我有下一个文件夹结构:

-features
  -admin
  -desktop
  -mobile
-step definitions
-support

我想知道当前正在运行的文件夹(admin / desktop / mobile)。 可能吗? 因为我想在 hooks 文件中添加一个条件,以执行不同文件夹所需的条件。

2 个答案:

答案 0 :(得分:2)

您可以使用Scenario#location方法访问要素文件位置的详细信息,该方法会返回Cucumber::Core::Ast::Location::Precise。您可以使用#file

访问该功能的文件路径
scenario.location.file
#=> "features/mobile/test.feature"

例如,钩子看起来像:

Before do |scenario|
  platform = scenario.location.file.split(File::SEPARATOR)[1].to_sym
  #=> :admin:, :desktop or :mobile

  # Output the platform (or whatever conditional logic you want)
  case platform
  when :admin then puts 'admin'
  when :desktop then puts 'desktop'
  when :mobile then puts 'mobile'
  end
end

答案 1 :(得分:0)

如果您使用的是Ruby 2.0及更高版本,那么__dir__方法就是您的选择(如果您想获得绝对路径):

# File: /home/xxx/folder/features/admin/test.rb puts __dir__ # => "/home/xxx/folder/features/admin"

但是,如果您只想知道文件所在目录的名称,该怎么办?然后你可以使用File.basename

# File: /home/xxx/folder/features/admin/test.rb puts File.basename(__dir__) # => "admin"