module Capybara::Screenshot::RSpec

Constants

REPORTERS

Reporters extend RSpec formatters to display information about screenshots for failed examples.

Technically, a reporter is a module that gets injected into a RSpec formatter class. It uses method aliasing to extend some (usually just one) of the formatter's methods.

Implementing a custom reporter is as simple as creating a module and setting up the appropriate aliases. Use `BaseReporter.enhance_with_screenshot` if you don't want to set up the aliases manually:

module MyReporter
  extend Capybara::Screenshot::RSpec::BaseReporter

  # Will replace the formatter's original `dump_failure_info` method with
  # `dump_failure_info_with_screenshot` from this module:
  enhance_with_screenshot :dump_failure_info

  def dump_failure_info_with_screenshot(example)
    dump_failure_info_without_screenshot(example) # call original implementation
    ... # your additions here
  end
end

Finally customize `Capybara::Screenshot::RSpec::FORMATTERS` to make sure your reporter gets injected into the appropriate formatter.

Attributes

Public Class Methods

after_failed_example(example) click to toggle source
# File lib/capybara-screenshot/rspec.rb, line 53
def after_failed_example(example)
  if example.example_group.include?(Capybara::DSL) # Capybara DSL method has been included for a feature we can snapshot
    Capybara.using_session(Capybara::Screenshot.final_session_name) do
      if Capybara::Screenshot.autosave_on_failure && failed?(example) && Capybara.page.current_url != ''
        filename_prefix = Capybara::Screenshot.filename_prefix_for(:rspec, example)

        saver = Capybara::Screenshot.new_saver(Capybara, Capybara.page, true, filename_prefix)
        saver.save

        example.metadata[:screenshot] = {}
        example.metadata[:screenshot][:html]  = saver.html_path if saver.html_saved?
        example.metadata[:screenshot][:image] = saver.screenshot_path if saver.screenshot_saved?
      end
    end
  end
end

Private Class Methods

failed?(example) click to toggle source
# File lib/capybara-screenshot/rspec.rb, line 72
def failed?(example)
  return true if example.exception
  return false unless defined?(::RSpec::Expectations::FailureAggregator)

  failure_notifier = ::RSpec::Support.failure_notifier
  return false unless failure_notifier.is_a?(::RSpec::Expectations::FailureAggregator)

  failure_notifier.failures.any? || failure_notifier.other_errors.any?
end