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
NEW_LINE =
"\n"

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
27
28
29
30
31
32
33
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 19

def initialize(image_path, base_image_path, options = {})
  @image_path = Pathname.new(image_path)

  @new_file_name = @image_path.to_s
  @annotated_image_path = @image_path.sub_ext(".diff.png")

  @base_image_path = Pathname.new(base_image_path)

  @old_file_name = @base_image_path.to_s
  @annotated_base_image_path = @base_image_path.sub_ext(".diff.png")

  @driver_options = options.dup

  @driver = Drivers.for(@driver_options)
end

Instance Attribute Details

#annotated_base_image_pathObject (readonly)

Returns the value of attribute annotated_base_image_path.



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

def annotated_base_image_path
  @annotated_base_image_path
end

#annotated_image_pathObject (readonly)

Returns the value of attribute annotated_image_path.



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

def annotated_image_path
  @annotated_image_path
end

#base_image_pathObject (readonly)

Returns the value of attribute base_image_path.



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

def base_image_path
  @base_image_path
end

#driverObject (readonly)

Returns the value of attribute driver.



13
14
15
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 13

def driver
  @driver
end

#driver_optionsObject (readonly)

Returns the value of attribute driver_options.



13
14
15
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 13

def driver_options
  @driver_options
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



97
98
99
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 97

def error_message
  @error_message
end

#image_pathObject (readonly)

Returns the value of attribute image_path.



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

def image_path
  @image_path
end

#new_file_nameObject (readonly)

Returns the value of attribute new_file_name.



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

def new_file_name
  @new_file_name
end

#old_file_nameObject (readonly)

Returns the value of attribute old_file_name.



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

def old_file_name
  @old_file_name
end

Instance Method Details

#build_error_for_different_dimensions(comparison) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 74

def build_error_for_different_dimensions(comparison)
  change_msg = [comparison.base_image, comparison.new_image]
    .map { |i| driver.dimension(i).join("x") }
    .join(" => ")

  "Screenshot dimension has been changed for #{@new_file_name}: #{change_msg}"
end

#clean_tmp_filesObject



82
83
84
85
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 82

def clean_tmp_files
  @annotated_base_image_path.unlink if @annotated_base_image_path.exist?
  @annotated_image_path.unlink if @annotated_image_path.exist?
end

#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)


64
65
66
67
68
69
70
71
72
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 64

def different?
  @error_message = nil

  @error_message = _different?

  clean_tmp_files unless @error_message

  !@error_message.nil?
end

#image_files_exist?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 91

def image_files_exist?
  @base_image_path.exist? && @image_path.exist?
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)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 37

def quick_equal?
  @error_message = nil
  return false unless image_files_exist?
  # TODO: Confirm this change. There are screenshots with the same size, but there is a big difference
  return true if new_file_size == old_file_size

  comparison = load_and_process_images

  unless driver.same_dimension?(comparison)
    @error_message = build_error_for_different_dimensions(comparison)
    return false
  end

  return true if driver.same_pixels?(comparison)

  # Could not make any difference to be tolerable, so skip and return as not equal
  return false if without_tolerable_options?

  @difference = driver.find_difference_region(comparison)
  return true unless @difference.different?

  @error_message = @difference.inspect
  false
end

#save(image, image_path) ⇒ Object



87
88
89
# File 'lib/capybara/screenshot/diff/image_compare.rb', line 87

def save(image, image_path)
  driver.save_image_to(image, image_path.to_s)
end