Module: Capybara::Screenshot::RSpec

Defined in:
lib/capybara-screenshot/rspec.rb,
lib/capybara-screenshot/rspec/base_reporter.rb,
lib/capybara-screenshot/rspec/json_reporter.rb,
lib/capybara-screenshot/rspec/text_reporter.rb,
lib/capybara-screenshot/rspec/html_link_reporter.rb,
lib/capybara-screenshot/rspec/html_embed_reporter.rb,
lib/capybara-screenshot/rspec/textmate_link_reporter.rb

Defined Under Namespace

Modules: BaseReporter, HtmlEmbedReporter, HtmlLinkReporter, JsonReporter, TextMateLinkReporter, TextReporter

Constant Summary collapse

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.

{
  "RSpec::Core::Formatters::ProgressFormatter"      => Capybara::Screenshot::RSpec::TextReporter,
  "RSpec::Core::Formatters::DocumentationFormatter" => Capybara::Screenshot::RSpec::TextReporter,
  "RSpec::Core::Formatters::HtmlFormatter"          => Capybara::Screenshot::RSpec::HtmlLinkReporter,
  "RSpec::Core::Formatters::JsonFormatter"          => Capybara::Screenshot::RSpec::JsonReporter,
  "RSpec::Core::Formatters::TextMateFormatter"      => Capybara::Screenshot::RSpec::TextMateLinkReporter, # RSpec 2
  "RSpec::Mate::Formatters::TextMateFormatter"      => Capybara::Screenshot::RSpec::TextMateLinkReporter,  # RSpec 3
  "Fuubar"                                          => Capybara::Screenshot::RSpec::TextReporter
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

Returns the value of attribute add_link_to_screenshot_for_failed_examples.



50
51
52
# File 'lib/capybara-screenshot/rspec.rb', line 50

def add_link_to_screenshot_for_failed_examples
  @add_link_to_screenshot_for_failed_examples
end

Class Method Details

.after_failed_example(example) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/capybara-screenshot/rspec.rb', line 52

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.page.current_url != '' && Capybara::Screenshot.autosave_on_failure && example.exception
        filename_prefix = Capybara::Screenshot.filename_prefix_for(:rspec, example)

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

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