Richlewis
Richlewis

Reputation: 15384

Using After hooks for each tag specified

I am trying to get to grips with Cucumbers After Hooks and would like to run a hook after every tag in a feature. So for example within my feature login.feature I have multiple scenarios, each with their own tag

Feature: Login to myApp with various Users

@login_user_1
 Scenario: Load Login Screen for User_1
 Given I am on the login page
 Then I will enter my credentials
 Then I will successfully Login
 Then I should Logout

@login_user_2
 Scenario: Load Login Screen for User_2
 Given I am on the login page
 Then I will enter my credentials
 Then I will successfully Login
 Then I should Logout

My after hook consists of creating a screenshot for each scenario that fails, but at the moment only the last scenario in my feature is outputting a failure in the console, though images for both failed scenarios are created

After do |scenario|
  dir_path = "/var/apps/MyApp/report/screenshots"
  #Check scenario has failed?
    if(scenario.failed?)
     time = Time.now.strftime('%Y_%m_%d_%Y_%H_%M_%S_')
     name_of_scenario = time + scenario.name.gsub(/\s+/, "_").gsub("/","_")
     puts "#===========================================================#"
     puts "TEST FAILED - Name of screenshot is #{name_of_scenario}"
     puts "#===========================================================#"
     file_path = File.expand_path(dir_path)+'/'+name_of_scenario +'.png'
     page.driver.browser.save_screenshot file_path
    end
end

The output i get at the moment is

Feature: Login to MyApp as various Users

@login_user_1
Scenario: Load Login Screen for user_1 # features/login.feature:4
Given I am on the login page         # features/step_definitions/login.rb:1
  expected to find css "#lnlogop" but there were no matches (RSpec::Expectations::ExpectationNotMetError)
  ./features/step_definitions/login.rb:3:in `/^I am on the login page$/'
  features/login.feature:5:in `Given I am on the login page'
Then I will enter my credentials     # features/step_definitions/login.rb:6
Then I will successfully Login       # features/step_definitions/login.rb:13
Then I should Logout                 # features/step_definitions/login.rb:17

@login_user_2
Scenario: Load Login Screen for user_2 # features/login.feature:11
Given I am on the login page           # features/step_definitions/login.rb:1
  #===========================================================#
  TEST FAILED - Name of screenshot is 2015_02_13_2015_11_15_02_Load_Login_Screen_for_MAuser
  #===========================================================#
  expected to find css "#lnlogop" but there were no matches (RSpec::Expectations::ExpectationNotMetError)
  ./features/step_definitions/login.rb:3:in `/^I am on the login page$/'
  features/login.feature:12:in `Given I am on the login page'
Then I will enter my credentials       # features/step_definitions/login.rb:6
Then I will successfully Login         # features/step_definitions/login.rb:13
Then I should Logout                   # features/step_definitions/login.rb:17

Failing Scenarios:
cucumber features/login.feature:4 # Scenario: Load Login Screen for user_1
cucumber features/login.feature:11 # Scenario: Load Login Screen for user_2

What I want to achieve is have output in the console so that I know which scenario has failed.

Around Hooks

I looked into Around Hooks, and found that i could achieve this but no screenshot was being produced

Around('@user_1', '@user_2') do |scenario, block|
  block.call
  #Check scenario has failed?
    if(scenario.failed?)
     dir_path = "/var/apps/MyApp/report/screenshots"
     time = Time.now.strftime('%Y_%m_%d_%Y_%H_%M_%S_')
     name_of_scenario = time + scenario.name.gsub(/\s+/, "_").gsub("/","_")
     puts "#===========================================================#"
     puts "TEST FAILED - Name of screenshot is #{name_of_scenario}"
     puts "#===========================================================#"
     file_path = File.expand_path(dir_path)+'/'+name_of_scenario +'.png'
     page.driver.browser.save_screenshot file_path
    end
end

How can i output a failure message per scenario and get it to create the file

Thanks

Upvotes: 0

Views: 217

Answers (1)

jmccure
jmccure

Reputation: 1259

Cucumber takes control of all puts messages so it can pass it along to all the formatters.

In the After hook try STDOUT.puts instead

Upvotes: 1

Related Questions