dfaulken
dfaulken

Reputation: 516

Turn off screenshots in Rails system tests

I'm using system tests in Rails 5.1, and I'd like to turn off the automated screenshots for failure cases. Usually the failure message is plenty for me to figure out what's going on — and notably, the webdriver browser window always takes focus in order to take a screenshot, which is annoying when I'm working on the code (or trying to do anything else).

Is there a built in way in Capybara configuration to turn off screenshots? I looked through the docs and couldn't find anything stated explicitly, but since this is a new-ish addition to mainstream Rails, I figure that doesn't necessarily mean it doesn't exist.

Upvotes: 2

Views: 1637

Answers (4)

Samuel Frost
Samuel Frost

Reputation: 25

I really feel like there should be an option for this somewhere, but it still seems to be an issue in rails 7.

Here's my solution (put this in your spec_helper.rb or appropriate location to configure your rspec). You can put this inside a conditional or call super inside the method if some condition is met.

  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

Upvotes: 0

Tomasz Mazur
Tomasz Mazur

Reputation: 1015

I think it is more elegant to override supports_screenshot? method, In my case I wanted to disable screenshots on 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

Upvotes: 1

Thomas Walpole
Thomas Walpole

Reputation: 49900

In Rails 5.1 ActionDispatch::SystemTestCase (from which ApplicationSystemTestCase derives) has a default after_teardown method of

def after_teardown
  take_failed_screenshot
  Capybara.reset_sessions!
  super
end

To stop this from taking a screenshot, the easiest method is to override take_failed_screenshot in your ApplicationSystemTestCase class

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by ...

  # Add this method
  def take_failed_screenshot
    false
  end
end

Upvotes: 5

user2139714
user2139714

Reputation:

Capybara::Screenshot.autosave_on_failure = false

I think that will solve your problem.

Try to find file where you are importing this library. Or find capybara-screenshot/lib/capybara-screenshot.rb and change self.autosave_on_failure = true to false

Upvotes: -1

Related Questions