为每个指定的标签使用 After 钩子

Using After hooks for each tag specified

我正在尝试掌握 Cucumbers After Hooks,并希望 运行 在功能中的每个标签之后添加一个 hook。因此,例如在我的功能 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 hook 包括为每个失败的场景创建一个屏幕截图,但目前只有我的功能中的最后一个场景在控制台中输出失败,尽管为两个失败的场景创建了图像

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

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

挂钩

我查看了 Around 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

如何为每个场景输出失败消息并让它创建文件

谢谢

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

在 After 钩子中尝试 STDOUT.puts 而不是