Class: Capybara::Screenshot::Diff::Reporters::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/screenshot/diff/reporters/default.rb

Constant Summary collapse

DIFF_COLOR =
[255, 0, 0, 255].freeze
SKIP_COLOR =
[255, 192, 0, 255].freeze
NEW_LINE =
"\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(difference) ⇒ Default

Returns a new instance of Default.



8
9
10
11
12
13
14
15
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 8

def initialize(difference)
  @difference = difference

  screenshot_format = difference.comparison.options[:screenshot_format] || comparison.new_image_path.extname.slice(1..-1)
  @annotated_image_path = comparison.new_image_path.sub_ext(".diff.#{screenshot_format}")
  @annotated_base_image_path = comparison.base_image_path.sub_ext(".diff.#{screenshot_format}")
  @heatmap_diff_path = comparison.new_image_path.sub_ext(".heatmap.diff.#{screenshot_format}")
end

Instance Attribute Details

#annotated_base_image_pathObject (readonly)

Returns the value of attribute annotated_base_image_path.



6
7
8
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 6

def annotated_base_image_path
  @annotated_base_image_path
end

#annotated_image_pathObject (readonly)

Returns the value of attribute annotated_image_path.



6
7
8
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 6

def annotated_image_path
  @annotated_image_path
end

#differenceObject (readonly)

Returns the value of attribute difference.



6
7
8
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 6

def difference
  @difference
end

#heatmap_diff_pathObject (readonly)

Returns the value of attribute heatmap_diff_path.



6
7
8
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 6

def heatmap_diff_path
  @heatmap_diff_path
end

Instance Method Details

#annotate_and_save_imagesObject



45
46
47
48
49
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 45

def annotate_and_save_images
  save_annotation_for(new_image, annotated_image_path)
  save_annotation_for(base_image, annotated_base_image_path)
  save_heatmap_diff if difference.meta[:diff_mask]
end

#annotate_difference(image, region) ⇒ Object



60
61
62
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 60

def annotate_difference(image, region)
  driver.draw_rectangles([image], region, DIFF_COLOR, offset: 1).first
end

#annotate_skip_areas(image, skip_areas) ⇒ Object



66
67
68
69
70
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 66

def annotate_skip_areas(image, skip_areas)
  skip_areas.reduce(image) do |memo, region|
    driver.draw_rectangles([memo], region, SKIP_COLOR).first
  end
end

#build_error_for_different_dimensionsObject



37
38
39
40
41
42
43
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 37

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

  "Screenshot dimension has been changed for #{image_path.to_path}: #{change_msg}"
end

#build_error_messageObject



78
79
80
81
82
83
84
85
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 78

def build_error_message
  [
    "(#{difference.inspect})",
    image_path.to_path,
    annotated_base_image_path.to_path,
    annotated_image_path.to_path
  ].join(NEW_LINE)
end

#clean_tmp_filesObject



32
33
34
35
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 32

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

#generateObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 17

def generate
  if difference.equal?
    # NOTE: Delete previous run runtime files
    clean_tmp_files
    return nil
  end

  if difference.failed? && difference.failed_by[:different_dimensions]
    return build_error_for_different_dimensions
  end

  annotate_and_save_images
  build_error_message
end

#save(image, image_path) ⇒ Object



72
73
74
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 72

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

#save_annotation_for(image, image_path) ⇒ Object



51
52
53
54
55
56
# File 'lib/capybara/screenshot/diff/reporters/default.rb', line 51

def save_annotation_for(image, image_path)
  image = annotate_difference(image, difference.region)
  image = annotate_skip_areas(image, difference.skip_area) if difference.skip_area

  save(image, image_path.to_path)
end