-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added after_save_html and after_save_screenshot callbacks
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
--------------------------------------------- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'capybara-screenshot/callbacks/integrations/saver' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'capybara-screenshot/saver' | ||
require 'capybara-screenshot/callbacks' | ||
|
||
Capybara::Screenshot::Saver.class_eval do | ||
include Capybara::Screenshot::Callbacks | ||
|
||
define_callback :after_save_html | ||
define_callback :after_save_screenshot | ||
|
||
def save_html_with_after_callback | ||
save_html_without_after_callback | ||
run_callbacks :after_save_html, html_path if html_saved? | ||
end | ||
alias_method :save_html_without_after_callback, :save_html | ||
alias_method :save_html, :save_html_with_after_callback | ||
|
||
def save_screenshot_with_after_callback | ||
save_screenshot_without_after_callback | ||
run_callbacks :after_save_screenshot, screenshot_path if screenshot_saved? | ||
end | ||
alias_method :save_screenshot_without_after_callback, :save_screenshot | ||
alias_method :save_screenshot, :save_screenshot_with_after_callback | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters