Search code examples
rubycucumberwatirwatir-webdriverpage-object-gem

Is it possible to know from which folder cucumber scenario is running?


I have next folder structure:

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

I want to know from which folder current scenario is running now (admin / desktop / mobile). Is it possible? Because I want to add a condition into hooks file for execute needed conditions of different folders.


Solution

  • You can access details about the feature file location using the Scenario#location method, which returns a Cucumber::Core::Ast::Location::Precise. From that you can access the feature's file path using #file:

    scenario.location.file
    #=> "features/mobile/test.feature"
    

    For example, the hook could look like:

    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