class Capybara::Screenshot::Diff::Reporters::Default

Constants

DIFF_COLOR
NEW_LINE
SKIP_COLOR

Attributes

annotated_base_image_path[R]
annotated_image_path[R]
difference[R]
heatmap_diff_path[R]

Public Class Methods

new(difference) click to toggle source
# 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

Public Instance Methods

annotate_and_save_images() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_dimensions() click to toggle source
# 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_message() click to toggle source
# 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_files() click to toggle source
# 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
generate() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

base_image() click to toggle source
# File lib/capybara/screenshot/diff/reporters/default.rb, line 96
def base_image
  difference.comparison.base_image
end
comparison() click to toggle source
# File lib/capybara/screenshot/diff/reporters/default.rb, line 112
def comparison
  @_comparison ||= difference.comparison
end
driver() click to toggle source
# File lib/capybara/screenshot/diff/reporters/default.rb, line 108
def driver
  @_driver ||= comparison.driver
end
image_path() click to toggle source
# File lib/capybara/screenshot/diff/reporters/default.rb, line 104
def image_path
  comparison.new_image_path
end
new_image() click to toggle source
# File lib/capybara/screenshot/diff/reporters/default.rb, line 100
def new_image
  difference.comparison.new_image
end
save_heatmap_diff() click to toggle source
# File lib/capybara/screenshot/diff/reporters/default.rb, line 89
def save_heatmap_diff
  merged_image = driver.merge(new_image, base_image)
  highlighted_mask = driver.highlight_mask(difference.meta[:diff_mask], merged_image, color: DIFF_COLOR)

  save(highlighted_mask, heatmap_diff_path.to_path)
end