在 Rails 系统测试中关闭屏幕截图

Turn off screenshots in Rails system tests

我在 Rails 5.1 中使用系统测试,我想关闭失败案例的自动屏幕截图。通常,失败消息足以让我弄清楚发生了什么——值得注意的是,webdriver 浏览器 window 总是需要关注以截取屏幕截图,这在我处理代码(或尝试做其他事情)。

Capybara 配置中是否有内置的方式来关闭屏幕截图?我查看了文档,找不到任何明确说明的内容,但由于这是对主流 Rails 的新补充,我认为这并不一定意味着它不存在。

Capybara::Screenshot.autosave_on_failure = false

我想这会解决你的问题。

尝试在导入此库的位置找到文件。或者找到 capybara-screenshot/lib/capybara-screenshot.rb 并将 self.autosave_on_failure = true 更改为 false

在 Rails 5.1 ActionDispatch::SystemTestCase(ApplicationSystemTestCase 从中派生)有一个默认的 after_teardown 方法

def after_teardown
  take_failed_screenshot
  Capybara.reset_sessions!
  super
end

要阻止此截图,最简单的方法是在您的 ApplicationSystemTestCase class

中覆盖 take_failed_screenshot
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by ...

  # Add this method
  def take_failed_screenshot
    false
  end
end

我认为覆盖 supports_screenshot? 方法更优雅,在我的例子中,我想在 Heroku 上禁用屏幕截图 CI

 class ApplicationSystemTestCase < ActionDispatch::SystemTestCase


   private

   # disable only on CI, i.e: HEROKU CI
   def supports_screenshot?
    return false if ENV['CI'] 
    super
   end

   # to disable screenshots completely on every test invironment (local, CI)
   def supports_screenshot?
    false
   end

end