对指定的每个标记使用After hooks

时间:2015-02-13 11:21:12

标签: ruby cucumber capybara

我正试图抓住{H}之后的Cucumbers并希望在功能中的每个标记之后运行一个钩子。例如,在我的功能login.feature中,我有多个场景,每个场景都有自己的标记

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

我的挂钩包括为每个失败的场景创建屏幕截图,但目前只有我的功能中的最后一个场景在控制台中输出失败,尽管创建了两个失败场景的图像

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

我现在得到的输出是

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

我想要实现的是在控制台中输出,以便我知道哪个场景失败了。

围绕钩子

我查看了周围的Hooks,发现我可以实现这一点,但没有制作截图

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

如何在每个方案中输出失败消息并让它创建文件

由于

1 个答案:

答案 0 :(得分:1)

Cucumber控制所有puts消息,以便将其传递给所有格式化程序。

在After hook尝试STDOUT.puts而不是