After-hook scenario.exception 在 Cucumber 2.0 中不存在

After-hook scenario.exception does not exist in Cucumber 2.0

我正在使用 Capybara、Cucumber 和 Selenium-Webdriver 进行基本的 Web 测试。如果测试失败,我想捕获失败消息并将其发送到单独的日志文件。

Cucumber 提供钩子以在场景失败后捕获失败消息(来自他们的 wiki):

After do |scenario|
  if scenario.failed?
    subject = "[Project X] #{scenario.exception.message}"
    send_failure_email(subject)
  end
end

然而,当我在我的 features/support/support.rb 文件中实现它时,我得到了一个无方法错误:

undefined method `exception' for #<Cucumber::RunningTestCase::Scenario:0x007fc7b4380968>

此错误是我的设置造成的,还是 Cucumber 2.0 特有的错误?

这是我的 features/support/support.rb 代码:

require 'capybara/cucumber'
Capybara.default_driver = :selenium

After do |scenario|
    if scenario.failed?
        puts scenario.exception.message
    end
end

这是我的 features/step_definitions/step_definition.rb 代码:

Given(/^I am on the Google homepage$/) do
  visit 'http://www.google.com'
end

Then(/^I will search for "(.*?)"$/) do |searchText|
  fill_in 'lst-ib', :with => searchText
end

Then(/^I should see "(.*?)"$/) do |expectedText|
  page.should have_content(expectedText)
end

Then(/^I will click the Product link$/) do
  click_link('Product')
end

这是我的 features/findGameSparks.feature 代码:

Feature: Find the GameSparks Website

Scenario: Search for the website        
    Given I am on the Google homepage
    Then I will search for "GameSparks"
    Then I should see "A NON-EXISTENT STRING CAUSING TEST FAILURE!!!!"
    Then I will click the Product link

以下是我使用的所有内容的版本号:

OSX 10.10.1
ruby 2.2.1p85
rvm 1.26.11
gem 2.4.6
cucumber 2.0.0
cucumber-core 1.1.3
capybara 2.4.4
selenium-webdriver 2.45.0

此错误已在 4 月 1 日的此提交中修复:https://github.com/cucumber/cucumber/commit/e92917c8f10a4907dedf26d77665116b69f3e3b9

Cucumber 的最新版本是 2.0.0,于 3 月 27 日发布,因此修复似乎尚未发布。

您可以指向 Gemfile 中的 github 存储库以获取最新的可用代码:

gem 'cucumber', git: 'https://github.com/cucumber/cucumber.git'

但是请注意,运行 "prerelease" 这样的代码可能会导致其他问题,例如,如果 cucumber 的提交者弄乱了 master 分支。