capybara-screenshot 0.3.22 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rspec +1 -1
  4. data/.travis.yml +9 -0
  5. data/Appraisals +31 -0
  6. data/CHANGELOG.md +16 -0
  7. data/Gemfile +1 -0
  8. data/README.md +23 -17
  9. data/Rakefile +9 -1
  10. data/capybara-screenshot.gemspec +7 -1
  11. data/gemfiles/cucumber.1.2.gemfile +9 -0
  12. data/gemfiles/cucumber.1.2.gemfile.lock +99 -0
  13. data/gemfiles/cucumber.1.3.0.gemfile +9 -0
  14. data/gemfiles/cucumber.1.3.0.gemfile.lock +100 -0
  15. data/gemfiles/latest.gemfile +8 -0
  16. data/gemfiles/latest.gemfile.lock +102 -0
  17. data/gemfiles/rspec.2.14.gemfile +9 -0
  18. data/gemfiles/rspec.2.14.gemfile.lock +98 -0
  19. data/gemfiles/rspec.2.99.gemfile +9 -0
  20. data/gemfiles/rspec.2.99.gemfile.lock +98 -0
  21. data/gemfiles/rspec.3.0.gemfile +9 -0
  22. data/gemfiles/rspec.3.0.gemfile.lock +102 -0
  23. data/gemfiles/spinach.0.7.gemfile +9 -0
  24. data/gemfiles/spinach.0.7.gemfile.lock +100 -0
  25. data/gemfiles/spinach.0.8.0.gemfile +9 -0
  26. data/gemfiles/spinach.0.8.0.gemfile.lock +100 -0
  27. data/lib/capybara-screenshot.rb +5 -0
  28. data/lib/capybara-screenshot/capybara.rb +12 -0
  29. data/lib/capybara-screenshot/cucumber.rb +18 -12
  30. data/lib/capybara-screenshot/minitest.rb +15 -16
  31. data/lib/capybara-screenshot/rspec.rb +19 -11
  32. data/lib/capybara-screenshot/rspec/text_reporter.rb +21 -3
  33. data/lib/capybara-screenshot/spinach.rb +22 -6
  34. data/lib/capybara-screenshot/testunit.rb +25 -7
  35. data/lib/capybara-screenshot/version.rb +1 -1
  36. data/spec/cucumber/cucumber_spec.rb +64 -0
  37. data/spec/cucumber/step_definitions/step_definitions.rb +18 -0
  38. data/spec/cucumber/support/env.rb +17 -0
  39. data/spec/feature/minitest_spec.rb +90 -0
  40. data/spec/feature/testunit_spec.rb +71 -0
  41. data/spec/rspec/rspec_spec.rb +101 -0
  42. data/spec/spec_helper.rb +9 -1
  43. data/spec/spinach/spinach_spec.rb +53 -0
  44. data/spec/spinach/support/spinach_failure.rb +41 -0
  45. data/spec/support/common_setup.rb +28 -0
  46. data/spec/support/html_reporter_context.rb +1 -1
  47. data/spec/support/test_app.rb +13 -0
  48. data/spec/{rspec → unit}/base_reporter_spec.rb +2 -2
  49. data/spec/{rspec_spec.rb → unit/capybara-screenshot_rspec_spec.rb} +15 -10
  50. data/spec/{capybara-screenshot_spec.rb → unit/capybara-screenshot_spec.rb} +8 -8
  51. data/spec/unit/capybara_spec.rb +50 -0
  52. data/spec/{rspec → unit/rspec_reporters}/html_embed_reporter_spec.rb +2 -2
  53. data/spec/{rspec → unit/rspec_reporters}/html_link_reporter_spec.rb +2 -2
  54. data/spec/unit/rspec_reporters/text_reporter_spec.rb +96 -0
  55. data/spec/{rspec → unit/rspec_reporters}/textmate_link_reporter_spec.rb +5 -3
  56. data/spec/unit/saver_spec.rb +269 -0
  57. metadata +161 -41
  58. data/spec/capybara-screenshot/capybara_spec.rb +0 -18
  59. data/spec/capybara-screenshot/saver_spec.rb +0 -269
  60. data/spec/rspec/text_reporter_spec.rb +0 -76
@@ -22,7 +22,7 @@ shared_context 'html reporter' do
22
22
  end
23
23
 
24
24
  it 'doesnt change the original content of the reporter' do
25
- @reporter.extra_failure_content(nil).should == "original content"
25
+ expect(@reporter.extra_failure_content(nil)).to eql("original content")
26
26
  end
27
27
  end
28
28
  end
@@ -0,0 +1,13 @@
1
+ require 'sinatra/base'
2
+
3
+ Sinatra::Application.root = '.'
4
+
5
+ class TestApp < Sinatra::Base
6
+ get '/' do
7
+ 'This is the root page'
8
+ end
9
+
10
+ get '/different_page' do
11
+ 'This is a different page'
12
+ end
13
+ end
@@ -17,9 +17,9 @@ describe Capybara::Screenshot::RSpec::BaseReporter do
17
17
  end
18
18
  end
19
19
 
20
- klass.new.foo.should == :original
20
+ expect(klass.new.foo).to eql(:original)
21
21
  klass.send :include, reporter_module
22
- klass.new.foo.should == [:original, :enhanced]
22
+ expect(klass.new.foo).to eql([:original, :enhanced])
23
23
  end
24
24
  end
25
25
  end
@@ -4,38 +4,43 @@ describe Capybara::Screenshot::RSpec do
4
4
  describe '.after_failed_example' do
5
5
  context 'for a failed example in a feature that can be snapshotted' do
6
6
  before do
7
- Capybara.page.stub(save_page: nil, current_url: "http://test.local")
8
- Capybara::Screenshot::Saver.stub(new: mock_saver)
7
+ allow(Capybara.page).to receive(:save_page)
8
+ allow(Capybara.page).to receive(:current_url).and_return("http://test.local")
9
+ allow(Capybara::Screenshot::Saver).to receive(:new).and_return(mock_saver)
9
10
  end
10
11
  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
+ let(:mock_saver) do
13
+ Capybara::Screenshot::Saver.new(Capybara, Capybara.page).tap do |saver|
14
+ allow(saver).to receive(:save)
15
+ end
16
+ end
12
17
 
13
18
  it 'instantiates a saver and calls `save` on it' do
14
- mock_saver.should_receive(:save)
19
+ expect(mock_saver).to receive(:save)
15
20
  described_class.after_failed_example(example)
16
21
  end
17
22
 
18
23
  it 'extends the metadata with an empty hash for screenshot metadata' do
19
24
  described_class.after_failed_example(example)
20
- example.metadata.should have_key(:screenshot)
21
- example.metadata[:screenshot].should == {}
25
+ expect(example.metadata).to have_key(:screenshot)
26
+ expect(example.metadata[:screenshot]).to eql({})
22
27
  end
23
28
 
24
29
  context 'when a html file gets saved' do
25
- before { mock_saver.stub(:html_saved? => true) }
30
+ before { allow(mock_saver).to receive(:html_saved?).and_return(true) }
26
31
 
27
32
  it 'adds the html file path to the screenshot metadata' do
28
33
  described_class.after_failed_example(example)
29
- example.metadata[:screenshot][:html].should start_with("./screenshot")
34
+ expect(example.metadata[:screenshot][:html]).to match("./screenshot")
30
35
  end
31
36
  end
32
37
 
33
38
  context 'when an image gets saved' do
34
- before { mock_saver.stub(:screenshot_saved? => true) }
39
+ before { allow(mock_saver).to receive(:screenshot_saved?).and_return(true) }
35
40
 
36
41
  it 'adds the image path to the screenshot metadata' do
37
42
  described_class.after_failed_example(example)
38
- example.metadata[:screenshot][:image].should start_with("./screenshot")
43
+ expect(example.metadata[:screenshot][:image]).to match("./screenshot")
39
44
  end
40
45
  end
41
46
  end
@@ -10,11 +10,11 @@ describe Capybara::Screenshot do
10
10
  Capybara::Screenshot.registered_drivers = @original_drivers
11
11
  end
12
12
 
13
- it 'should store driver with block' do
13
+ it 'stores driver with block' do
14
14
  block = lambda {}
15
15
  Capybara::Screenshot.register_driver :foo, &block
16
16
 
17
- Capybara::Screenshot.registered_drivers[:foo].should eq block
17
+ expect(Capybara::Screenshot.registered_drivers[:foo]).to eql(block)
18
18
  end
19
19
  end
20
20
 
@@ -27,17 +27,17 @@ describe Capybara::Screenshot do
27
27
  Capybara::Screenshot.filename_prefix_formatters = @original_formatters
28
28
  end
29
29
 
30
- it 'should store test type with block' do
30
+ it 'stores test type with block' do
31
31
  block = lambda { |arg| }
32
32
  Capybara::Screenshot.register_filename_prefix_formatter :foo, &block
33
33
 
34
- Capybara::Screenshot.filename_prefix_formatters[:foo].should eq block
34
+ expect(Capybara::Screenshot.filename_prefix_formatters[:foo]).to eql(block)
35
35
  end
36
36
  end
37
37
 
38
38
  describe ".filename_prefix_for" do
39
- it 'should return "screenshot" for undefined formatter' do
40
- Capybara::Screenshot.filename_prefix_for(:foo, double('test')).should eq 'screenshot'
39
+ it 'returns "screenshot" for undefined formatter' do
40
+ expect(Capybara::Screenshot.filename_prefix_for(:foo, double('test'))).to eql('screenshot')
41
41
  end
42
42
  end
43
43
 
@@ -51,10 +51,10 @@ describe Capybara::Screenshot do
51
51
  }.to change {
52
52
  Capybara::Screenshot::RSpec.add_link_to_screenshot_for_failed_examples
53
53
  }.from(true).to(false)
54
- $stderr.string.should include("append_screenshot_path is deprecated")
54
+ expect($stderr.string).to include("append_screenshot_path is deprecated")
55
55
  ensure
56
56
  $stderr = original_stderr
57
57
  end
58
58
  end
59
59
  end
60
- end
60
+ end
@@ -0,0 +1,50 @@
1
+ require 'capybara-screenshot'
2
+ require 'capybara/dsl'
3
+
4
+ describe Capybara do
5
+ it 'adds screen shot methods to the Capybara module' do
6
+ expect(::Capybara).to respond_to(:screenshot_and_save_page)
7
+ expect(::Capybara).to respond_to(:screenshot_and_open_image)
8
+ end
9
+
10
+ context 'request type example', :type => :request do
11
+ it 'has access to screen shot instance methods' do
12
+ expect(subject).to respond_to(:screenshot_and_save_page)
13
+ expect(subject).to respond_to(:screenshot_and_open_image)
14
+ end
15
+ end
16
+
17
+ describe 'using_session' do
18
+ include Capybara::DSL
19
+
20
+ it 'saves the name of the final session' do
21
+ expect(Capybara::Screenshot).to receive(:final_session_name=).with(:different_session)
22
+ expect {
23
+ using_session :different_session do
24
+ expect(0).to eq 1
25
+ end
26
+ }.to raise_exception ::RSpec::Expectations::ExpectationNotMetError
27
+ end
28
+ end
29
+ end
30
+
31
+ describe 'final_session_name' do
32
+ subject { Capybara::Screenshot.clone }
33
+
34
+ describe 'when the final session name has been set' do
35
+ before do
36
+ subject.final_session_name = 'my-failing-session'
37
+ end
38
+
39
+ it 'returns the name' do
40
+ expect(subject.final_session_name).to eq 'my-failing-session'
41
+ end
42
+ end
43
+
44
+ describe 'when the final session name has not been set' do
45
+ it 'returns the current session name' do
46
+ allow(Capybara).to receive(:session_name).and_return('my-current-session')
47
+ expect(subject.final_session_name).to eq 'my-current-session'
48
+ end
49
+ end
50
+ end
@@ -9,10 +9,10 @@ describe Capybara::Screenshot::RSpec::HtmlEmbedReporter do
9
9
  end
10
10
 
11
11
  it 'embeds the image base64 encoded into the content' do
12
- File.should_receive(:binread).with("path/to/image").and_return("image data")
12
+ expect(File).to receive(:binread).with("path/to/image").and_return("image data")
13
13
  encoded_image_data = Base64.encode64('image data')
14
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}'>"
15
+ expect(content_without_styles).to eql("original content<img src='data:image/png;base64,#{encoded_image_data}'>")
16
16
  end
17
17
  end
18
18
  end
@@ -10,7 +10,7 @@ describe Capybara::Screenshot::RSpec::HtmlLinkReporter do
10
10
 
11
11
  it 'appends a link to the html to the original content' do
12
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>}
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
14
  end
15
15
  end
16
16
 
@@ -21,7 +21,7 @@ describe Capybara::Screenshot::RSpec::HtmlLinkReporter do
21
21
 
22
22
  it 'appends links to both files to the original content' do
23
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>}
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
25
  end
26
26
  end
27
27
  end
@@ -0,0 +1,96 @@
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::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 = double("example", metadata: metadata)
46
+ if ::RSpec::Version::STRING.to_i <= 2
47
+ example
48
+ else
49
+ double("notification").tap do |notification|
50
+ allow(notification).to receive(:example).and_return(example)
51
+ end
52
+ end
53
+ end
54
+
55
+ context 'when there is no screenshot' do
56
+ let(:example) { example_failed_method_argument_double }
57
+
58
+ it 'doesnt change the original output of the reporter' do
59
+ @reporter.send(example_failed_method, example)
60
+ expect(@reporter.output.string).to eql("original failure info\n")
61
+ end
62
+ end
63
+
64
+ context 'when a html file was saved' do
65
+ let(:example) { example_failed_method_argument_double(screenshot: { html: "path/to/html" }) }
66
+
67
+ it 'appends the html file path to the original output' do
68
+ @reporter.send(example_failed_method, example)
69
+ expect(@reporter.output.string).to eql("original failure info\n #{"HTML screenshot: path/to/html".yellow}\n")
70
+ end
71
+ end
72
+
73
+ context 'when a html file and an image were saved' do
74
+ let(:example) { example_failed_method_argument_double(screenshot: { html: "path/to/html", image: "path/to/image" }) }
75
+
76
+ it 'appends the image path to the original output' do
77
+ @reporter.send(example_failed_method, example)
78
+ expect(@reporter.output.string).to eql("original failure info\n #{"HTML screenshot: path/to/html".yellow}\n #{"Image screenshot: path/to/image".yellow}\n")
79
+ end
80
+ end
81
+
82
+
83
+ it 'works with older RSpec formatters where `#red` is used instead of `#failure_color`' do
84
+ old_reporter_class = Class.new(@reporter_class) do
85
+ undef_method :failure_color
86
+ def red(str)
87
+ "red(#{str})"
88
+ end
89
+ end
90
+ old_reporter = old_reporter_class.new
91
+ old_reporter.singleton_class.send :include, described_class
92
+ example = example_failed_method_argument_double(screenshot: { html: "path/to/html" })
93
+ old_reporter.send(example_failed_method, example)
94
+ expect(old_reporter.output.string).to eql("original failure info\n #{"HTML screenshot: path/to/html".yellow}\n")
95
+ end
96
+ end
@@ -13,8 +13,9 @@ describe Capybara::Screenshot::RSpec::TextMateLinkReporter do
13
13
  # Single quotes are handled differently by CGI.escape_html in Ruby 1.9 / Ruby 2, so to be
14
14
  # compatible with both versions we can't hard code the final escaped string.
15
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>} +
16
+ expect(content_without_styles).to eql(%{original content<p>} +
17
17
  %{Saved files: <a href="file://path/to/a%20html%20file" onclick="#{expected_onclick_handler}">HTML page</a></p>}
18
+ )
18
19
  end
19
20
  end
20
21
 
@@ -29,9 +30,10 @@ describe Capybara::Screenshot::RSpec::TextMateLinkReporter do
29
30
  # compatible with both versions we can't hard code the final escaped string.
30
31
  expected_onclick_handler_1 = CGI.escape_html("TextMate.system('open file://path/to/html'); return false;")
31
32
  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
+ expect(content_without_styles).to eql(%{original content<p>} +
33
34
  %{Saved files: <a href="file://path/to/html" onclick="#{expected_onclick_handler_1}">HTML page</a>} +
34
35
  %{<a href="file://path/to/an%20image" onclick="#{expected_onclick_handler_2}">Screenshot</a></p>}
36
+ )
35
37
  end
36
38
  end
37
- end
39
+ end
@@ -0,0 +1,269 @@
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 webkit driver" do
172
+ before do
173
+ allow(capybara_mock).to receive(:current_driver).and_return(:webkit)
174
+ end
175
+
176
+ context 'has render method' do
177
+ before do
178
+ allow(driver_mock).to receive(:respond_to?).with(:'save_screenshot').and_return(false)
179
+ end
180
+
181
+ it 'saves driver render' do
182
+ expect(driver_mock).to receive(:render).with(screenshot_path)
183
+
184
+ saver.save
185
+ expect(saver).to be_screenshot_saved
186
+ end
187
+ end
188
+
189
+ context 'has save_screenshot method' do
190
+ let(:webkit_options){ {width: 800, height: 600} }
191
+
192
+ before do
193
+ allow(driver_mock).to receive(:respond_to?).with(:'save_screenshot').and_return(true)
194
+ end
195
+
196
+ it 'saves driver render' do
197
+ expect(driver_mock).to receive(:save_screenshot).with(screenshot_path, {})
198
+
199
+ saver.save
200
+ expect(saver).to be_screenshot_saved
201
+ end
202
+
203
+ it 'passes webkit_options to driver' do
204
+ allow(Capybara::Screenshot).to receive(:webkit_options).and_return( webkit_options )
205
+ expect(driver_mock).to receive(:save_screenshot).with(screenshot_path, webkit_options)
206
+
207
+ saver.save
208
+ expect(saver).to be_screenshot_saved
209
+ end
210
+ end
211
+ end
212
+
213
+ describe "with webkit debug driver" do
214
+ before do
215
+ allow(capybara_mock).to receive(:current_driver).and_return(:webkit_debug)
216
+ end
217
+
218
+ it 'saves driver render' do
219
+ expect(driver_mock).to receive(:render).with(screenshot_path)
220
+
221
+ saver.save
222
+ expect(saver).to be_screenshot_saved
223
+ end
224
+ end
225
+
226
+ describe "with unknown driver" do
227
+ before do
228
+ allow(capybara_mock).to receive(:current_driver).and_return(:unknown)
229
+ allow(saver).to receive(:warn).and_return(nil)
230
+ end
231
+
232
+ it 'saves driver render' do
233
+ expect(driver_mock).to receive(:render).with(screenshot_path)
234
+
235
+ saver.save
236
+ expect(saver).to be_screenshot_saved
237
+ end
238
+
239
+ it 'outputs warning about unknown results' do
240
+ # Not pure mock testing
241
+ expect(saver).to receive(:warn).with(/screenshot driver for 'unknown'.*unknown results/).and_return(nil)
242
+
243
+ saver.save
244
+ expect(saver).to be_screenshot_saved
245
+ end
246
+
247
+ describe "with rack_test driver" do
248
+ before do
249
+ allow(capybara_mock).to receive(:current_driver).and_return(:rack_test)
250
+ end
251
+
252
+ it 'indicates that a screenshot could not be saved' do
253
+ saver.save
254
+ expect(saver).to_not be_screenshot_saved
255
+ end
256
+ end
257
+
258
+ describe "with mechanize driver" do
259
+ before do
260
+ allow(capybara_mock).to receive(:current_driver).and_return(:mechanize)
261
+ end
262
+
263
+ it 'indicates that a screenshot could not be saved' do
264
+ saver.save
265
+ expect(saver).to_not be_screenshot_saved
266
+ end
267
+ end
268
+ end
269
+ end