capybara-screenshot 0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,10 +3,12 @@ module Capybara
3
3
  class << self
4
4
  attr_accessor :autosave_on_failure
5
5
  attr_accessor :registered_drivers
6
+ attr_accessor :filename_prefix_formatters
6
7
  end
7
8
 
8
9
  self.autosave_on_failure = true
9
10
  self.registered_drivers = {}
11
+ self.filename_prefix_formatters = {}
10
12
 
11
13
  def self.screen_shot_and_save_page
12
14
  saver = Saver.new(Capybara, Capybara.page)
@@ -23,6 +25,12 @@ module Capybara
23
25
  {:html => saver.html_path, :image => saver.screenshot_path}
24
26
  end
25
27
 
28
+ def self.filename_prefix_for(test_type, test)
29
+ filename_prefix_formatters.fetch(test_type) { |key|
30
+ filename_prefix_formatters[:default]
31
+ }.call(test)
32
+ end
33
+
26
34
  def self.capybara_root
27
35
  return @capybara_root if defined?(@capybara_root)
28
36
 
@@ -43,20 +51,38 @@ module Capybara
43
51
  def self.register_driver(driver, &block)
44
52
  self.registered_drivers[driver] = block
45
53
  end
54
+
55
+ def self.register_filename_prefix_formatter(test_type, &block)
56
+ self.filename_prefix_formatters[test_type] = block
57
+ end
46
58
  end
47
59
  end
48
60
 
49
- Capybara::Screenshot.register_driver(:selenium) do |driver, path|
50
- driver.browser.save_screenshot(path)
51
- end
61
+ # Register driver renderers
62
+ Capybara::Screenshot.class_eval do
63
+ register_driver(:default) do |driver, path|
64
+ driver.render(path)
65
+ end
66
+
67
+ register_driver(:selenium) do |driver, path|
68
+ driver.browser.save_screenshot(path)
69
+ end
70
+
71
+ register_driver(:poltergeist) do |driver, path|
72
+ driver.render(path, :full => true)
73
+ end
52
74
 
53
- Capybara::Screenshot.register_driver(:poltergeist) do |driver, path|
54
- driver.render(path, :full => true)
55
- end
75
+ register_driver(:webkit) do |driver, path|
76
+ driver.render(path)
77
+ end
78
+ end
56
79
 
57
- Capybara::Screenshot.register_driver(:webkit) do |driver, path|
58
- driver.render(path)
59
- end
80
+ # Register filename prefix formatters
81
+ Capybara::Screenshot.class_eval do
82
+ register_filename_prefix_formatter(:default) do |test|
83
+ 'screenshot'
84
+ end
85
+ end
60
86
 
61
87
  require 'capybara-screenshot/saver'
62
88
 
@@ -17,7 +17,9 @@ World(Capybara::Screenshot::Cucumber)
17
17
 
18
18
  After do |scenario|
19
19
  if Capybara::Screenshot.autosave_on_failure && scenario.failed?
20
- saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page)
20
+ filename_prefix = Capybara::Screenshot.filename_prefix_for(:cucumber, scenario)
21
+
22
+ saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
21
23
  saver.save
22
24
 
23
25
  # Trying to embed the screenshot into our output."
@@ -10,10 +10,13 @@ if defined?(ActionDispatch::IntegrationTest)
10
10
  ActionDispatch::IntegrationTest.send method do |context|
11
11
  # by adding the argument context, MiniTest passes the context of the test
12
12
  # which has an instance variable @passed indicating success / failure
13
- context.instance_eval do
14
- if Capybara::Screenshot.autosave_on_failure && @passed.blank?
15
- Capybara::Screenshot.screen_shot_and_save_page
16
- end
13
+ failed = context.instance_variable_get(:@passed).blank?
14
+
15
+ if Capybara::Screenshot.autosave_on_failure && failed
16
+ filename_prefix = Capybara::Screenshot.filename_prefix_for(:minitest, context)
17
+
18
+ saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
19
+ saver.save
17
20
  end
18
21
  end
19
22
  rescue NoMethodError
@@ -1,7 +1,9 @@
1
1
  RSpec.configure do |config|
2
2
  config.after(:type => :request) do
3
3
  if Capybara::Screenshot.autosave_on_failure && example.exception
4
- saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page)
4
+ filename_prefix = Capybara::Screenshot.filename_prefix_for(:rspec, example)
5
+
6
+ saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
5
7
  saver.save
6
8
 
7
9
  example.metadata[:full_description] += "\n Screenshot: #{saver.screenshot_path}"
@@ -3,9 +3,9 @@ module Capybara
3
3
  class Saver
4
4
  attr_reader :capybara, :page, :file_base_name
5
5
 
6
- def initialize(capybara, page, html_save=true)
6
+ def initialize(capybara, page, html_save=true, filename_prefix='screenshot')
7
7
  @capybara, @page, @html_save = capybara, page, html_save
8
- @file_base_name = "screenshot-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}"
8
+ @file_base_name = "#{filename_prefix}-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}"
9
9
  end
10
10
 
11
11
  def save
@@ -22,16 +22,10 @@ module Capybara
22
22
  end
23
23
 
24
24
  def save_screenshot
25
- if Capybara::Screenshot.registered_drivers.has_key?(capybara.current_driver)
26
- Capybara::Screenshot.registered_drivers[capybara.current_driver].call(page.driver, screenshot_path)
27
- else
25
+ Capybara::Screenshot.registered_drivers.fetch(capybara.current_driver) { |driver_name|
28
26
  warn "capybara-screenshot could not detect a screenshot driver for '#{capybara.current_driver}'. Saving with default with unknown results."
29
- save_with_default
30
- end
31
- end
32
-
33
- def save_with_default
34
- page.driver.render(screenshot_path)
27
+ Capybara::Screenshot.registered_drivers[:default]
28
+ }.call(page.driver, screenshot_path)
35
29
  end
36
30
 
37
31
  def html_path
@@ -1,5 +1,5 @@
1
1
  module Capybara
2
2
  module Screenshot
3
- VERSION = "0.2"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
@@ -16,7 +16,8 @@ describe Capybara::Screenshot::Saver do
16
16
  end
17
17
 
18
18
  let(:capybara_root) { '/tmp' }
19
- let(:file_basename) { "screenshot-2012-06-07-08-09-10" }
19
+ let(:timestamp) { '2012-06-07-08-09-10' }
20
+ let(:file_basename) { "screenshot-#{timestamp}" }
20
21
  let(:screenshot_path) { "#{capybara_root}/#{file_basename}.png" }
21
22
 
22
23
  let(:driver_mock) { mock('Capybara driver').as_null_object }
@@ -30,16 +31,41 @@ describe Capybara::Screenshot::Saver do
30
31
 
31
32
  let(:saver) { Capybara::Screenshot::Saver.new(capybara_mock, page_mock) }
32
33
 
33
- it 'should save html file with "screenshot-Y-M-D-H-M-S.html" format' do
34
- capybara_mock.should_receive(:save_page).with('body', "#{file_basename}.html")
35
-
36
- saver.save
34
+ context 'html filename' do
35
+ it 'should have default format of "screenshot-Y-M-D-H-M-S.html"' do
36
+ capybara_mock.should_receive(:save_page).with('body', "#{file_basename}.html")
37
+
38
+ saver.save
39
+ end
40
+
41
+ it 'should use name argument as prefix' do
42
+ saver = Capybara::Screenshot::Saver.new(capybara_mock, page_mock, true, 'custom-prefix')
43
+
44
+ capybara_mock.should_receive(:save_page).with('body', "custom-prefix-#{timestamp}.html")
45
+
46
+ saver.save
47
+ end
37
48
  end
38
49
 
39
- it 'should save screenshot file in capybara root output directory with format "screenshot-Y-M-D-H-M-S.png"' do
40
- driver_mock.should_receive(:render).with(screenshot_path)
50
+ context 'screenshot image path' do
51
+ it 'should be in capybara root output' do
52
+ driver_mock.should_receive(:render).with(/^#{capybara_root}\//)
41
53
 
42
- saver.save
54
+ saver.save
55
+ end
56
+
57
+ it 'should have default filename format of "screenshot-Y-M-D-H-M-S.png"' do
58
+ driver_mock.should_receive(:render).with(/#{file_basename}\.png$/)
59
+
60
+ saver.save
61
+ end
62
+
63
+ it 'should use filename prefix argument as basename prefix' do
64
+ saver = Capybara::Screenshot::Saver.new(capybara_mock, page_mock, true, 'custom-prefix')
65
+ driver_mock.should_receive(:render).with(/custom-prefix-#{timestamp}\.png$/)
66
+
67
+ saver.save
68
+ end
43
69
  end
44
70
 
45
71
  it 'should not save html if false passed as html argument' do
@@ -1,15 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Capybara::Screenshot do
4
- before(:all) do
5
- @original_drivers = Capybara::Screenshot.registered_drivers
6
- end
4
+ describe ".register_driver" do
5
+ before(:all) do
6
+ @original_drivers = Capybara::Screenshot.registered_drivers.dup
7
+ end
7
8
 
8
- after(:all) do
9
- Capybara::Screenshot.registered_drivers = @original_drivers
10
- end
9
+ after(:all) do
10
+ Capybara::Screenshot.registered_drivers = @original_drivers
11
+ end
11
12
 
12
- describe ".register_driver" do
13
13
  it 'should store driver with block' do
14
14
  block = lambda {}
15
15
  Capybara::Screenshot.register_driver :foo, &block
@@ -17,4 +17,27 @@ describe Capybara::Screenshot do
17
17
  Capybara::Screenshot.registered_drivers[:foo].should eq block
18
18
  end
19
19
  end
20
+
21
+ describe ".register_filename_prefix_formatter" do
22
+ before(:all) do
23
+ @original_formatters = Capybara::Screenshot.filename_prefix_formatters.dup
24
+ end
25
+
26
+ after(:all) do
27
+ Capybara::Screenshot.filename_prefix_formatters = @original_formatters
28
+ end
29
+
30
+ it 'should store test type with block' do
31
+ block = lambda { |arg| }
32
+ Capybara::Screenshot.register_filename_prefix_formatter :foo, &block
33
+
34
+ Capybara::Screenshot.filename_prefix_formatters[:foo].should eq block
35
+ end
36
+ end
37
+
38
+ describe ".filename_prefix_for" do
39
+ it 'should return "screenshot" for undefined formatter' do
40
+ Capybara::Screenshot.filename_prefix_for(:foo, mock('test')).should eq 'screenshot'
41
+ end
42
+ end
20
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-screenshot
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-20 00:00:00.000000000 Z
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
@@ -98,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
98
  version: '0'
99
99
  segments:
100
100
  - 0
101
- hash: 1087741615674543132
101
+ hash: 866292530487117762
102
102
  required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  none: false
104
104
  requirements:
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  version: '0'
108
108
  segments:
109
109
  - 0
110
- hash: 1087741615674543132
110
+ hash: 866292530487117762
111
111
  requirements: []
112
112
  rubyforge_project: capybara-screenshot
113
113
  rubygems_version: 1.8.24