Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

after_save_html and after_save_screenshot callbacks #171

Merged
merged 1 commit into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,30 @@ Capybara::Screenshot.prune_strategy = :keep_last_run
Capybara::Screenshot.prune_strategy = { keep: 20 }
```

Callbacks
---------

You can hook your own logic into callbacks after the html/screenshot has been saved.

```ruby
# after Saver#save_html
Capybara::Screenshot.after_save_html do |path|
mail = Mail.new do
delivery_method :sendmail
from '[email protected]'
to '[email protected]'
subject 'Capybara Screenshot'
add_file File.read path
end
mail.delivery_method :sendmail
mail.deliver
end

# after Saver#save_screenshot
Capybara::Screenhot.after_save_screenshot do |path|
# ...
end
```

Information about screenshots in RSpec output
---------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions lib/capybara-screenshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ def self.new_saver(*args)
return saver
end

def self.after_save_html &block
Saver.after_save_html &block
end

def self.after_save_screenshot &block
Saver.after_save_screenshot &block
end

private

# If the path isn't set, default to the current directory
Expand Down
44 changes: 44 additions & 0 deletions lib/capybara-screenshot/callbacks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Capybara
module Screenshot
module Callbacks
class CallbackSet < Array
def call *args
each do |callback|
callback.call *args
end
end
end

module ClassMethods
def callbacks
@callbacks ||= {}
end

def define_callback name
callbacks[name] ||= CallbackSet.new

define_singleton_method name do |&block|
callbacks[name] << block
end
end

def run_callbacks name, *args
if cb_set = callbacks[name]
cb_set.call *args
end
end
end

module InstanceMethods
def run_callbacks name, *args
self.class.run_callbacks name, *args
end
end

def self.included receiver
receiver.extend ClassMethods
receiver.send :include, InstanceMethods
end
end
end
end
9 changes: 9 additions & 0 deletions lib/capybara-screenshot/saver.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
require 'capybara-screenshot/helpers'
require 'capybara-screenshot/callbacks'

module Capybara
module Screenshot
class Saver
include Capybara::Screenshot::Callbacks

define_callback :after_save_html
define_callback :after_save_screenshot

attr_reader :capybara, :page, :file_base_name

def initialize(capybara, page, html_save=true, filename_prefix='screenshot')
@capybara, @page, @html_save = capybara, page, html_save
time_now = Time.now
Expand Down Expand Up @@ -36,6 +43,7 @@ def save_html
end
end
@html_saved = true
run_callbacks :after_save_html, html_path if html_saved?
end

def save_screenshot
Expand All @@ -47,6 +55,7 @@ def save_screenshot
}.call(page.driver, path)
@screenshot_saved = result != :not_supported
end
run_callbacks :after_save_screenshot, screenshot_path if screenshot_saved?
end

def html_path
Expand Down
34 changes: 34 additions & 0 deletions spec/unit/saver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,40 @@
end
end

describe 'callbacks' do
let(:saver) { Capybara::Screenshot::Saver.new(capybara_mock, page_mock) }

before do
allow(saver).to receive(:html_path) { 'page.html' }
allow(saver).to receive(:screenshot_path) { 'screenshot.png' }
end

before :all do
Capybara::Screenshot.after_save_html do |path|
puts "after_save_html ran with #{path}"
end
Capybara::Screenshot.after_save_screenshot do |path|
puts "after_save_screenshot ran with #{path}"
end
end

after :all do
Capybara::Screenshot::Saver.instance_eval { @callbacks = nil }
end

it 'runs after_save_html callbacks' do
expect do
saver.save
end.to output(/after_save_html ran with page\.html/).to_stdout
end

it 'runs after_save_screenshot callbacks' do
expect do
saver.save
end.to output(/after_save_screenshot ran with screenshot\.png/).to_stdout
end
end

describe "with selenium driver" do
before do
allow(capybara_mock).to receive(:current_driver).and_return(:selenium)
Expand Down