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,40 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rspec/core/rake_task'
7
+
8
+ rspec_rake_task = RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task default: [:spec]
11
+
12
+ def target_gem
13
+ gem_file = ENV['BUNDLE_GEMFILE'] || ''
14
+ targets = %w(cucumber spinach rspec)
15
+
16
+ target = gem_file.match(/(#{targets.join('|')})/)
17
+ if target && targets.include?(target[1])
18
+ target[1].to_sym
19
+ else
20
+ []
21
+ end
22
+ end
23
+
24
+ namespace :travis do
25
+ task :ci => target_gem do
26
+ Rake::Task['spec'].invoke
27
+ end
28
+
29
+ task :cucumber do
30
+ rspec_rake_task.pattern = 'spec/cucumber'
31
+ end
32
+
33
+ task :spinach do
34
+ rspec_rake_task.pattern = 'spec/spinach'
35
+ end
36
+
37
+ task :rspec do
38
+ rspec_rake_task.pattern = 'spec/rspec'
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "capybara-screenshot/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "capybara-screenshot-nocolor"
7
+ s.version = Capybara::Screenshot::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Matthew O'Riordan"]
10
+ s.email = ["matthew.oriordan@gmail.com"]
11
+ s.homepage = "http://github.com/mattheworiordan/capybara-screenshot"
12
+ s.summary = %q{Automatically create snapshots when Cucumber steps fail with Capybara and Rails}
13
+ s.description = %q{When a Cucumber step fails, it is useful to create a screenshot image and HTML file of the current page}
14
+ s.license = 'MIT'
15
+
16
+ s.rubyforge_project = "capybara-screenshot"
17
+
18
+ if RUBY_VERSION < "1.9"
19
+ s.add_dependency 'capybara', ['>= 1.0', '< 2']
20
+ else
21
+ s.add_dependency 'capybara', ['>= 1.0', '< 3']
22
+ end
23
+ s.add_dependency 'launchy'
24
+
25
+ s.add_development_dependency 'rspec'
26
+ s.add_development_dependency 'timecop'
27
+ s.add_development_dependency 'cucumber'
28
+ s.add_development_dependency 'aruba'
29
+ s.add_development_dependency 'sinatra'
30
+ s.add_development_dependency 'test-unit'
31
+ s.add_development_dependency 'spinach'
32
+ s.add_development_dependency 'minitest'
33
+
34
+ s.files = `git ls-files`.split("\n")
35
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
36
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
37
+ s.require_paths = ["lib"]
38
+ end
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "appraisal"
7
+ gem "cucumber", "1.2"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "appraisal"
7
+ gem "cucumber", "1.3.0"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "appraisal"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "appraisal"
7
+ gem "rspec", "2.14"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "appraisal"
7
+ gem "rspec", "2.99"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "appraisal"
7
+ gem "rspec", "3.0"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "appraisal"
7
+ gem "spinach", "0.7"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "appraisal"
7
+ gem "spinach", "0.8.0"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,157 @@
1
+ module Capybara
2
+ module Screenshot
3
+ class << self
4
+ attr_accessor :autosave_on_failure
5
+ attr_accessor :registered_drivers
6
+ attr_accessor :filename_prefix_formatters
7
+ attr_accessor :append_timestamp
8
+ attr_accessor :append_random
9
+ attr_accessor :webkit_options
10
+ attr_writer :final_session_name
11
+ attr_accessor :prune_strategy
12
+ end
13
+
14
+ self.autosave_on_failure = true
15
+ self.registered_drivers = {}
16
+ self.filename_prefix_formatters = {}
17
+ self.append_timestamp = true
18
+ self.append_random = false
19
+ self.webkit_options = {}
20
+ self.prune_strategy = :keep_all
21
+
22
+ def self.append_screenshot_path=(value)
23
+ $stderr.puts "WARNING: Capybara::Screenshot.append_screenshot_path is deprecated. " +
24
+ "Please use Capybara::Screenshot::RSpec.add_link_to_screenshot_for_failed_examples instead."
25
+ RSpec.add_link_to_screenshot_for_failed_examples = value
26
+ end
27
+
28
+ def self.screenshot_and_save_page
29
+ saver = Saver.new(Capybara, Capybara.page)
30
+ saver.save
31
+ {:html => saver.html_path, :image => saver.screenshot_path}
32
+ end
33
+
34
+ def self.screenshot_and_open_image
35
+ require "launchy"
36
+
37
+ saver = Saver.new(Capybara, Capybara.page, false)
38
+ saver.save
39
+ Launchy.open saver.screenshot_path
40
+ {:html => nil, :image => saver.screenshot_path}
41
+ end
42
+
43
+ class << self
44
+ alias screen_shot_and_save_page screenshot_and_save_page
45
+ alias screen_shot_and_open_image screenshot_and_open_image
46
+ end
47
+
48
+ def self.filename_prefix_for(test_type, test)
49
+ filename_prefix_formatters.fetch(test_type) { |key|
50
+ filename_prefix_formatters[:default]
51
+ }.call(test)
52
+ end
53
+
54
+ def self.capybara_root
55
+ return @capybara_root if defined?(@capybara_root)
56
+ #If the path isn't set, default to the current directory
57
+ capybara_tmp_path = Capybara.save_and_open_page_path || '.'
58
+
59
+ @capybara = if defined?(::Rails)
60
+ ::Rails.root.join capybara_tmp_path
61
+ elsif defined?(Padrino)
62
+ Padrino.root capybara_tmp_path
63
+ elsif defined?(Sinatra)
64
+ File.join(Sinatra::Application.root, capybara_tmp_path)
65
+ else
66
+ capybara_tmp_path
67
+ end.to_s
68
+ end
69
+
70
+ def self.register_driver(driver, &block)
71
+ self.registered_drivers[driver] = block
72
+ end
73
+
74
+ def self.register_filename_prefix_formatter(test_type, &block)
75
+ self.filename_prefix_formatters[test_type] = block
76
+ end
77
+
78
+ def self.final_session_name
79
+ @final_session_name || Capybara.session_name || :default
80
+ end
81
+
82
+ # Prune screenshots based on prune_strategy
83
+ # Will run only once unless force:true
84
+ def self.prune(options = {})
85
+ reset_prune_history if options[:force]
86
+ Capybara::Screenshot::Pruner.new(Capybara::Screenshot.prune_strategy).prune_old_screenshots unless @pruned_previous_screenshots
87
+ @pruned_previous_screenshots = true
88
+ end
89
+
90
+ # Reset prune history allowing further prunining on next failure
91
+ def self.reset_prune_history
92
+ @pruned_previous_screenshots = nil
93
+ end
94
+ end
95
+ end
96
+
97
+ # Register driver renderers.
98
+ # The block should return `:not_supported` if a screenshot could not be saved.
99
+ Capybara::Screenshot.class_eval do
100
+ register_driver(:default) do |driver, path|
101
+ driver.render(path)
102
+ end
103
+
104
+ register_driver(:rack_test) do |driver, path|
105
+ :not_supported
106
+ end
107
+
108
+ register_driver(:mechanize) do |driver, path|
109
+ :not_supported
110
+ end
111
+
112
+ register_driver(:selenium) do |driver, path|
113
+ driver.browser.save_screenshot(path)
114
+ end
115
+
116
+ register_driver(:poltergeist) do |driver, path|
117
+ driver.render(path, :full => true)
118
+ end
119
+
120
+ register_driver(:poltergeist_billy) do |driver, path|
121
+ driver.render(path, :full => true)
122
+ end
123
+
124
+ register_driver(:webkit) do |driver, path|
125
+ if driver.respond_to?(:save_screenshot)
126
+ driver.save_screenshot(path, webkit_options)
127
+ else
128
+ driver.render(path)
129
+ end
130
+ end
131
+
132
+ register_driver(:webkit_debug) do |driver, path|
133
+ driver.render(path)
134
+ end
135
+
136
+ register_driver(:terminus) do |driver, path|
137
+ if driver.respond_to?(:save_screenshot)
138
+ driver.save_screenshot(path)
139
+ else
140
+ :not_supported
141
+ end
142
+ end
143
+ end
144
+
145
+ # Register filename prefix formatters
146
+ Capybara::Screenshot.class_eval do
147
+ register_filename_prefix_formatter(:default) do |test|
148
+ 'screenshot'
149
+ end
150
+ end
151
+
152
+ require 'capybara/dsl'
153
+ require 'capybara/util/save_and_open_page' if Capybara::VERSION.match(/^\d+/)[0] == '1' # no longer needed in Capybara version 2
154
+
155
+ require 'capybara-screenshot/saver'
156
+ require 'capybara-screenshot/capybara'
157
+ require 'capybara-screenshot/pruner'
@@ -0,0 +1,28 @@
1
+ require "capybara-screenshot"
2
+
3
+ module Capybara
4
+ module DSL
5
+ # Adds class methods to Capybara module and gets mixed into
6
+ # the current scope during Cucumber and RSpec tests
7
+
8
+ def screenshot_and_save_page
9
+ Capybara::Screenshot.screenshot_and_save_page
10
+ end
11
+
12
+ def screenshot_and_open_image
13
+ Capybara::Screenshot.screenshot_and_open_image
14
+ end
15
+
16
+ def using_session_with_screenshot(name)
17
+ using_session_without_screenshot(name) do
18
+ original_session_name = Capybara.session_name
19
+ Capybara::Screenshot.final_session_name = name
20
+ yield
21
+ Capybara::Screenshot.final_session_name = original_session_name
22
+ end
23
+ end
24
+
25
+ alias_method :using_session_without_screenshot, :using_session
26
+ alias_method :using_session, :using_session_with_screenshot
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ require "capybara-screenshot"
2
+
3
+ Before do |scenario|
4
+ Capybara::Screenshot.final_session_name = nil
5
+ end
6
+
7
+ After do |scenario|
8
+ if Capybara::Screenshot.autosave_on_failure && scenario.failed?
9
+ Capybara.using_session(Capybara::Screenshot.final_session_name) do
10
+ filename_prefix = Capybara::Screenshot.filename_prefix_for(:cucumber, scenario)
11
+
12
+ saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
13
+ saver.save
14
+ saver.output_screenshot_path
15
+
16
+ # Trying to embed the screenshot into our output."
17
+ if File.exist?(saver.screenshot_path)
18
+ require "base64"
19
+ #encode the image into it's base64 representation
20
+ image = open(saver.screenshot_path, 'rb') {|io|io.read}
21
+ encoded_img = Base64.encode64(image)
22
+ #this will embed the image in the HTML report, embed() is defined in cucumber
23
+ embed(encoded_img, 'image/png;base64', "Screenshot of the error")
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ require "capybara-screenshot"
2
+
3
+ module Capybara::Screenshot::MiniTestPlugin
4
+ def before_setup
5
+ super
6
+ Capybara::Screenshot.final_session_name = nil
7
+ end
8
+
9
+ def after_teardown
10
+ super
11
+ if self.class.ancestors.map(&:to_s).include?('ActionDispatch::IntegrationTest')
12
+ if Capybara::Screenshot.autosave_on_failure && !passed?
13
+ Capybara.using_session(Capybara::Screenshot.final_session_name) do
14
+ filename_prefix = Capybara::Screenshot.filename_prefix_for(:minitest, self)
15
+
16
+ saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
17
+ saver.save
18
+ saver.output_screenshot_path
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ class MiniTest::Unit::TestCase
26
+ include Capybara::Screenshot::MiniTestPlugin
27
+ end
@@ -0,0 +1,47 @@
1
+ module Capybara
2
+ module Screenshot
3
+ class Pruner
4
+ attr_reader :strategy
5
+
6
+ def initialize(strategy)
7
+ @strategy = strategy
8
+
9
+ @strategy_proc = case strategy
10
+ when :keep_all
11
+ -> { }
12
+ when :keep_last_run
13
+ -> { prune_with_last_run_strategy }
14
+ when Hash
15
+ raise ArgumentError, ":keep key is required" unless strategy[:keep]
16
+ raise ArgumentError, ":keep value must be number greater than zero" unless strategy[:keep].to_i > 0
17
+ -> { prune_with_numeric_strategy(strategy[:keep].to_i) }
18
+ else
19
+ fail "Invalid prune strategy #{strategy}. `:keep_all`or `{ keep: 10 }` are valid examples."
20
+ end
21
+ end
22
+
23
+ def prune_old_screenshots
24
+ strategy_proc.call
25
+ end
26
+
27
+ private
28
+ attr_reader :strategy_proc
29
+
30
+ def wildcard_path
31
+ File.expand_path('*', Screenshot.capybara_root)
32
+ end
33
+
34
+ def prune_with_last_run_strategy
35
+ FileUtils.rm_rf(Dir.glob(wildcard_path))
36
+ end
37
+
38
+ def prune_with_numeric_strategy(count)
39
+ files = Dir.glob(wildcard_path).sort_by do |file_name|
40
+ File.mtime(File.expand_path(file_name, Screenshot.capybara_root))
41
+ end
42
+
43
+ FileUtils.rm_rf(files[0...-count])
44
+ end
45
+ end
46
+ end
47
+ end