mattheworiordan / capybara-screenshot

Automatically save screen shots when a Capybara scenario fails
MIT License
1.02k stars 168 forks source link

Capybara.page not in scope #263

Closed shamca65 closed 5 years ago

shamca65 commented 5 years ago

I'm trying to override the after_failed_example method so I can inflict some custom file naming on our screenshots and provide the option to turn html file saving off. I'm loading the module as an initializer. So far, so good, but the Capybara.page.current_url is blank, making me think I need to require something additional?

require "capybara-screenshot/rspec"
module Capybara
  module Screenshot
    module RSpec

      class << self
        attr_accessor :use_description_as_filename
        attr_accessor :save_html_file
      end

      self.use_description_as_filename = true
      self.save_html_file = true

      def self.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
            puts ">>>> Capybara.page.current_url: " + Capybara.page.current_url.to_s
            if Capybara::Screenshot.autosave_on_failure && failed?(example) && Capybara.page.current_url != ''
              saver = Capybara::Screenshot.new_saver(Capybara, Capybara.page, Capybara::Screenshot.save_html_file?, set_saver_filename_prefix(example))
              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

      private

        def self.set_saver_filename_prefix(example)
          return example.description.to_s.gsub(" ", "-") if Capybara::Screenshot.use_description_as_filename?
          return Capybara::Screenshot.filename_prefix_for(:rspec, example)
        end
      end
    end
  end
end
shamca65 commented 5 years ago

Silly oversight. Needed to include : require 'capybara/rspec'