【问题标题】:How to disable screenshot in rails 6 rspec app如何在rails 6 rspec应用程序中禁用屏幕截图
【发布时间】:2021-12-30 18:11:35
【问题描述】:

我目前正在使用 Rspec 和 Capybara 运行 Rails 6 应用程序。运行系统规范时,rails 会自动生成屏幕截图。这使我的测试变慢。我想禁用屏幕截图。如何禁用屏幕截图?

spec_helper.rb

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups
end

rails_helper.rb

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end
RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
  config.include FactoryBot::Syntax::Methods# config.filter_gems_from_backtrace("gem name")
end
Capybara.default_driver = :selenium_chrome_headless

目前,禁用屏幕截图的唯一方法是包含这样的 before 块:

require 'rails_helper'

RSpec.describe 'Hello world', type: :system do
  before do
    driven_by(:selenium_chrome_headless)
  end
  describe 'index page' do
    it 'shows the right content' do
      get hello_world_index_path
      expect(page).to have('hello world')
    end
  end
end

我正在寻找一种更可持续的方式来默认禁用屏幕截图。

【问题讨论】:

  • 你使用capybara-screenshot gem吗?
  • @SampatBadhe 不,我没有。我目前正在使用 Rails 6,我认为 Rails 6 不需要 capybara-screenshot 来截取屏幕截图。

标签: ruby-on-rails rspec


【解决方案1】:

Rails 在测试的拆解阶段默认调用 take_failed_screenshothttps://github.com/rails/rails/blob/c5bf2b4736f2ddafbc477af5b9478dd7143e5466/actionpack/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb#L8

我没有看到任何可以关闭它的配置。

方法在这里定义https://github.com/rails/rails/blob/c5bf2b4736f2ddafbc477af5b9478dd7143e5466/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb#L44-L46

也许您可以尝试覆盖该方法以不截取屏幕截图?像这样的事情是你使用 minitest:

# test/application_system_test_case.rb

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]

  def take_failed_screenshot
  end
end

或者如果你使用 RSpec:

# spec/rails_helper.rb

module NoFailedScreenshots
  def take_failed_screenshot
  end
end

RSpec.configure do |config|
  config.include(NoFailedScreenshots)
  ...

但是有一条评论,如果这些是您引用的屏幕截图,那么如果这会减慢速度,那么您会遇到太多失败的规范的问题,您应该改正规范。

如果这不是发生在你身上的事情,那么你可能有一些自定义设置截屏,它不是标准的 Rails

【讨论】:

    猜你喜欢
    • 2022-10-08
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多