capybara-screenshot 0.3.19 → 0.3.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/CHANGELOG.md +8 -0
- data/README.md +16 -6
- data/lib/capybara-screenshot.rb +9 -3
- data/lib/capybara-screenshot/rspec.rb +72 -12
- data/lib/capybara-screenshot/rspec/base_reporter.rb +21 -0
- data/lib/capybara-screenshot/rspec/html_embed_reporter.rb +25 -0
- data/lib/capybara-screenshot/rspec/html_link_reporter.rb +37 -0
- data/lib/capybara-screenshot/rspec/text_reporter.rb +21 -0
- data/lib/capybara-screenshot/rspec/textmate_link_reporter.rb +19 -0
- data/lib/capybara-screenshot/version.rb +1 -1
- data/spec/capybara-screenshot/saver_spec.rb +11 -1
- data/spec/capybara-screenshot_spec.rb +17 -0
- data/spec/rspec/base_reporter_spec.rb +25 -0
- data/spec/rspec/html_embed_reporter_spec.rb +18 -0
- data/spec/rspec/html_link_reporter_spec.rb +27 -0
- data/spec/rspec/text_reporter_spec.rb +76 -0
- data/spec/rspec/textmate_link_reporter_spec.rb +37 -0
- data/spec/rspec_spec.rb +43 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/support/html_reporter_context.rb +28 -0
- metadata +21 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZWU2OWY4YzU2ZjQyYjIwNWM0MGEzOTA3Mjg4YjFiMTY5NTA1NTdhYQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NmU0ZjcwMTYyMzlmOGYwYzYyODI1YmZlNjg1OGExMmI3ZTM0NDgyYw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NTRiMDIxYmQ1NzkwNTIzM2RkOGMzNzE0MDJiMGE1OGM3MTk3ZDc1YmNiZDYx
|
10
|
+
ZTVhNDI0MjM0OGQ0YWQ0YTIyOWQ4M2Y5MWJmMGVjYmRmODZkMTYxOWIxNjQ5
|
11
|
+
MzRlZDI5NDIwMmI3NzE0ZDU5ZTE5NGI0ZmEyNTZlNmFlZjE4NzU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YzhhMjc5MTQ1MGNlNjU4YWE2ZmZjNzNiNDAwM2I3M2I2ZDM3MTk5ODk3ZjVh
|
14
|
+
YTM3NjE5Yjk1MDBlMDM3Yjk3ODYyY2E5NTk0NDAxMTQ1N2RkNmIzMDZiMmU3
|
15
|
+
NGY5ZjI2ZmVlYzA1MzczN2E4ZDkyMjAzYjZhYzk1MzYzNmVjOGY=
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
11 July 2014 - 0.3.19 -> 0.3.20
|
2
|
+
-----------
|
3
|
+
|
4
|
+
* Added reporters to improve screenshot info in RSpec output
|
5
|
+
* Added support for Webkit options such as width and height
|
6
|
+
|
7
|
+
Thanks to https://github.com/multiplegeorges and https://github.com/noniq
|
8
|
+
|
1
9
|
2 April 2014 - 0.3.18 -> 0.3.19
|
2
10
|
-----------
|
3
11
|
|
data/README.md
CHANGED
@@ -32,6 +32,7 @@ For **Cucumber**, in env.rb or a support file, please add:
|
|
32
32
|
For **RSpec**, in spec_helper.rb or a support file, after the require for 'capybara/rspec', please add:
|
33
33
|
|
34
34
|
# you should require 'capybara/rspec' first
|
35
|
+
require 'capybara-screenshot'
|
35
36
|
require 'capybara-screenshot/rspec'
|
36
37
|
|
37
38
|
For **Minitest**, typically in 'test/test_helper.rb', please add:
|
@@ -75,6 +76,10 @@ The gem supports the default rendering method for Capybara to generate the scree
|
|
75
76
|
There are also some specific driver configurations for Selenium, Webkit, and Poltergeist. See [the definitions here](https://github.com/mattheworiordan/capybara-screenshot/blob/master/lib/capybara-screenshot.rb). The Rack::Test driver, Rails' default, does not allow
|
76
77
|
rendering, so it has a driver definition as a noop.
|
77
78
|
|
79
|
+
Capybara-webkit defaults to a screenshot size of 1000px by 10px. To specify a custom size, use the following option:
|
80
|
+
|
81
|
+
Capybara::Screenshot.webkit_options = {width: 1024, height: 768}
|
82
|
+
|
78
83
|
If a driver is not found the default rendering will be used. If this doesn't work with your driver, then you can
|
79
84
|
add another driver configuration like so
|
80
85
|
|
@@ -106,14 +111,19 @@ By default screenshots are saved to the current working directory. If you want t
|
|
106
111
|
Capybara.save_and_open_page_path = "/file/path"
|
107
112
|
|
108
113
|
|
109
|
-
|
110
|
-
|
114
|
+
Information about screenshots in RSpec output
|
115
|
+
---------------------------------------------
|
116
|
+
|
117
|
+
By default, capybara-screenshot extend RSpec’s formatters to include a link to the screenshot and/or saved html page for each failed spec. If you want to disable this feature completely (eg. to avoid problems with CI tools), use:
|
118
|
+
|
119
|
+
Capybara::Screenshot::RSpec.add_link_to_screenshot_for_failed_examples = false
|
120
|
+
|
121
|
+
It’s also possible to directly embed the screenshot image in the output if you’re using RSpec’s HtmlFormatter:
|
122
|
+
|
123
|
+
Capybara::Screenshot::RSpec::REPORTERS["RSpec::Core::Formatters::HtmlFormatter"] = Capybara::Screenshot::RSpec::HtmlEmbedReporter
|
111
124
|
|
112
|
-
|
113
|
-
changes the name of the spec and breaks trend reporting using tools like CI Reporter. You can turn off the default behavior
|
114
|
-
with the following invocation
|
125
|
+
If you want to further customize the information added to RSpec’s output, just implement your own reporter class and customize `Capybara::Screenshot::RSpec::REPORTERS` accordingly. See [rspec.rb](lib/capybara-screenshot/rspec.rb) for more info.
|
115
126
|
|
116
|
-
Capybara::Screenshot.append_screenshot_path = false
|
117
127
|
|
118
128
|
Example application
|
119
129
|
-------------------
|
data/lib/capybara-screenshot.rb
CHANGED
@@ -4,15 +4,21 @@ module Capybara
|
|
4
4
|
attr_accessor :autosave_on_failure
|
5
5
|
attr_accessor :registered_drivers
|
6
6
|
attr_accessor :filename_prefix_formatters
|
7
|
-
attr_accessor :append_screenshot_path
|
8
7
|
attr_accessor :append_timestamp
|
8
|
+
attr_accessor :webkit_options
|
9
9
|
end
|
10
10
|
|
11
11
|
self.autosave_on_failure = true
|
12
12
|
self.registered_drivers = {}
|
13
13
|
self.filename_prefix_formatters = {}
|
14
|
-
self.append_screenshot_path = true
|
15
14
|
self.append_timestamp = true
|
15
|
+
self.webkit_options = {}
|
16
|
+
|
17
|
+
def self.append_screenshot_path=(value)
|
18
|
+
$stderr.puts "WARNING: Capybara::Screenshot.append_screenshot_path is deprecated. " +
|
19
|
+
"Please use Capybara::Screenshot::RSpec.add_link_to_screenshot_for_failed_examples instead."
|
20
|
+
RSpec.add_link_to_screenshot_for_failed_examples = value
|
21
|
+
end
|
16
22
|
|
17
23
|
def self.screenshot_and_save_page
|
18
24
|
saver = Saver.new(Capybara, Capybara.page)
|
@@ -91,7 +97,7 @@ Capybara::Screenshot.class_eval do
|
|
91
97
|
|
92
98
|
register_driver(:webkit) do |driver, path|
|
93
99
|
if driver.respond_to?(:save_screenshot)
|
94
|
-
driver.save_screenshot(path)
|
100
|
+
driver.save_screenshot(path, webkit_options)
|
95
101
|
else
|
96
102
|
driver.render(path)
|
97
103
|
end
|
@@ -1,21 +1,81 @@
|
|
1
|
+
require "capybara-screenshot/rspec/text_reporter"
|
2
|
+
require "capybara-screenshot/rspec/html_link_reporter"
|
3
|
+
require "capybara-screenshot/rspec/html_embed_reporter"
|
4
|
+
require "capybara-screenshot/rspec/textmate_link_reporter"
|
5
|
+
|
6
|
+
module Capybara
|
7
|
+
module Screenshot
|
8
|
+
module RSpec
|
9
|
+
|
10
|
+
# Reporters extend RSpec formatters to display information about screenshots for failed
|
11
|
+
# examples.
|
12
|
+
#
|
13
|
+
# Technically, a reporter is a module that gets injected into a RSpec formatter class.
|
14
|
+
# It uses method aliasing to extend some (usually just one) of the formatter's methods.
|
15
|
+
#
|
16
|
+
# Implementing a custom reporter is as simple as creating a module and setting up the
|
17
|
+
# appropriate aliases. Use `BaseReporter.enhance_with_screenshot` if you don't want
|
18
|
+
# to set up the aliases manually:
|
19
|
+
#
|
20
|
+
# module MyReporter
|
21
|
+
# extend Capybara::Screenshot::RSpec::BaseReporter
|
22
|
+
#
|
23
|
+
# # Will replace the formatter's original `dump_failure_info` method with
|
24
|
+
# # `dump_failure_info_with_screenshot` from this module:
|
25
|
+
# enhance_with_screenshot :dump_failure_info
|
26
|
+
#
|
27
|
+
# def dump_failure_info_with_screenshot(example)
|
28
|
+
# dump_failure_info_without_screenshot(example) # call original implementation
|
29
|
+
# ... # your additions here
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# Finally customize `Capybara::Screenshot::RSpec::FORMATTERS` to make sure your reporter
|
34
|
+
# gets injected into the appropriate formatter.
|
35
|
+
|
36
|
+
REPORTERS = {
|
37
|
+
"RSpec::Core::Formatters::ProgressFormatter" => Capybara::Screenshot::RSpec::TextReporter,
|
38
|
+
"RSpec::Core::Formatters::HtmlFormatter" => Capybara::Screenshot::RSpec::HtmlLinkReporter,
|
39
|
+
"RSpec::Core::Formatters::TextMateFormatter" => Capybara::Screenshot::RSpec::TextMateLinkReporter, # RSpec 2
|
40
|
+
"RSpec::Mate::Formatters::TextMateFormatter" => Capybara::Screenshot::RSpec::TextMateLinkReporter # RSpec 3
|
41
|
+
}
|
42
|
+
|
43
|
+
class << self
|
44
|
+
attr_accessor :add_link_to_screenshot_for_failed_examples
|
45
|
+
|
46
|
+
def after_failed_example(example)
|
47
|
+
if Capybara.page.respond_to?(:save_page) # Capybara DSL method has been included for a feature we can snapshot
|
48
|
+
if Capybara.page.current_url != '' && Capybara::Screenshot.autosave_on_failure && example.exception
|
49
|
+
filename_prefix = Capybara::Screenshot.filename_prefix_for(:rspec, example)
|
50
|
+
saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
|
51
|
+
saver.save
|
52
|
+
|
53
|
+
example.metadata[:screenshot] = {}
|
54
|
+
example.metadata[:screenshot][:html] = saver.html_path if saver.html_saved?
|
55
|
+
example.metadata[:screenshot][:image] = saver.screenshot_path if saver.screenshot_saved?
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
self.add_link_to_screenshot_for_failed_examples = true
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
1
66
|
RSpec.configure do |config|
|
2
|
-
# use the before hook to add an after hook that runs last
|
3
67
|
config.after do |example_from_block_arg|
|
4
|
-
|
5
68
|
# RSpec 3 no longer defines `example`, but passes the example as block argument instead
|
6
69
|
example = config.respond_to?(:expose_current_running_example_as) ? example_from_block_arg : self.example
|
7
70
|
|
8
|
-
|
9
|
-
|
10
|
-
filename_prefix = Capybara::Screenshot.filename_prefix_for(:rspec, example)
|
11
|
-
|
12
|
-
saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
|
13
|
-
saver.save
|
71
|
+
Capybara::Screenshot::RSpec.after_failed_example(example)
|
72
|
+
end
|
14
73
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
74
|
+
config.before(:suite) do
|
75
|
+
if Capybara::Screenshot::RSpec.add_link_to_screenshot_for_failed_examples
|
76
|
+
RSpec.configuration.formatters.each do |formatter|
|
77
|
+
next unless (reporter_module = Capybara::Screenshot::RSpec::REPORTERS[formatter.class.to_s])
|
78
|
+
formatter.singleton_class.send :include, reporter_module
|
19
79
|
end
|
20
80
|
end
|
21
81
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Capybara
|
2
|
+
module Screenshot
|
3
|
+
module RSpec
|
4
|
+
module BaseReporter
|
5
|
+
|
6
|
+
# Automatically set up method aliases (very much like ActiveSupport's `alias_method_chain`)
|
7
|
+
# when the module gets included.
|
8
|
+
def enhance_with_screenshot(method)
|
9
|
+
with_method, without_method = "#{method}_with_screenshot", "#{method}_without_screenshot"
|
10
|
+
define_singleton_method :included do |mod|
|
11
|
+
if mod.method_defined?(method) || mod.private_method_defined?(method)
|
12
|
+
mod.send :alias_method, without_method, method
|
13
|
+
mod.send :alias_method, method, with_method
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'capybara-screenshot/rspec/base_reporter'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
module Capybara
|
5
|
+
module Screenshot
|
6
|
+
module RSpec
|
7
|
+
module HtmlEmbedReporter
|
8
|
+
extend BaseReporter
|
9
|
+
enhance_with_screenshot :extra_failure_content
|
10
|
+
|
11
|
+
def extra_failure_content_with_screenshot(exception)
|
12
|
+
result = extra_failure_content_without_screenshot(exception)
|
13
|
+
example = @failed_examples.last
|
14
|
+
# Ignores saved html file, only saved image will be embedded (if present)
|
15
|
+
if (screenshot = example.metadata[:screenshot]) && screenshot[:image]
|
16
|
+
image = File.binread(screenshot[:image])
|
17
|
+
encoded_img = Base64.encode64(image)
|
18
|
+
result += "<img src='data:image/png;base64,#{encoded_img}' style='display: block'>"
|
19
|
+
end
|
20
|
+
result
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'capybara-screenshot/rspec/base_reporter'
|
2
|
+
require 'cgi'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module Capybara
|
6
|
+
module Screenshot
|
7
|
+
module RSpec
|
8
|
+
module HtmlLinkReporter
|
9
|
+
extend BaseReporter
|
10
|
+
enhance_with_screenshot :extra_failure_content
|
11
|
+
|
12
|
+
def extra_failure_content_with_screenshot(exception)
|
13
|
+
result = extra_failure_content_without_screenshot(exception)
|
14
|
+
example = @failed_examples.last
|
15
|
+
if (screenshot = example.metadata[:screenshot])
|
16
|
+
result << "<p>Saved files: "
|
17
|
+
result << link_to_screenshot("HTML page", screenshot[:html]) if screenshot[:html]
|
18
|
+
result << link_to_screenshot("Screenshot", screenshot[:image]) if screenshot[:image]
|
19
|
+
result << "</p>"
|
20
|
+
end
|
21
|
+
result
|
22
|
+
end
|
23
|
+
|
24
|
+
def link_to_screenshot(title, path)
|
25
|
+
url = URI.escape("file://#{path}")
|
26
|
+
title = CGI.escape_html(title)
|
27
|
+
attributes = attributes_for_screenshot_link(url).map { |name, val| %{#{name}="#{CGI.escape_html(val)}"} }.join(" ")
|
28
|
+
"<a #{attributes}>#{title}</a>"
|
29
|
+
end
|
30
|
+
|
31
|
+
def attributes_for_screenshot_link(url)
|
32
|
+
{"href" => url, "style" => "margin-right: 10px; font-weight: bold"}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'capybara-screenshot/rspec/base_reporter'
|
2
|
+
|
3
|
+
module Capybara
|
4
|
+
module Screenshot
|
5
|
+
module RSpec
|
6
|
+
module TextReporter
|
7
|
+
extend BaseReporter
|
8
|
+
enhance_with_screenshot :dump_failure_info
|
9
|
+
|
10
|
+
def dump_failure_info_with_screenshot(example)
|
11
|
+
dump_failure_info_without_screenshot(example)
|
12
|
+
return unless (screenshot = example.metadata[:screenshot])
|
13
|
+
|
14
|
+
colorize = lambda { |str| respond_to?(:failure_color, true) ? failure_color(str) : red(str) }
|
15
|
+
output.puts(long_padding + colorize["HTML page: #{screenshot[:html]}"]) if screenshot[:html]
|
16
|
+
output.puts(long_padding + colorize["Screenshot: #{screenshot[:image]}"]) if screenshot[:image]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'capybara-screenshot/rspec/base_reporter'
|
2
|
+
require 'capybara-screenshot/rspec/html_link_reporter'
|
3
|
+
require 'shellwords'
|
4
|
+
|
5
|
+
module Capybara
|
6
|
+
module Screenshot
|
7
|
+
module RSpec
|
8
|
+
module TextMateLinkReporter
|
9
|
+
extend BaseReporter
|
10
|
+
include HtmlLinkReporter
|
11
|
+
enhance_with_screenshot :extra_failure_content
|
12
|
+
|
13
|
+
def attributes_for_screenshot_link(url)
|
14
|
+
super.merge("onclick" => "TextMate.system('open #{Shellwords.escape(url)}'); return false;")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -166,12 +166,22 @@ describe Capybara::Screenshot::Saver do
|
|
166
166
|
end
|
167
167
|
|
168
168
|
context 'has save_screenshot method' do
|
169
|
+
let(:webkit_options){ {width: 800, height: 600} }
|
170
|
+
|
169
171
|
before do
|
170
172
|
driver_mock.stub(:respond_to?).with(:'save_screenshot').and_return(true)
|
171
173
|
end
|
172
174
|
|
173
175
|
it 'should save driver render' do
|
174
|
-
driver_mock.should_receive(:save_screenshot).with(screenshot_path)
|
176
|
+
driver_mock.should_receive(:save_screenshot).with(screenshot_path, {})
|
177
|
+
|
178
|
+
saver.save
|
179
|
+
saver.screenshot_saved?.should be_true
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'should pass webkit_options to driver' do
|
183
|
+
Capybara::Screenshot.stub(:webkit_options).and_return( webkit_options )
|
184
|
+
driver_mock.should_receive(:save_screenshot).with(screenshot_path, webkit_options)
|
175
185
|
|
176
186
|
saver.save
|
177
187
|
saver.screenshot_saved?.should be_true
|
@@ -40,4 +40,21 @@ describe Capybara::Screenshot do
|
|
40
40
|
Capybara::Screenshot.filename_prefix_for(:foo, double('test')).should eq 'screenshot'
|
41
41
|
end
|
42
42
|
end
|
43
|
+
|
44
|
+
describe '.append_screenshot_path' do
|
45
|
+
it 'prints a deprecation message and delegates to RSpec.add_link_to_screenshot_for_failed_examples' do
|
46
|
+
begin
|
47
|
+
original_stderr = $stderr
|
48
|
+
$stderr = StringIO.new
|
49
|
+
expect {
|
50
|
+
Capybara::Screenshot.append_screenshot_path = false
|
51
|
+
}.to change {
|
52
|
+
Capybara::Screenshot::RSpec.add_link_to_screenshot_for_failed_examples
|
53
|
+
}.from(true).to(false)
|
54
|
+
$stderr.string.should include("append_screenshot_path is deprecated")
|
55
|
+
ensure
|
56
|
+
$stderr = original_stderr
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
43
60
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Capybara::Screenshot::RSpec::BaseReporter do
|
4
|
+
describe '#enhance_with_screenshot' do
|
5
|
+
it 'makes the original method available under an alias and replaces it with the enhanced method' do
|
6
|
+
reporter_module = Module.new do
|
7
|
+
extend Capybara::Screenshot::RSpec::BaseReporter
|
8
|
+
enhance_with_screenshot :foo
|
9
|
+
def foo_with_screenshot
|
10
|
+
[foo_without_screenshot, :enhanced]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
klass = Class.new do
|
15
|
+
def foo
|
16
|
+
:original
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
klass.new.foo.should == :original
|
21
|
+
klass.send :include, reporter_module
|
22
|
+
klass.new.foo.should == [:original, :enhanced]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Capybara::Screenshot::RSpec::HtmlEmbedReporter do
|
4
|
+
include_context 'html reporter'
|
5
|
+
|
6
|
+
context 'when an image was saved' do
|
7
|
+
before do
|
8
|
+
set_example double("example", metadata: {screenshot: {image: "path/to/image"}})
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'embeds the image base64 encoded into the content' do
|
12
|
+
File.should_receive(:binread).with("path/to/image").and_return("image data")
|
13
|
+
encoded_image_data = Base64.encode64('image data')
|
14
|
+
content_without_styles = @reporter.extra_failure_content(nil).gsub(/ ?style='.*?' ?/, "")
|
15
|
+
content_without_styles.should == "original content<img src='data:image/png;base64,#{encoded_image_data}'>"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Capybara::Screenshot::RSpec::HtmlLinkReporter do
|
4
|
+
include_context 'html reporter'
|
5
|
+
|
6
|
+
context 'when a html file was saved' do
|
7
|
+
before do
|
8
|
+
set_example double("example", metadata: {screenshot: {html: "path/to/a html file"}})
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'appends a link to the html to the original content' do
|
12
|
+
content_without_styles = @reporter.extra_failure_content(nil).gsub(/ ?style=".*?" ?/, "")
|
13
|
+
content_without_styles.should == %{original content<p>Saved files: <a href="file://path/to/a%20html%20file">HTML page</a></p>}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when a html file and an image were saved' do
|
18
|
+
before do
|
19
|
+
set_example double("example", metadata: {screenshot: {html: "path/to/html", image: "path/to/an image"}})
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'appends links to both files to the original content' do
|
23
|
+
content_without_styles = @reporter.extra_failure_content(nil).gsub(/ ?style=".*?" ?/, "")
|
24
|
+
content_without_styles.should == %{original content<p>Saved files: <a href="file://path/to/html">HTML page</a><a href="file://path/to/an%20image">Screenshot</a></p>}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Capybara::Screenshot::RSpec::TextReporter do
|
4
|
+
before do
|
5
|
+
# Mocking `RSpec::Core::Formatters::ProgressFormatter`, but only implementing the methods that
|
6
|
+
# are actually used in `TextReporter#dump_failure_info_with_screenshot`.
|
7
|
+
@reporter_class = Class.new do
|
8
|
+
attr_reader :output
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@output = StringIO.new
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
|
16
|
+
def long_padding
|
17
|
+
" "
|
18
|
+
end
|
19
|
+
|
20
|
+
def failure_color(str)
|
21
|
+
"colorized(#{str})"
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def dump_failure_info(example)
|
27
|
+
output.puts "original failure info"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
@reporter = @reporter_class.new
|
32
|
+
@reporter.singleton_class.send :include, described_class
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when there is no screenshot' do
|
36
|
+
let(:example) { double("example", metadata: {}) }
|
37
|
+
|
38
|
+
it 'doesnt change the original output of the reporter' do
|
39
|
+
@reporter.dump_failure_info(example)
|
40
|
+
@reporter.output.string.should == "original failure info\n"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when a html file was saved' do
|
45
|
+
let(:example) { double("example", metadata: {screenshot: {html: "path/to/html"}}) }
|
46
|
+
|
47
|
+
it 'appends the html file path to the original output' do
|
48
|
+
@reporter.dump_failure_info(example)
|
49
|
+
@reporter.output.string.should == "original failure info\n colorized(HTML page: path/to/html)\n"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'when a html file and an image were saved' do
|
54
|
+
let(:example) { double("example", metadata: {screenshot: {html: "path/to/html", image: "path/to/image"}}) }
|
55
|
+
|
56
|
+
it 'appends the image path to the original output' do
|
57
|
+
@reporter.dump_failure_info(example)
|
58
|
+
@reporter.output.string.should == "original failure info\n colorized(HTML page: path/to/html)\n colorized(Screenshot: path/to/image)\n"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
it 'works with older RSpec formatters where `#red` is used instead of `#failure_color`' do
|
64
|
+
old_reporter_class = Class.new(@reporter_class) do
|
65
|
+
undef_method :failure_color
|
66
|
+
def red(str)
|
67
|
+
"red(#{str})"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
old_reporter = old_reporter_class.new
|
71
|
+
old_reporter.singleton_class.send :include, described_class
|
72
|
+
example = double("example", metadata: {screenshot: {html: "path/to/html"}})
|
73
|
+
old_reporter.dump_failure_info(example)
|
74
|
+
old_reporter.output.string.should == "original failure info\n red(HTML page: path/to/html)\n"
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Capybara::Screenshot::RSpec::TextMateLinkReporter do
|
4
|
+
include_context 'html reporter'
|
5
|
+
|
6
|
+
context 'when a html file was saved' do
|
7
|
+
before do
|
8
|
+
set_example double("example", metadata: {screenshot: {html: "path/to/a html file"}})
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'appends a link to the html to the original content' do
|
12
|
+
content_without_styles = @reporter.extra_failure_content(nil).gsub(/ ?style=".*?"/, "")
|
13
|
+
# Single quotes are handled differently by CGI.escape_html in Ruby 1.9 / Ruby 2, so to be
|
14
|
+
# compatible with both versions we can't hard code the final escaped string.
|
15
|
+
expected_onclick_handler = CGI.escape_html("TextMate.system('open file://path/to/a\\%20html\\%20file'); return false;")
|
16
|
+
content_without_styles.should == %{original content<p>} +
|
17
|
+
%{Saved files: <a href="file://path/to/a%20html%20file" onclick="#{expected_onclick_handler}">HTML page</a></p>}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when a html file and an image were saved' do
|
22
|
+
before do
|
23
|
+
set_example double("example", metadata: {screenshot: {html: "path/to/html", image: "path/to/an image"}})
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'appends links to both files to the original content' do
|
27
|
+
content_without_styles = @reporter.extra_failure_content(nil).gsub(/ ?style=".*?"/, "")
|
28
|
+
# Single quotes are handled differently by CGI.escape_html in Ruby 1.9 / Ruby 2, so to be
|
29
|
+
# compatible with both versions we can't hard code the final escaped string.
|
30
|
+
expected_onclick_handler_1 = CGI.escape_html("TextMate.system('open file://path/to/html'); return false;")
|
31
|
+
expected_onclick_handler_2 = CGI.escape_html("TextMate.system('open file://path/to/an\\%20image'); return false;")
|
32
|
+
content_without_styles.should == %{original content<p>} +
|
33
|
+
%{Saved files: <a href="file://path/to/html" onclick="#{expected_onclick_handler_1}">HTML page</a>} +
|
34
|
+
%{<a href="file://path/to/an%20image" onclick="#{expected_onclick_handler_2}">Screenshot</a></p>}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/spec/rspec_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Capybara::Screenshot::RSpec do
|
4
|
+
describe '.after_failed_example' do
|
5
|
+
context 'for a failed example in a feature that can be snapshotted' do
|
6
|
+
before do
|
7
|
+
Capybara.page.stub(save_page: nil, current_url: "http://test.local")
|
8
|
+
Capybara::Screenshot::Saver.stub(new: mock_saver)
|
9
|
+
end
|
10
|
+
let(:example) { double("example", exception: Exception.new, metadata: {}) }
|
11
|
+
let(:mock_saver) { Capybara::Screenshot::Saver.new(Capybara, Capybara.page).tap { |saver| saver.stub(:save) } }
|
12
|
+
|
13
|
+
it 'instantiates a saver and calls `save` on it' do
|
14
|
+
mock_saver.should_receive(:save)
|
15
|
+
described_class.after_failed_example(example)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'extends the metadata with an empty hash for screenshot metadata' do
|
19
|
+
described_class.after_failed_example(example)
|
20
|
+
example.metadata.should have_key(:screenshot)
|
21
|
+
example.metadata[:screenshot].should == {}
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when a html file gets saved' do
|
25
|
+
before { mock_saver.stub(:html_saved? => true) }
|
26
|
+
|
27
|
+
it 'adds the html file path to the screenshot metadata' do
|
28
|
+
described_class.after_failed_example(example)
|
29
|
+
example.metadata[:screenshot][:html].should start_with("./screenshot")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when an image gets saved' do
|
34
|
+
before { mock_saver.stub(:screenshot_saved? => true) }
|
35
|
+
|
36
|
+
it 'adds the image path to the screenshot metadata' do
|
37
|
+
described_class.after_failed_example(example)
|
38
|
+
example.metadata[:screenshot][:image].should start_with("./screenshot")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -10,6 +10,8 @@ require 'capybara-screenshot'
|
|
10
10
|
require 'capybara-screenshot/rspec'
|
11
11
|
require 'timecop'
|
12
12
|
|
13
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
14
|
+
|
13
15
|
RSpec.configure do |config|
|
14
16
|
config.treat_symbols_as_metadata_keys_with_true_values = true
|
15
17
|
config.run_all_when_everything_filtered = true
|
@@ -0,0 +1,28 @@
|
|
1
|
+
shared_context 'html reporter' do
|
2
|
+
def set_example(example)
|
3
|
+
@reporter.instance_variable_set :@failed_examples, [example]
|
4
|
+
end
|
5
|
+
|
6
|
+
before do
|
7
|
+
# Mocking `RSpec::Core::Formatters::HtmlFormatter`, but only implementing the things that
|
8
|
+
# are actually used in `HtmlLinkReporter#extra_failure_content_with_screenshot`.
|
9
|
+
@reporter_class = Class.new do
|
10
|
+
def extra_failure_content(exception)
|
11
|
+
"original content"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
@reporter = @reporter_class.new
|
16
|
+
@reporter.singleton_class.send :include, described_class
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when there is no screenshot' do
|
20
|
+
before do
|
21
|
+
set_example double("example", metadata: {})
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'doesnt change the original content of the reporter' do
|
25
|
+
@reporter.extra_failure_content(nil).should == "original content"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capybara-screenshot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.20
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew O'Riordan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: capybara
|
@@ -94,6 +94,11 @@ files:
|
|
94
94
|
- lib/capybara-screenshot/cucumber.rb
|
95
95
|
- lib/capybara-screenshot/minitest.rb
|
96
96
|
- lib/capybara-screenshot/rspec.rb
|
97
|
+
- lib/capybara-screenshot/rspec/base_reporter.rb
|
98
|
+
- lib/capybara-screenshot/rspec/html_embed_reporter.rb
|
99
|
+
- lib/capybara-screenshot/rspec/html_link_reporter.rb
|
100
|
+
- lib/capybara-screenshot/rspec/text_reporter.rb
|
101
|
+
- lib/capybara-screenshot/rspec/textmate_link_reporter.rb
|
97
102
|
- lib/capybara-screenshot/saver.rb
|
98
103
|
- lib/capybara-screenshot/spinach.rb
|
99
104
|
- lib/capybara-screenshot/testunit.rb
|
@@ -101,7 +106,14 @@ files:
|
|
101
106
|
- spec/capybara-screenshot/capybara_spec.rb
|
102
107
|
- spec/capybara-screenshot/saver_spec.rb
|
103
108
|
- spec/capybara-screenshot_spec.rb
|
109
|
+
- spec/rspec/base_reporter_spec.rb
|
110
|
+
- spec/rspec/html_embed_reporter_spec.rb
|
111
|
+
- spec/rspec/html_link_reporter_spec.rb
|
112
|
+
- spec/rspec/text_reporter_spec.rb
|
113
|
+
- spec/rspec/textmate_link_reporter_spec.rb
|
114
|
+
- spec/rspec_spec.rb
|
104
115
|
- spec/spec_helper.rb
|
116
|
+
- spec/support/html_reporter_context.rb
|
105
117
|
homepage: http://github.com/mattheworiordan/capybara-screenshot
|
106
118
|
licenses:
|
107
119
|
- MIT
|
@@ -131,4 +143,11 @@ test_files:
|
|
131
143
|
- spec/capybara-screenshot/capybara_spec.rb
|
132
144
|
- spec/capybara-screenshot/saver_spec.rb
|
133
145
|
- spec/capybara-screenshot_spec.rb
|
146
|
+
- spec/rspec/base_reporter_spec.rb
|
147
|
+
- spec/rspec/html_embed_reporter_spec.rb
|
148
|
+
- spec/rspec/html_link_reporter_spec.rb
|
149
|
+
- spec/rspec/text_reporter_spec.rb
|
150
|
+
- spec/rspec/textmate_link_reporter_spec.rb
|
151
|
+
- spec/rspec_spec.rb
|
134
152
|
- spec/spec_helper.rb
|
153
|
+
- spec/support/html_reporter_context.rb
|