O Milli
O Milli

Reputation: 39

How to disable screenshot in rails 6 rspec app

I'm currently running a Rails 6 App with Rspec and Capybara. When running the system specs, rails automatically generates screenshots. This makes my tests slow. I would like to disable the screenshots. How do i disable screenshots?

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

Currently, the only way to disable screenshot is by including a before block like this:

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

I'm looking for a more sustainable way to disable the screenshots by default.

Upvotes: 1

Views: 472

Answers (1)

arieljuod
arieljuod

Reputation: 15838

Rails calls the take_failed_screenshot by default in the teardown phase of the tests https://github.com/rails/rails/blob/c5bf2b4736f2ddafbc477af5b9478dd7143e5466/actionpack/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb#L8

I don't see any configuration to turn that off.

The method is defined here https://github.com/rails/rails/blob/c5bf2b4736f2ddafbc477af5b9478dd7143e5466/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb#L44-L46

Maybe you can try to override that method to not take the screenshot? something like this is you use 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

or if you use RSpec:

# spec/rails_helper.rb

module NoFailedScreenshots
  def take_failed_screenshot
  end
end

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

But one comment, if these are the screenshots you refer to, then you have a problem with too many failing specs if that slows things down, you should fix the specs instead.

If this is not what's happening to you then you probably have some custom setup taking screenshots that it's not standard Rails

Upvotes: 1

Related Questions