如何在rails 6 rsspec应用程序中禁用屏幕截图



我目前正在运行一个带有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

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

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

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

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

也许你可以尝试覆盖该方法,不进行屏幕截图?像这样的东西是你使用的最小:

# 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

最新更新