Sha256: cefe20fcc8f9a2ff11e172897d0024d817741f4b2a8be343ad9e4c09a79e338d

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require "capybara/screenshot/diff/image_compare"

module CapybaraScreenshotDiff
  class AttemptsReporter
    def initialize(snapshot, comparison_options, stability_options = {})
      @snapshot = snapshot
      @comparison_options = comparison_options
      @wait = stability_options[:wait]
    end

    def generate
      attempts_screenshot_paths = @snapshot.find_attempts_paths

      annotate_attempts(attempts_screenshot_paths)

      "Could not get stable screenshot within #{@wait}s:\n#{attempts_screenshot_paths.join("\n")}"
    end

    def build_comparison_for(attempt_path, previous_attempt_path)
      Capybara::Screenshot::Diff::ImageCompare.new(attempt_path, previous_attempt_path, @comparison_options)
    end

    private

    def annotate_attempts(attempts_screenshot_paths)
      previous_file = nil
      attempts_screenshot_paths.reverse_each do |file_name|
        if previous_file && File.exist?(previous_file)
          attempts_comparison = build_comparison_for(file_name, previous_file)

          if attempts_comparison.different?
            FileUtils.mv(attempts_comparison.reporter.annotated_base_image_path, previous_file, force: true)
          else
            warn "[capybara-screenshot-diff] Some attempts was stable, but mistakenly marked as not: " \
                   "#{previous_file} and #{file_name} are equal"
          end

          FileUtils.rm(attempts_comparison.reporter.annotated_image_path, force: true)
        end

        previous_file = file_name
      end

      previous_file
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capybara-screenshot-diff-1.9.2 lib/capybara_screenshot_diff/attempts_reporter.rb