Monday, 4 May 2015

Cucumber _get scenario tags, feature name and scenario name during cucumber scenario execution in Ruby - Interesting..

Feature: Development feature
   To login to portal using valid credentials


   @portal   Scenario Outline: Login with valid credentials
      Given I launch portal login page
     Then I Sign in with valid credentials 
     Examples:     |brand|     |xyz|
   @portal    Scenario: Login with invalid credentials
     Given I launch portal login page
     Then sign in using invalid user_name and password
     And I should see error_message on login page
 
 
hooks.rb file:
 
Before do |scenario|
  # Feature name  case scenario    when Cucumber::Ast::Scenario      @feature_name = scenario.feature.name
    when Cucumber::Ast::OutlineTable::ExampleRow      @feature_name = scenario.scenario_outline.feature.name
  end  puts @feature_name
  # Scenario name  case scenario    when Cucumber::Ast::Scenario      @scenario_name = scenario.name
    when Cucumber::Ast::OutlineTable::ExampleRow      @scenario_name = scenario.scenario_outline.name
  end
  puts @scenario_name
  # Tags (as an array)  @scenario_tags = scenario.source_tag_names
  # puts "Printing tags: #{@scenario_tags}"end 


In Execution Results:

Development feature
To login to portal using valid credentials

Login with valid credentials

Printing tags: ["@portal"]