【问题标题】:After a Capybara failed scenario output screen shotCapybara 失败后的场景输出屏幕截图
【发布时间】:2013-10-30 10:49:53
【问题描述】:

我想在场景失败后输出页面的屏幕截图。

我正在使用 capybara、rspec 和 launchy - 我的 gemfile 有

group :development, :test do
    gem 'rspec-rails'
    gem 'capybara'
    gem 'launchy'
end 

我看到了各种建议,包括以下代码

After do |scenario|
  save_and_open_page if scenario.failed?
end

所以,我创建了一个 spec\support\env.rb 文件,我在其中放入了这段代码(仅此而已,该文件中没有其他内容)。现在当我运行时

bundle exec rspec

我明白了

C:/Working/x/spec/support/env.rb:1:in `<top (required)>': undefined method `After' for main:Object (NoMethodError)

如果我注释掉 env.rb 中的行,那么测试会按我的预期运行。

我错过了什么?

谢谢

版本:

  • 导轨 4
  • rspec 2.14.1
  • 水豚2.1.0
  • 启动 2.3.0

【问题讨论】:

    标签: ruby-on-rails rspec capybara


    【解决方案1】:

    尝试将其包装在 Rspec.configure 块中,例如:

    RSpec.configure do |config|
      config.after { |example_group| save_and_open_page if example_group.exception }
    end
    

    【讨论】:

    • 我将其更改为:RSpec.configure do |config| config.after { |scenario| save_and_open_page if scenario.failed? } end 以绕过错误"undefined local variable or method 'scenario'",但现在我得到undefined method failed?
    • 对不起,它出现了拼写错误,没有尝试解决方案。似乎 after 钩子产生了一个 ExampleGroup 而不是一个 Example 到块,我编辑了我的答案。如果它对你有用,请告诉我。
    • 这是对它的排序@ejosafat。我实际上最终使用了 save_page 并使用 example_group.example.description 来命名页面,因此很容易看出哪个场景失败了。感谢您的更新。
    【解决方案2】:

    @ejosafat - 额外的“示例”。在你的回答中,所以我使用它如下:

    RSpec.configure do |config|
      config.after { |example_group| save_and_open_page if example_group.exception }
    end
    

    【讨论】:

      【解决方案3】:

      有一个 gem,你只需要将它添加到你的 Gemfile 中:

      gem 'capybara-screenshot', :group => :test
      

      所有说明都可以在这里找到:- https://github.com/mattheworiordan/capybara-screenshot

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-10
        相关资源
        最近更新 更多