capybara-screenshot-nocolor 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +17 -0
  5. data/Appraisals +31 -0
  6. data/CHANGELOG.md +198 -0
  7. data/Gemfile +7 -0
  8. data/LICENSE +19 -0
  9. data/README.md +240 -0
  10. data/Rakefile +40 -0
  11. data/capybara-screenshot.gemspec +38 -0
  12. data/gemfiles/cucumber.1.2.gemfile +9 -0
  13. data/gemfiles/cucumber.1.3.0.gemfile +9 -0
  14. data/gemfiles/latest.gemfile +8 -0
  15. data/gemfiles/rspec.2.14.gemfile +9 -0
  16. data/gemfiles/rspec.2.99.gemfile +9 -0
  17. data/gemfiles/rspec.3.0.gemfile +9 -0
  18. data/gemfiles/spinach.0.7.gemfile +9 -0
  19. data/gemfiles/spinach.0.8.0.gemfile +9 -0
  20. data/lib/capybara-screenshot.rb +157 -0
  21. data/lib/capybara-screenshot/capybara.rb +28 -0
  22. data/lib/capybara-screenshot/cucumber.rb +27 -0
  23. data/lib/capybara-screenshot/minitest.rb +27 -0
  24. data/lib/capybara-screenshot/pruner.rb +47 -0
  25. data/lib/capybara-screenshot/rspec.rb +92 -0
  26. data/lib/capybara-screenshot/rspec/base_reporter.rb +21 -0
  27. data/lib/capybara-screenshot/rspec/html_embed_reporter.rb +25 -0
  28. data/lib/capybara-screenshot/rspec/html_link_reporter.rb +37 -0
  29. data/lib/capybara-screenshot/rspec/text_reporter.rb +38 -0
  30. data/lib/capybara-screenshot/rspec/textmate_link_reporter.rb +19 -0
  31. data/lib/capybara-screenshot/saver.rb +87 -0
  32. data/lib/capybara-screenshot/spinach.rb +26 -0
  33. data/lib/capybara-screenshot/testunit.rb +39 -0
  34. data/lib/capybara-screenshot/version.rb +5 -0
  35. data/spec/cucumber/cucumber_spec.rb +93 -0
  36. data/spec/cucumber/step_definitions/step_definitions.rb +18 -0
  37. data/spec/cucumber/support/env.rb +17 -0
  38. data/spec/feature/minitest_spec.rb +110 -0
  39. data/spec/feature/testunit_spec.rb +81 -0
  40. data/spec/rspec/rspec_spec.rb +159 -0
  41. data/spec/spec_helper.rb +29 -0
  42. data/spec/spinach/spinach_spec.rb +64 -0
  43. data/spec/spinach/support/spinach_failure.rb +41 -0
  44. data/spec/support/common_setup.rb +59 -0
  45. data/spec/support/html_reporter_context.rb +28 -0
  46. data/spec/support/test_app.rb +13 -0
  47. data/spec/unit/base_reporter_spec.rb +25 -0
  48. data/spec/unit/capybara-screenshot_rspec_spec.rb +48 -0
  49. data/spec/unit/capybara-screenshot_spec.rb +88 -0
  50. data/spec/unit/capybara_spec.rb +50 -0
  51. data/spec/unit/pruner_spec.rb +108 -0
  52. data/spec/unit/rspec_reporters/html_embed_reporter_spec.rb +18 -0
  53. data/spec/unit/rspec_reporters/html_link_reporter_spec.rb +27 -0
  54. data/spec/unit/rspec_reporters/text_reporter_spec.rb +97 -0
  55. data/spec/unit/rspec_reporters/textmate_link_reporter_spec.rb +39 -0
  56. data/spec/unit/saver_spec.rb +282 -0
  57. metadata +247 -0
@@ -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
+ expect(File).to 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
+ expect(content_without_styles).to eql("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
+ expect(content_without_styles).to eql(%{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
+ expect(content_without_styles).to eql(%{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,97 @@
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
+ alias_method :example_failed, :dump_failure_info
30
+ end
31
+
32
+ @reporter = @reporter_class.new
33
+ @reporter.singleton_class.send :include, described_class
34
+ end
35
+
36
+ let(:example_failed_method) do
37
+ if ::RSpec::Core::Version::STRING.to_i <= 2
38
+ :dump_failure_info
39
+ else
40
+ :example_failed
41
+ end
42
+ end
43
+
44
+ def example_failed_method_argument_double(metadata = {})
45
+ example_group = Module.new.include(Capybara::DSL)
46
+ example = double("example", metadata: metadata, example_group: example_group)
47
+ if ::RSpec::Core::Version::STRING.to_i <= 2
48
+ example
49
+ else
50
+ double("notification").tap do |notification|
51
+ allow(notification).to receive(:example).and_return(example)
52
+ end
53
+ end
54
+ end
55
+
56
+ context 'when there is no screenshot' do
57
+ let(:example) { example_failed_method_argument_double }
58
+
59
+ it 'doesnt change the original output of the reporter' do
60
+ @reporter.send(example_failed_method, example)
61
+ expect(@reporter.output.string).to eql("original failure info\n")
62
+ end
63
+ end
64
+
65
+ context 'when a html file was saved' do
66
+ let(:example) { example_failed_method_argument_double(screenshot: { html: "path/to/html" }) }
67
+
68
+ it 'appends the html file path to the original output' do
69
+ @reporter.send(example_failed_method, example)
70
+ expect(@reporter.output.string).to eql("original failure info\n #{"HTML screenshot: path/to/html"}\n")
71
+ end
72
+ end
73
+
74
+ context 'when a html file and an image were saved' do
75
+ let(:example) { example_failed_method_argument_double(screenshot: { html: "path/to/html", image: "path/to/image" }) }
76
+
77
+ it 'appends the image path to the original output' do
78
+ @reporter.send(example_failed_method, example)
79
+ expect(@reporter.output.string).to eql("original failure info\n #{"HTML screenshot: path/to/html"}\n #{"Image screenshot: path/to/image"}\n")
80
+ end
81
+ end
82
+
83
+
84
+ it 'works with older RSpec formatters where `#red` is used instead of `#failure_color`' do
85
+ old_reporter_class = Class.new(@reporter_class) do
86
+ undef_method :failure_color
87
+ def red(str)
88
+ "red(#{str})"
89
+ end
90
+ end
91
+ old_reporter = old_reporter_class.new
92
+ old_reporter.singleton_class.send :include, described_class
93
+ example = example_failed_method_argument_double(screenshot: { html: "path/to/html" })
94
+ old_reporter.send(example_failed_method, example)
95
+ expect(old_reporter.output.string).to eql("original failure info\n #{"HTML screenshot: path/to/html"}\n")
96
+ end
97
+ end
@@ -0,0 +1,39 @@
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
+ expect(content_without_styles).to eql(%{original content<p>} +
17
+ %{Saved files: <a href="file://path/to/a%20html%20file" onclick="#{expected_onclick_handler}">HTML page</a></p>}
18
+ )
19
+ end
20
+ end
21
+
22
+ context 'when a html file and an image were saved' do
23
+ before do
24
+ set_example double("example", metadata: {screenshot: {html: "path/to/html", image: "path/to/an image"}})
25
+ end
26
+
27
+ it 'appends links to both files to the original content' do
28
+ content_without_styles = @reporter.extra_failure_content(nil).gsub(/ ?style=".*?"/, "")
29
+ # Single quotes are handled differently by CGI.escape_html in Ruby 1.9 / Ruby 2, so to be
30
+ # compatible with both versions we can't hard code the final escaped string.
31
+ expected_onclick_handler_1 = CGI.escape_html("TextMate.system('open file://path/to/html'); return false;")
32
+ expected_onclick_handler_2 = CGI.escape_html("TextMate.system('open file://path/to/an\\%20image'); return false;")
33
+ expect(content_without_styles).to eql(%{original content<p>} +
34
+ %{Saved files: <a href="file://path/to/html" onclick="#{expected_onclick_handler_1}">HTML page</a>} +
35
+ %{<a href="file://path/to/an%20image" onclick="#{expected_onclick_handler_2}">Screenshot</a></p>}
36
+ )
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,282 @@
1
+ require 'spec_helper'
2
+
3
+ describe Capybara::Screenshot::Saver do
4
+ before(:all) do
5
+ @original_drivers = Capybara::Screenshot.registered_drivers
6
+ Capybara::Screenshot.registered_drivers[:default] = lambda {|driver, path| driver.render(path) }
7
+ end
8
+
9
+ after(:all) do
10
+ Capybara::Screenshot.registered_drivers = @original_drivers
11
+ end
12
+
13
+ before do
14
+ allow(Capybara::Screenshot).to receive(:capybara_root).and_return(capybara_root)
15
+ Timecop.freeze(Time.local(2012, 6, 7, 8, 9, 10, 0))
16
+ end
17
+
18
+ let(:capybara_root) { '/tmp' }
19
+ let(:timestamp) { '2012-06-07-08-09-10.000' }
20
+ let(:file_basename) { "screenshot_#{timestamp}" }
21
+ let(:screenshot_path) { "#{capybara_root}/#{file_basename}.png" }
22
+
23
+ let(:driver_mock) { double('Capybara driver').as_null_object }
24
+ let(:page_mock) { double('Capybara session page', :body => 'body', :driver => driver_mock).as_null_object }
25
+ let(:capybara_mock) {
26
+ double(Capybara).as_null_object.tap do |m|
27
+ allow(m).to receive(:current_driver).and_return(:default)
28
+ allow(m).to receive(:current_path).and_return('/')
29
+ end
30
+ }
31
+
32
+ let(:saver) { Capybara::Screenshot::Saver.new(capybara_mock, page_mock) }
33
+
34
+ context 'html filename with Capybara Version 1' do
35
+ before do
36
+ stub_const("Capybara::VERSION", '1')
37
+ end
38
+
39
+ it 'has a default format of "screenshot_Y-M-D-H-M-S.ms.html"' do
40
+ expect(capybara_mock).to receive(:save_page).with('body', File.join(capybara_root, "#{file_basename}.html"))
41
+
42
+ saver.save
43
+ end
44
+
45
+ it 'uses name argument as prefix' do
46
+ saver = Capybara::Screenshot::Saver.new(capybara_mock, page_mock, true, 'custom-prefix')
47
+
48
+ expect(capybara_mock).to receive(:save_page).with('body', File.join(capybara_root, "custom-prefix_#{timestamp}.html"))
49
+
50
+ saver.save
51
+ end
52
+ end
53
+
54
+ context 'html filename with Capybara Version 2' do
55
+ before do
56
+ stub_const("Capybara::VERSION", '2')
57
+ end
58
+
59
+ it 'has a default format of "screenshot_Y-M-D-H-M-S.ms.html"' do
60
+ expect(capybara_mock).to receive(:save_page).with(File.join(capybara_root, "#{file_basename}.html"))
61
+
62
+ saver.save
63
+ end
64
+
65
+ it 'uses name argument as prefix' do
66
+ saver = Capybara::Screenshot::Saver.new(capybara_mock, page_mock, true, 'custom-prefix')
67
+
68
+ expect(capybara_mock).to receive(:save_page).with(File.join(capybara_root, "custom-prefix_#{timestamp}.html"))
69
+
70
+ saver.save
71
+ end
72
+ end
73
+
74
+ context 'screenshot image path' do
75
+ it 'is in capybara root output' do
76
+ expect(driver_mock).to receive(:render).with(/^#{capybara_root}\//)
77
+
78
+ saver.save
79
+ end
80
+
81
+ it 'has a default filename format of "screenshot_Y-M-D-H-M-S.ms.png"' do
82
+ expect(driver_mock).to receive(:render).with(/#{file_basename}\.png$/)
83
+
84
+ saver.save
85
+ end
86
+
87
+ it "does not append timestamp if append_timestamp is false " do
88
+ default_config = Capybara::Screenshot.append_timestamp
89
+ Capybara::Screenshot.append_timestamp = false
90
+ expect(driver_mock).to receive(:render).with(/screenshot.png$/)
91
+
92
+ saver.save
93
+ Capybara::Screenshot.append_timestamp = default_config
94
+ end
95
+
96
+ it 'uses filename prefix argument as basename prefix' do
97
+ saver = Capybara::Screenshot::Saver.new(capybara_mock, page_mock, true, 'custom-prefix')
98
+ expect(driver_mock).to receive(:render).with(/#{capybara_root}\/custom-prefix_#{timestamp}\.png$/)
99
+
100
+ saver.save
101
+ end
102
+ end
103
+
104
+ it 'does not save html if false passed as html argument' do
105
+ saver = Capybara::Screenshot::Saver.new(capybara_mock, page_mock, false)
106
+ expect(capybara_mock).to_not receive(:save_page)
107
+
108
+ saver.save
109
+ expect(saver).to_not be_html_saved
110
+ end
111
+
112
+ it 'does not save if current_path is empty' do
113
+ allow(capybara_mock).to receive(:current_path).and_return(nil)
114
+ expect(capybara_mock).to_not receive(:save_page)
115
+ expect(driver_mock).to_not receive(:render)
116
+
117
+ saver.save
118
+ expect(saver).to_not be_screenshot_saved
119
+ expect(saver).to_not be_html_saved
120
+ end
121
+
122
+ describe '#output_screenshot_path' do
123
+ let(:saver) { Capybara::Screenshot::Saver.new(capybara_mock, page_mock) }
124
+
125
+ before do
126
+ allow(saver).to receive(:html_path) { 'page.html' }
127
+ allow(saver).to receive(:screenshot_path) { 'screenshot.png' }
128
+ end
129
+
130
+ it 'outputs the path for the HTML screenshot' do
131
+ allow(saver).to receive(:html_saved?).and_return(true)
132
+ expect(saver).to receive(:output).with("HTML screenshot: page.html")
133
+ saver.output_screenshot_path
134
+ end
135
+
136
+ it 'outputs the path for the Image screenshot' do
137
+ allow(saver).to receive(:screenshot_saved?).and_return(true)
138
+ expect(saver).to receive(:output).with("Image screenshot: screenshot.png")
139
+ saver.output_screenshot_path
140
+ end
141
+ end
142
+
143
+ describe "with selenium driver" do
144
+ before do
145
+ allow(capybara_mock).to receive(:current_driver).and_return(:selenium)
146
+ end
147
+
148
+ it 'saves via browser' do
149
+ browser_mock = double('browser')
150
+ expect(driver_mock).to receive(:browser).and_return(browser_mock)
151
+ expect(browser_mock).to receive(:save_screenshot).with(screenshot_path)
152
+
153
+ saver.save
154
+ expect(saver).to be_screenshot_saved
155
+ end
156
+ end
157
+
158
+ describe "with poltergeist driver" do
159
+ before do
160
+ allow(capybara_mock).to receive(:current_driver).and_return(:poltergeist)
161
+ end
162
+
163
+ it 'saves driver render with :full => true' do
164
+ expect(driver_mock).to receive(:render).with(screenshot_path, {:full => true})
165
+
166
+ saver.save
167
+ expect(saver).to be_screenshot_saved
168
+ end
169
+ end
170
+
171
+ describe "with poltergeist_billy driver" do
172
+ before do
173
+ allow(capybara_mock).to receive(:current_driver).and_return(:poltergeist_billy)
174
+ end
175
+
176
+ it 'saves driver render with :full => true' do
177
+ expect(driver_mock).to receive(:render).with(screenshot_path, {:full => true})
178
+
179
+ saver.save
180
+ expect(saver).to be_screenshot_saved
181
+ end
182
+ end
183
+
184
+ describe "with webkit driver" do
185
+ before do
186
+ allow(capybara_mock).to receive(:current_driver).and_return(:webkit)
187
+ end
188
+
189
+ context 'has render method' do
190
+ before do
191
+ allow(driver_mock).to receive(:respond_to?).with(:'save_screenshot').and_return(false)
192
+ end
193
+
194
+ it 'saves driver render' do
195
+ expect(driver_mock).to receive(:render).with(screenshot_path)
196
+
197
+ saver.save
198
+ expect(saver).to be_screenshot_saved
199
+ end
200
+ end
201
+
202
+ context 'has save_screenshot method' do
203
+ let(:webkit_options){ {width: 800, height: 600} }
204
+
205
+ before do
206
+ allow(driver_mock).to receive(:respond_to?).with(:'save_screenshot').and_return(true)
207
+ end
208
+
209
+ it 'saves driver render' do
210
+ expect(driver_mock).to receive(:save_screenshot).with(screenshot_path, {})
211
+
212
+ saver.save
213
+ expect(saver).to be_screenshot_saved
214
+ end
215
+
216
+ it 'passes webkit_options to driver' do
217
+ allow(Capybara::Screenshot).to receive(:webkit_options).and_return( webkit_options )
218
+ expect(driver_mock).to receive(:save_screenshot).with(screenshot_path, webkit_options)
219
+
220
+ saver.save
221
+ expect(saver).to be_screenshot_saved
222
+ end
223
+ end
224
+ end
225
+
226
+ describe "with webkit debug driver" do
227
+ before do
228
+ allow(capybara_mock).to receive(:current_driver).and_return(:webkit_debug)
229
+ end
230
+
231
+ it 'saves driver render' do
232
+ expect(driver_mock).to receive(:render).with(screenshot_path)
233
+
234
+ saver.save
235
+ expect(saver).to be_screenshot_saved
236
+ end
237
+ end
238
+
239
+ describe "with unknown driver" do
240
+ before do
241
+ allow(capybara_mock).to receive(:current_driver).and_return(:unknown)
242
+ allow(saver).to receive(:warn).and_return(nil)
243
+ end
244
+
245
+ it 'saves driver render' do
246
+ expect(driver_mock).to receive(:render).with(screenshot_path)
247
+
248
+ saver.save
249
+ expect(saver).to be_screenshot_saved
250
+ end
251
+
252
+ it 'outputs warning about unknown results' do
253
+ # Not pure mock testing
254
+ expect(saver).to receive(:warn).with(/screenshot driver for 'unknown'.*unknown results/).and_return(nil)
255
+
256
+ saver.save
257
+ expect(saver).to be_screenshot_saved
258
+ end
259
+
260
+ describe "with rack_test driver" do
261
+ before do
262
+ allow(capybara_mock).to receive(:current_driver).and_return(:rack_test)
263
+ end
264
+
265
+ it 'indicates that a screenshot could not be saved' do
266
+ saver.save
267
+ expect(saver).to_not be_screenshot_saved
268
+ end
269
+ end
270
+
271
+ describe "with mechanize driver" do
272
+ before do
273
+ allow(capybara_mock).to receive(:current_driver).and_return(:mechanize)
274
+ end
275
+
276
+ it 'indicates that a screenshot could not be saved' do
277
+ saver.save
278
+ expect(saver).to_not be_screenshot_saved
279
+ end
280
+ end
281
+ end
282
+ end