【发布时间】: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-screenshotgem吗? -
@SampatBadhe 不,我没有。我目前正在使用 Rails 6,我认为 Rails 6 不需要 capybara-screenshot 来截取屏幕截图。
标签: ruby-on-rails rspec