Class: Capybara::Screenshot::Diff::ImageCompare
- Inherits:
-
Object
- Object
- Capybara::Screenshot::Diff::ImageCompare
- Defined in:
- lib/capybara/screenshot/diff/image_compare.rb
Overview
Compare two image and determine if they are equal, different, or within some comparison range considering color values and difference area size.
Constant Summary collapse
- TOLERABLE_OPTIONS =
[:tolerance, :color_distance_limit, :shift_distance_limit, :area_size_limit].freeze
Instance Attribute Summary collapse
-
#base_image_path ⇒ Object
readonly
Returns the value of attribute base_image_path.
-
#difference ⇒ Object
readonly
Returns the value of attribute difference.
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#driver_options ⇒ Object
readonly
Returns the value of attribute driver_options.
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#image_path ⇒ Object
readonly
Returns the value of attribute image_path.
Instance Method Summary collapse
-
#different? ⇒ Boolean
Compare the two image referenced by this object, and return ‘true` if they are different, and `false` if they are the same.
-
#initialize(image_path, base_image_path, options = {}) ⇒ ImageCompare
constructor
A new instance of ImageCompare.
- #processed ⇒ Object
- #processed? ⇒ Boolean
-
#quick_equal? ⇒ Boolean
Compare the two image files and return ‘true` or `false` as quickly as possible.
- #reporter ⇒ Object
Constructor Details
#initialize(image_path, base_image_path, options = {}) ⇒ ImageCompare
Returns a new instance of ImageCompare.
19 20 21 22 23 24 25 26 |
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 19 def initialize(image_path, base_image_path, = {}) @image_path = Pathname.new(image_path) @base_image_path = Pathname.new(base_image_path) @driver_options = .dup @driver = Drivers.for(@driver_options) end |
Instance Attribute Details
#base_image_path ⇒ Object (readonly)
Returns the value of attribute base_image_path.
16 17 18 |
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 16 def base_image_path @base_image_path end |
#difference ⇒ Object
Returns the value of attribute difference.
17 18 19 |
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 17 def difference @difference end |
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
15 16 17 |
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 15 def driver @driver end |
#driver_options ⇒ Object (readonly)
Returns the value of attribute driver_options.
15 16 17 |
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 15 def @driver_options end |
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
17 18 19 |
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 17 def @error_message end |
#image_path ⇒ Object (readonly)
Returns the value of attribute image_path.
16 17 18 |
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 16 def image_path @image_path end |
Instance Method Details
#different? ⇒ Boolean
Compare the two image referenced by this object, and return ‘true` if they are different, and `false` if they are the same.
58 59 60 |
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 58 def different? processed.difference.different? end |
#processed ⇒ Object
73 74 75 76 77 |
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 73 def processed self.difference = find_difference unless processed? @error_message ||= reporter.generate self end |
#processed? ⇒ Boolean
69 70 71 |
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 69 def processed? !!difference end |
#quick_equal? ⇒ Boolean
Compare the two image files and return ‘true` or `false` as quickly as possible. Return falsely if the old file does not exist or the image dimensions do not match.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 30 def quick_equal? require_images_exists! # NOTE: This is very fuzzy logic, but so far it's helps to support current performance. return true if new_file_size == old_file_size comparison = load_and_process_images unless driver.same_dimension?(comparison) self.difference = build_failed_difference(comparison, {different_dimensions: true}) return false end if driver.same_pixels?(comparison) self.difference = build_no_difference(comparison) return true end # NOTE: Could not make any difference to be tolerable, so skip and return as not equal. return false if self.difference = driver.find_difference_region(comparison) !difference.different? end |