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

问题描述 投票:0回答:4

我正在 Rails 5.1 中使用系统测试,我想关闭失败案例的自动屏幕截图。通常,失败消息足以让我弄清楚发生了什么 - 值得注意的是,网络驱动程序浏览器窗口总是获得焦点以便截取屏幕截图,这在我处理代码(或尝试执行其他任何操作)时很烦人).

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

ruby-on-rails capybara
4个回答
5
投票

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

after_teardown
方法

def after_teardown
  take_failed_screenshot
  Capybara.reset_sessions!
  super
end

要阻止其截图,最简单的方法是在 ApplicationSystemTestCase 类中覆盖

take_failed_screenshot

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by ...

  # Add this method
  def take_failed_screenshot
    false
  end
end

1
投票

我认为重写 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

0
投票

我真的觉得应该在某个地方有一个选项,但它似乎仍然是 Rails 7 中的一个问题。

这是我的解决方案(将其放在您的spec_helper.rb或适当的位置来配置您的rspec)。如果满足某些条件,您可以将其放入条件中或在方法中调用 super 。

  RSpec.configure do |config|
    config.before(:suite) do
      ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper.module_eval do
        def take_failed_screenshot
          # Overrides the default behavior to prevent taking screenshots on failure.
        end
      end
    end
  end

-1
投票
Capybara::Screenshot.autosave_on_failure = false

我想这会解决你的问题。

尝试找到要导入该库的文件。或者找到 capybara-screenshot/lib/capybara-screenshot.rb 并将

self.autosave_on_failure = true
更改为
false

© www.soinside.com 2019 - 2024. All rights reserved.