capybara-screenshot-diff 0.9.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea2eb0302f1b07c925218f42f50e34a92ea53d6a
4
- data.tar.gz: bf0641106aec6b525fd13912b7863b616a8d16df
3
+ metadata.gz: 6ae86364bfe6e5bbcfe03ce6c9b6a0a39c60b2ac
4
+ data.tar.gz: 803b1d3d309c2a4e93e2de388c5666d03712325a
5
5
  SHA512:
6
- metadata.gz: 61c6f15802542903bee2236fb7b72b65fae467b45e885515f15bac7da2dd60d4faa6a2b03e9de90450a1f66710c9c853c31c75e284befae8adab177d89cf3ac4
7
- data.tar.gz: d1a8ac84a126b5227d00e43e37115a27547ca9877d4e6ec4da0cd1c37980899b8da441e39c50c6de51775359604c99a497a6a0101f589e75e23b123bebcc35f6
6
+ metadata.gz: 73d1f23776d8afc8315b7d518b78448a2215b526f7f50088fc809df70359ccb27b1f635b6e905d080ca286596e98825f98f77cb2a303c59b3c54eba12de8e3fe
7
+ data.tar.gz: 8534dcb0cf6b3a72b26d42f010d255f35afe38e9f4b8defc34396227932b93fe6baf19f6ba71f09258d4ba09943266239b9d7f924b226c99596b8d6ca6873919
data/.rubocop_todo.yml CHANGED
@@ -13,7 +13,7 @@ Metrics/AbcSize:
13
13
  # Offense count: 1
14
14
  # Configuration parameters: CountComments.
15
15
  Metrics/ClassLength:
16
- Max: 181
16
+ Max: 182
17
17
 
18
18
  # Offense count: 3
19
19
  Metrics/CyclomaticComplexity:
@@ -1,3 +1,4 @@
1
+ require 'capybara/dsl'
1
2
  require 'capybara/screenshot/diff/version'
2
3
  require 'capybara/screenshot/diff/image_compare'
3
4
  require 'capybara/screenshot/diff/test_methods'
@@ -32,9 +33,33 @@ module Capybara
32
33
 
33
34
  # Module to track screen shot changes
34
35
  module Diff
36
+ include Capybara::DSL
37
+ include Capybara::Screenshot::Os
38
+
35
39
  mattr_accessor :area_size_limit
36
40
  mattr_accessor :color_distance_limit
37
41
  mattr_accessor(:enabled) { true }
42
+
43
+ def self.included(clas)
44
+ clas.include TestMethods
45
+ clas.setup do
46
+ if Capybara::Screenshot.window_size
47
+ if selenium?
48
+ page.driver.browser.manage.window.resize_to(*Capybara::Screenshot.window_size)
49
+ elsif poltergeist?
50
+ page.driver.resize(*Capybara::Screenshot.window_size)
51
+ end
52
+ end
53
+ end
54
+
55
+ clas.teardown do
56
+ if Capybara::Screenshot::Diff.enabled && @test_screenshots
57
+ test_screenshot_errors = @test_screenshots
58
+ .map { |caller, name, compare| assert_image_not_changed(caller, name, compare) }.compact
59
+ fail(test_screenshot_errors.join("\n\n")) if test_screenshot_errors.any?
60
+ end
61
+ end
62
+ end
38
63
  end
39
64
  end
40
65
  end
@@ -3,6 +3,8 @@ require 'chunky_png'
3
3
  module Capybara
4
4
  module Screenshot
5
5
  module Diff
6
+ # Compare two images and determine if they are equal, different, or within som comparison
7
+ # range considering color values and difference area size.
6
8
  class ImageCompare
7
9
  include ChunkyPNG::Color
8
10
 
@@ -21,10 +23,11 @@ module Capybara
21
23
  reset
22
24
  end
23
25
 
26
+ # Resets the calculated data about the comparison with regard to the "new_image".
27
+ # Data about the original image is kept.
24
28
  def reset
25
29
  @max_color_distance = @color_distance_limit ? 0 : nil
26
30
  @left = @top = @right = @bottom = nil
27
- @_old_filesize = nil
28
31
  end
29
32
 
30
33
  # Compare the two image files and return `true` or `false` as quickly as possible.
@@ -18,7 +18,48 @@ module Capybara
18
18
  }()
19
19
  EOF
20
20
 
21
- def reduce_retina_image_size(file_name)
21
+ def take_stable_screenshot(comparison, color_distance_limit:, area_size_limit:)
22
+ input = prepare_page_for_screenshot
23
+ previous_file_name = comparison.old_file_name
24
+ screenshot_started_at = last_image_change_at = Time.now
25
+ loop.with_index do |_x, i|
26
+ take_right_size_screenshot(comparison)
27
+
28
+ break unless Capybara::Screenshot.stability_time_limit
29
+ if comparison.quick_equal?
30
+ clean_stabilization_images(comparison.new_file_name)
31
+ break
32
+ end
33
+ comparison.reset
34
+
35
+ if previous_file_name
36
+ stabilization_comparison =
37
+ ImageCompare.new(comparison.new_file_name, previous_file_name,
38
+ color_distance_limit: color_distance_limit, area_size_limit: area_size_limit)
39
+ if stabilization_comparison.quick_equal?
40
+ if (Time.now - last_image_change_at) > Capybara::Screenshot.stability_time_limit
41
+ clean_stabilization_images(comparison.new_file_name)
42
+ break
43
+ end
44
+ next
45
+ else
46
+ last_image_change_at = Time.now
47
+ end
48
+
49
+ check_max_wait_time(comparison, screenshot_started_at)
50
+ end
51
+
52
+ previous_file_name = "#{comparison.new_file_name.chomp('.png')}_x#{format('%02i', i)}.png~"
53
+
54
+ FileUtils.mv comparison.new_file_name, previous_file_name
55
+ end
56
+ ensure
57
+ input.click if input
58
+ end
59
+
60
+ private
61
+
62
+ private def reduce_retina_image_size(file_name)
22
63
  return if !ON_MAC || !selenium? || !Capybara::Screenshot.window_size
23
64
  saved_image = ChunkyPNG::Image.from_file(file_name)
24
65
  width = Capybara::Screenshot.window_size[0]
@@ -28,15 +69,15 @@ module Capybara
28
69
  resized_image.save(file_name)
29
70
  end
30
71
 
31
- def stabilization_images(base_file)
72
+ private def stabilization_images(base_file)
32
73
  Dir["#{base_file.chomp('.png')}_x*.png~"]
33
74
  end
34
75
 
35
- def clean_stabilization_images(base_file)
76
+ private def clean_stabilization_images(base_file)
36
77
  FileUtils.rm stabilization_images(base_file)
37
78
  end
38
79
 
39
- def prepare_page_for_screenshot
80
+ private def prepare_page_for_screenshot
40
81
  assert_images_loaded
41
82
  if Capybara::Screenshot.blur_active_element
42
83
  active_element = execute_script(<<-JS)
@@ -52,7 +93,7 @@ module Capybara
52
93
  input
53
94
  end
54
95
 
55
- def take_right_size_screenshot(comparison)
96
+ private def take_right_size_screenshot(comparison)
56
97
  save_screenshot(comparison.new_file_name)
57
98
 
58
99
  # TODO(uwe): Remove when chromedriver takes right size screenshots
@@ -60,48 +101,13 @@ module Capybara
60
101
  # ODOT
61
102
  end
62
103
 
63
- def take_stable_screenshot(comparison, color_distance_limit: , area_size_limit: )
64
- input = prepare_page_for_screenshot
65
- previous_file_name = comparison.old_file_name
66
- screenshot_started_at = last_image_change_at = Time.now
67
- loop.with_index do |_x, i|
68
- take_right_size_screenshot(comparison)
69
-
70
- break unless Capybara::Screenshot.stability_time_limit
71
- if comparison.quick_equal?
72
- clean_stabilization_images(comparison.new_file_name)
73
- break
74
- end
75
- comparison.reset
76
-
77
- if previous_file_name
78
- stabilization_comparison =
79
- ImageCompare.new(comparison.new_file_name, previous_file_name,
80
- color_distance_limit: color_distance_limit, area_size_limit: area_size_limit)
81
- if stabilization_comparison.quick_equal?
82
- if (Time.now - last_image_change_at) > Capybara::Screenshot.stability_time_limit
83
- clean_stabilization_images(comparison.new_file_name)
84
- break
85
- end
86
- next
87
- else
88
- last_image_change_at = Time.now
89
- end
90
-
91
- assert (Time.now - screenshot_started_at) < Capybara.default_max_wait_time,
92
- "Could not get stable screenshot within #{Capybara.default_max_wait_time}s\n" \
104
+ private def check_max_wait_time(comparison, screenshot_started_at)
105
+ assert (Time.now - screenshot_started_at) < Capybara.default_max_wait_time,
106
+ "Could not get stable screenshot within #{Capybara.default_max_wait_time}s\n" \
93
107
  "#{stabilization_images(comparison.new_file_name).join("\n")}"
94
- end
95
-
96
- previous_file_name = "#{comparison.new_file_name.chomp('.png')}_x#{format('%02i', i)}.png~"
97
-
98
- FileUtils.mv comparison.new_file_name, previous_file_name
99
- end
100
- ensure
101
- input.click if input
102
108
  end
103
109
 
104
- def assert_images_loaded(timeout: Capybara.default_max_wait_time)
110
+ private def assert_images_loaded(timeout: Capybara.default_max_wait_time)
105
111
  return unless respond_to? :evaluate_script
106
112
  start = Time.now
107
113
  loop do
@@ -75,20 +75,20 @@ module Capybara
75
75
  file_name = "#{Screenshot.screenshot_area_abs}/#{name}.png"
76
76
 
77
77
  FileUtils.mkdir_p File.dirname(file_name)
78
- comparison = ImageCompare.new(file_name, dimensions: Screenshot.window_size,
79
- color_distance_limit: color_distance_limit, area_size_limit: area_size_limit)
78
+ comparison = ImageCompare.new(file_name,
79
+ dimensions: Screenshot.window_size, color_distance_limit: color_distance_limit,
80
+ area_size_limit: area_size_limit)
80
81
  checkout_vcs(name, comparison)
81
82
  take_stable_screenshot(comparison, color_distance_limit: color_distance_limit,
82
- area_size_limit: area_size_limit)
83
+ area_size_limit: area_size_limit)
83
84
  return unless comparison.old_file_exists?
84
85
  (@test_screenshots ||= []) << [caller(1..1).first, name, comparison]
85
86
  end
86
87
 
87
88
  def window_size_is_wrong?
88
89
  selenium? && Screenshot.window_size &&
89
- (!page.driver.chrome? || ON_WINDOWS) && # TODO(uwe): Allow for Chrome when it works
90
90
  page.driver.browser.manage.window.size !=
91
- Selenium::WebDriver::Dimension.new(*Screenshot.window_size)
91
+ ::Selenium::WebDriver::Dimension.new(*Screenshot.window_size)
92
92
  end
93
93
 
94
94
  def assert_image_not_changed(caller, name, comparison)
@@ -113,30 +113,3 @@ module Capybara
113
113
  end
114
114
  end
115
115
  # rubocop:enable Metrics/ClassLength
116
-
117
- module ActionDispatch
118
- class IntegrationTest
119
- prepend Capybara::Screenshot::Diff::TestMethods
120
-
121
- setup do
122
- if Capybara::Screenshot.window_size
123
- if selenium?
124
- # TODO(uwe): Enable for Chrome and non-windows when it works)
125
- if !page.driver.chrome? || ON_WINDOWS
126
- page.driver.browser.manage.window.resize_to(*Capybara::Screenshot.window_size)
127
- end
128
- elsif poltergeist?
129
- page.driver.resize(*Capybara::Screenshot.window_size)
130
- end
131
- end
132
- end
133
-
134
- teardown do
135
- if Capybara::Screenshot::Diff.enabled && @test_screenshots
136
- test_screenshot_errors = @test_screenshots
137
- .map { |caller, name, compare| assert_image_not_changed(caller, name, compare) }.compact
138
- fail(test_screenshot_errors.join("\n\n")) if test_screenshot_errors.any?
139
- end
140
- end
141
- end
142
- end
@@ -3,7 +3,7 @@
3
3
  module Capybara
4
4
  module Screenshot
5
5
  module Diff
6
- VERSION = '0.9.1'.freeze
6
+ VERSION = '0.10.0'.freeze
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-screenshot-diff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uwe Kubosch
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-01 00:00:00.000000000 Z
11
+ date: 2017-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack