Class: Capybara::Screenshot::Diff::ImageCompare

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @image_path = Pathname.new(image_path)
  @base_image_path = Pathname.new(base_image_path)

  @driver_options = options.dup

  @driver = Drivers.for(@driver_options)
end

Instance Attribute Details

#base_image_pathObject (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

#differenceObject

Returns the value of attribute difference.



17
18
19
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 17

def difference
  @difference
end

#driverObject (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_optionsObject (readonly)

Returns the value of attribute driver_options.



15
16
17
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 15

def driver_options
  @driver_options
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



17
18
19
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 17

def error_message
  @error_message
end

#image_pathObject (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.

Returns:

  • (Boolean)


58
59
60
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 58

def different?
  processed.difference.different?
end

#processedObject



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

Returns:

  • (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.

Returns:

  • (Boolean)


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 without_tolerable_options?

  self.difference = driver.find_difference_region(comparison)

  !difference.different?
end

#reporterObject



62
63
64
65
66
67
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 62

def reporter
  @reporter ||= begin
    current_difference = difference || build_no_difference(nil)
    Capybara::Screenshot::Diff::Reporters::Default.new(current_difference)
  end
end