capybara-screenshot 0.3.22 → 1.0.0

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.
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
@@ -0,0 +1,100 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ capybara-screenshot (1.0.0)
5
+ capybara (>= 1.0, < 3)
6
+ colored
7
+ launchy
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ addressable (2.3.6)
13
+ appraisal (1.0.2)
14
+ bundler
15
+ rake
16
+ thor (>= 0.14.0)
17
+ aruba (0.6.1)
18
+ childprocess (>= 0.3.6)
19
+ cucumber (>= 1.1.1)
20
+ rspec-expectations (>= 2.7.0)
21
+ builder (3.2.2)
22
+ capybara (2.4.1)
23
+ mime-types (>= 1.16)
24
+ nokogiri (>= 1.3.3)
25
+ rack (>= 1.0.0)
26
+ rack-test (>= 0.5.4)
27
+ xpath (~> 2.0)
28
+ childprocess (0.5.3)
29
+ ffi (~> 1.0, >= 1.0.11)
30
+ colored (1.2)
31
+ colorize (0.5.8)
32
+ cucumber (1.3.17)
33
+ builder (>= 2.1.2)
34
+ diff-lcs (>= 1.1.3)
35
+ gherkin (~> 2.12)
36
+ multi_json (>= 1.7.5, < 2.0)
37
+ multi_test (>= 0.1.1)
38
+ diff-lcs (1.2.5)
39
+ ffi (1.9.3)
40
+ gherkin (2.12.2)
41
+ multi_json (~> 1.3)
42
+ gherkin-ruby (0.2.1)
43
+ launchy (2.4.2)
44
+ addressable (~> 2.3)
45
+ mime-types (2.3)
46
+ mini_portile (0.6.0)
47
+ minitest (5.4.1)
48
+ multi_json (1.10.1)
49
+ multi_test (0.1.1)
50
+ nokogiri (1.6.3.1)
51
+ mini_portile (= 0.6.0)
52
+ power_assert (0.1.3)
53
+ rack (1.5.2)
54
+ rack-protection (1.5.3)
55
+ rack
56
+ rack-test (0.6.2)
57
+ rack (>= 1.0)
58
+ rake (10.3.2)
59
+ rspec (3.1.0)
60
+ rspec-core (~> 3.1.0)
61
+ rspec-expectations (~> 3.1.0)
62
+ rspec-mocks (~> 3.1.0)
63
+ rspec-core (3.1.3)
64
+ rspec-support (~> 3.1.0)
65
+ rspec-expectations (3.1.1)
66
+ diff-lcs (>= 1.2.0, < 2.0)
67
+ rspec-support (~> 3.1.0)
68
+ rspec-mocks (3.1.0)
69
+ rspec-support (~> 3.1.0)
70
+ rspec-support (3.1.0)
71
+ sinatra (1.4.5)
72
+ rack (~> 1.4)
73
+ rack-protection (~> 1.4)
74
+ tilt (~> 1.3, >= 1.3.4)
75
+ spinach (0.8.0)
76
+ colorize (= 0.5.8)
77
+ gherkin-ruby (~> 0.2.1)
78
+ test-unit (3.0.1)
79
+ power_assert
80
+ thor (0.19.1)
81
+ tilt (1.4.1)
82
+ timecop (0.7.1)
83
+ xpath (2.0.0)
84
+ nokogiri (~> 1.3)
85
+
86
+ PLATFORMS
87
+ ruby
88
+
89
+ DEPENDENCIES
90
+ appraisal
91
+ aruba
92
+ capybara-screenshot!
93
+ cucumber
94
+ minitest
95
+ rake
96
+ rspec
97
+ sinatra
98
+ spinach (= 0.8.0)
99
+ test-unit
100
+ timecop
@@ -6,6 +6,7 @@ module Capybara
6
6
  attr_accessor :filename_prefix_formatters
7
7
  attr_accessor :append_timestamp
8
8
  attr_accessor :webkit_options
9
+ attr_writer :final_session_name
9
10
  end
10
11
 
11
12
  self.autosave_on_failure = true
@@ -69,6 +70,10 @@ module Capybara
69
70
  def self.register_filename_prefix_formatter(test_type, &block)
70
71
  self.filename_prefix_formatters[test_type] = block
71
72
  end
73
+
74
+ def self.final_session_name
75
+ @final_session_name || Capybara.session_name || :default
76
+ end
72
77
  end
73
78
  end
74
79
 
@@ -10,5 +10,17 @@ module Capybara
10
10
  def screenshot_and_open_image
11
11
  Capybara::Screenshot.screenshot_and_open_image
12
12
  end
13
+
14
+ def using_session_with_screenshot(name)
15
+ using_session_without_screenshot(name) do
16
+ original_session_name = Capybara.session_name
17
+ Capybara::Screenshot.final_session_name = name
18
+ yield
19
+ Capybara::Screenshot.final_session_name = original_session_name
20
+ end
21
+ end
22
+
23
+ alias_method :using_session_without_screenshot, :using_session
24
+ alias_method :using_session, :using_session_with_screenshot
13
25
  end
14
26
  end
@@ -1,19 +1,25 @@
1
+ Before do |scenario|
2
+ Capybara::Screenshot.final_session_name = nil
3
+ end
4
+
1
5
  After do |scenario|
2
6
  if Capybara::Screenshot.autosave_on_failure && scenario.failed?
3
- filename_prefix = Capybara::Screenshot.filename_prefix_for(:cucumber, scenario)
7
+ Capybara.using_session(Capybara::Screenshot.final_session_name) do
8
+ filename_prefix = Capybara::Screenshot.filename_prefix_for(:cucumber, scenario)
4
9
 
5
- saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
6
- saver.save
7
- saver.output_screenshot_path
10
+ saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
11
+ saver.save
12
+ saver.output_screenshot_path
8
13
 
9
- # Trying to embed the screenshot into our output."
10
- if File.exist?(saver.screenshot_path)
11
- require "base64"
12
- #encode the image into it's base64 representation
13
- image = open(saver.screenshot_path, 'rb') {|io|io.read}
14
- encoded_img = Base64.encode64(image)
15
- #this will embed the image in the HTML report, embed() is defined in cucumber
16
- embed("data:image/png;base64,#{encoded_img}", 'image/png', "Screenshot of the error")
14
+ # Trying to embed the screenshot into our output."
15
+ if File.exist?(saver.screenshot_path)
16
+ require "base64"
17
+ #encode the image into it's base64 representation
18
+ image = open(saver.screenshot_path, 'rb') {|io|io.read}
19
+ encoded_img = Base64.encode64(image)
20
+ #this will embed the image in the HTML report, embed() is defined in cucumber
21
+ embed("data:image/png;base64,#{encoded_img}", 'image/png', "Screenshot of the error")
22
+ end
17
23
  end
18
24
  end
19
25
  end
@@ -1,25 +1,24 @@
1
- method = if ActionDispatch::IntegrationTest.respond_to? :teardown
2
- :teardown
3
- elsif ActionDispatch::IntegrationTest.respond_to? :add_tear_down_hook
4
- :add_tear_down_hook
5
- end
6
-
7
- unless method.nil?
8
- begin
9
- ActionDispatch::IntegrationTest.send method do |context|
10
- # by adding the argument context, MiniTest passes the context of the test
11
- # which has an instance variable @passed indicating success / failure
12
- failed = !context.passed?
1
+ module Capybara::Screenshot::MiniTestPlugin
2
+ def before_setup
3
+ super
4
+ Capybara::Screenshot.final_session_name = nil
5
+ end
13
6
 
14
- if Capybara::Screenshot.autosave_on_failure && failed
15
- filename_prefix = Capybara::Screenshot.filename_prefix_for(:minitest, context)
7
+ def after_teardown
8
+ super
9
+ if self.class.ancestors.map(&:to_s).include?('ActionDispatch::IntegrationTest') &&
10
+ Capybara::Screenshot.autosave_on_failure && !passed?
11
+ Capybara.using_session(Capybara::Screenshot.final_session_name) do
12
+ filename_prefix = Capybara::Screenshot.filename_prefix_for(:minitest, self)
16
13
 
17
14
  saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
18
15
  saver.save
19
16
  saver.output_screenshot_path
20
17
  end
21
18
  end
22
- rescue NoMethodError
23
- # do nothing, teardown for minitest not available
24
19
  end
25
20
  end
21
+
22
+ class MiniTest::Unit::TestCase
23
+ include Capybara::Screenshot::MiniTestPlugin
24
+ end
@@ -34,10 +34,11 @@ module Capybara
34
34
  # gets injected into the appropriate formatter.
35
35
 
36
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
37
+ "RSpec::Core::Formatters::ProgressFormatter" => Capybara::Screenshot::RSpec::TextReporter,
38
+ "RSpec::Core::Formatters::DocumentationFormatter" => Capybara::Screenshot::RSpec::TextReporter,
39
+ "RSpec::Core::Formatters::HtmlFormatter" => Capybara::Screenshot::RSpec::HtmlLinkReporter,
40
+ "RSpec::Core::Formatters::TextMateFormatter" => Capybara::Screenshot::RSpec::TextMateLinkReporter, # RSpec 2
41
+ "RSpec::Mate::Formatters::TextMateFormatter" => Capybara::Screenshot::RSpec::TextMateLinkReporter # RSpec 3
41
42
  }
42
43
 
43
44
  class << self
@@ -45,14 +46,17 @@ module Capybara
45
46
 
46
47
  def after_failed_example(example)
47
48
  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
49
+ Capybara.using_session(Capybara::Screenshot.final_session_name) do
50
+ if Capybara.page.current_url != '' && Capybara::Screenshot.autosave_on_failure && example.exception
51
+ filename_prefix = Capybara::Screenshot.filename_prefix_for(:rspec, example)
52
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?
53
+ saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
54
+ saver.save
55
+
56
+ example.metadata[:screenshot] = {}
57
+ example.metadata[:screenshot][:html] = saver.html_path if saver.html_saved?
58
+ example.metadata[:screenshot][:image] = saver.screenshot_path if saver.screenshot_saved?
59
+ end
56
60
  end
57
61
  end
58
62
  end
@@ -64,6 +68,10 @@ module Capybara
64
68
  end
65
69
 
66
70
  RSpec.configure do |config|
71
+ config.before do
72
+ Capybara::Screenshot.final_session_name = nil
73
+ end
74
+
67
75
  config.after do |example_from_block_arg|
68
76
  # RSpec 3 no longer defines `example`, but passes the example as block argument instead
69
77
  example = config.respond_to?(:expose_current_running_example_as) ? example_from_block_arg : self.example
@@ -5,15 +5,33 @@ module Capybara
5
5
  module RSpec
6
6
  module TextReporter
7
7
  extend BaseReporter
8
- enhance_with_screenshot :dump_failure_info
8
+
9
+ if ::RSpec::Version::STRING.to_i <= 2
10
+ enhance_with_screenshot :dump_failure_info
11
+ else
12
+ enhance_with_screenshot :example_failed
13
+ end
9
14
 
10
15
  def dump_failure_info_with_screenshot(example)
11
- dump_failure_info_without_screenshot(example)
12
- return unless (screenshot = example.metadata[:screenshot])
16
+ dump_failure_info_without_screenshot example
17
+ output_screenshot_info(example)
18
+ end
13
19
 
20
+ def example_failed_with_screenshot(notification)
21
+ example_failed_without_screenshot notification
22
+ output_screenshot_info(notification.example)
23
+ end
24
+
25
+ private
26
+ def output_screenshot_info(example)
27
+ return unless (screenshot = example.metadata[:screenshot])
14
28
  output.puts(long_padding + "HTML screenshot: #{screenshot[:html]}".yellow) if screenshot[:html]
15
29
  output.puts(long_padding + "Image screenshot: #{screenshot[:image]}".yellow) if screenshot[:image]
16
30
  end
31
+
32
+ def long_padding
33
+ " "
34
+ end
17
35
  end
18
36
  end
19
37
  end
@@ -1,8 +1,24 @@
1
- Spinach.hooks.on_failed_step do |step_data, exception, location, step_definitions|
2
- if Capybara::Screenshot.autosave_on_failure
3
- filename_prefix = Capybara::Screenshot.filename_prefix_for(:spinach, step_data)
4
- saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
5
- saver.save
6
- saver.output_screenshot_path
1
+ Spinach.hooks.before_scenario do |scenario|
2
+ Capybara::Screenshot.final_session_name = nil
3
+ end
4
+
5
+ module Capybara::Screenshot::Spinach
6
+ def self.fail_with_screenshot(step_data, exception, location, step_definitions)
7
+ if Capybara::Screenshot.autosave_on_failure
8
+ Capybara.using_session(Capybara::Screenshot.final_session_name) do
9
+ filename_prefix = Capybara::Screenshot.filename_prefix_for(:spinach, step_data)
10
+ saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
11
+ saver.save
12
+ saver.output_screenshot_path
13
+ end
14
+ end
7
15
  end
8
16
  end
17
+
18
+ Spinach.hooks.on_failed_step do |*args|
19
+ Capybara::Screenshot::Spinach.fail_with_screenshot(*args)
20
+ end
21
+
22
+ Spinach.hooks.on_error_step do |*args|
23
+ Capybara::Screenshot::Spinach.fail_with_screenshot(*args)
24
+ end
@@ -1,21 +1,39 @@
1
1
  require 'test/unit/testresult'
2
- Test::Unit::TestResult.class_eval do
3
2
 
3
+ module Capybara::Screenshot
4
+ class << self
5
+ attr_accessor :testunit_paths
6
+ end
7
+
8
+ self.testunit_paths = [%r{test/integration}]
9
+ end
10
+
11
+ Test::Unit::TestCase.class_eval do
12
+ setup do
13
+ Capybara::Screenshot.final_session_name = nil
14
+ end
15
+ end
16
+
17
+ Test::Unit::TestResult.class_eval do
4
18
  private
5
19
 
6
20
  def notify_fault_with_screenshot(fault, *args)
7
21
  notify_fault_without_screenshot fault, *args
8
- if fault.location.first =~ %r{test/integration}
22
+ is_integration_test = fault.location.any? do |location|
23
+ Capybara::Screenshot.testunit_paths.any? { |path| location.match(path) }
24
+ end
25
+ if is_integration_test
9
26
  if Capybara::Screenshot.autosave_on_failure
10
- filename_prefix = Capybara::Screenshot.filename_prefix_for(:testunit, fault)
27
+ Capybara.using_session(Capybara::Screenshot.final_session_name) do
28
+ filename_prefix = Capybara::Screenshot.filename_prefix_for(:testunit, fault)
11
29
 
12
- saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
13
- saver.save
14
- saver.output_screenshot_path
30
+ saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
31
+ saver.save
32
+ saver.output_screenshot_path
33
+ end
15
34
  end
16
35
  end
17
36
  end
18
37
  alias notify_fault_without_screenshot notify_fault
19
38
  alias notify_fault notify_fault_with_screenshot
20
-
21
39
  end
@@ -1,5 +1,5 @@
1
1
  module Capybara
2
2
  module Screenshot
3
- VERSION = "0.3.22"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -0,0 +1,64 @@
1
+ require "spec_helper"
2
+ require "gherkin"
3
+
4
+ # If Gherkin Parser is not available, then we are using an old Spinach Gemset
5
+ if defined?(Gherkin::Parser::Parser)
6
+ describe "Using Capybara::Screenshot with Cucumber" do
7
+ include Aruba::Api
8
+ include CommonSetup
9
+
10
+ before do
11
+ clean_current_dir
12
+ end
13
+
14
+ def run_failing_case(failure_message, code)
15
+ write_file('features/support/env.rb', <<-RUBY)
16
+ #{ensure_load_paths_valid}
17
+ require 'cucumber/support/env.rb'
18
+ #{setup_test_app}
19
+ RUBY
20
+
21
+ write_file('features/step_definitions/step_definitions.rb', <<-RUBY)
22
+ %w(lib spec).each do |include_folder|
23
+ $LOAD_PATH.unshift(File.join('#{gem_root}', include_folder))
24
+ end
25
+ require 'cucumber/step_definitions/step_definitions.rb'
26
+ RUBY
27
+
28
+ write_file('features/cucumber.feature', code)
29
+ cmd = 'bundle exec cucumber'
30
+ run_simple cmd, false
31
+ expect(output_from(cmd)).to match failure_message
32
+ end
33
+
34
+ it "saves a screenshot on failure" do
35
+ run_failing_case(%q{Unable to find link or button "you'll never find me"}, <<-CUCUMBER)
36
+ Feature: Failure
37
+ Scenario: Failure
38
+ Given I visit "/"
39
+ And I click on a missing link
40
+ CUCUMBER
41
+ check_file_content('tmp/my_screenshot.html', 'This is the root page', true)
42
+ end
43
+
44
+ it "saves a screenshot on an error" do
45
+ run_failing_case(%q{you can't handle me}, <<-CUCUMBER)
46
+ Feature: Failure
47
+ Scenario: Failure
48
+ Given I visit "/"
49
+ And I trigger an unhandled exception
50
+ CUCUMBER
51
+ check_file_content('tmp/my_screenshot.html', 'This is the root page', true)
52
+ end
53
+
54
+ it "saves a screenshot for the correct session for failures using_session" do
55
+ run_failing_case(%q{Unable to find link or button "you'll never find me"}, <<-CUCUMBER)
56
+ Feature: Failure
57
+ Scenario: Failure in different session
58
+ Given I visit "/"
59
+ And I click on a missing link on a different page in a different session
60
+ CUCUMBER
61
+ check_file_content('tmp/my_screenshot.html', 'This is a different page', true)
62
+ end
63
+ end
64
+ end