Class: SnapDiff::ScreenshotAssertion

Inherits:
Object
  • Object
show all
Defined in:
lib/snap_diff/screenshot_assertion.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, **args) ⇒ ScreenshotAssertion

Returns a new instance of ScreenshotAssertion.



45
46
47
48
# File 'lib/snap_diff/screenshot_assertion.rb', line 45

def initialize(name, **args)
  @name = name
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



42
43
44
# File 'lib/snap_diff/screenshot_assertion.rb', line 42

def args
  @args
end

#callerObject

Returns the value of attribute caller.



43
44
45
# File 'lib/snap_diff/screenshot_assertion.rb', line 43

def caller
  @caller
end

#compareObject

Returns the value of attribute compare.



43
44
45
# File 'lib/snap_diff/screenshot_assertion.rb', line 43

def compare
  @compare
end

#nameObject (readonly)

Returns the value of attribute name.



42
43
44
# File 'lib/snap_diff/screenshot_assertion.rb', line 42

def name
  @name
end

Class Method Details

.verify_screenshots!(screenshots) ⇒ Array?

Note:

This method is typically called at the end of a test to assert all screenshots are as expected.

Verifies that all scheduled screenshots do not show any unintended differences.

Parameters:

  • screenshots (Array(Array(Array(String), String, Comparison)))

    The list of match screenshots jobs. Defaults to all screenshots taken during the test.

Returns:

  • (Array, nil)

    Returns an array of error messages if there are screenshot differences, otherwise nil.



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/snap_diff/screenshot_assertion.rb', line 98

def self.verify_screenshots!(screenshots)
  return unless SnapDiff.config.active? && SnapDiff.config.fail_on_difference

  test_screenshot_errors = screenshots.map do |assertion|
    assertion.validate
  end

  test_screenshot_errors.compact!

  test_screenshot_errors.empty? ? nil : test_screenshot_errors
end

Instance Method Details

#archive_baseline!Object

Commits the baseline after a passing comparison: the base image is moved over the actual image, so the captured screenshot becomes the recorded baseline again. This is the file-mutating half of the verify flow, kept explicit and separate from the pure "are they different?" question. Idempotent: a second call is a no-op.



79
80
81
82
83
# File 'lib/snap_diff/screenshot_assertion.rb', line 79

def archive_baseline!
  return unless compare && !compare.different? && compare.base_image_path.exist?

  FileUtils.mv(compare.base_image_path, compare.image_path, force: true)
end

#inspectObject

One-line debugging summary. Never triggers the (expensive, file-touching) comparison itself: an unprocessed comparison shows as "pending".



52
53
54
55
56
57
58
59
60
61
# File 'lib/snap_diff/screenshot_assertion.rb', line 52

def inspect
  return "#<#{self.class.name} #{name.inspect} (no comparison)>" unless compare

  state = if compare.processed?
    compare.difference.different? ? "different" : "matches"
  else
    "pending"
  end
  "#<#{self.class.name} #{name.inspect} #{state} new=#{compare.image_path} base=#{compare.base_image_path}>"
end

#validateObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/snap_diff/screenshot_assertion.rb', line 63

def validate
  return unless compare

  if compare.different?
    "Screenshot does not match for '#{name}': #{compare.error_message}\n#{caller.join("\n")}"
  else
    archive_baseline!
    nil
  end
end

#validate!Object



85
86
87
88
89
90
91
# File 'lib/snap_diff/screenshot_assertion.rb', line 85

def validate!
  error_msg = validate

  if error_msg
    raise SnapDiff::ExpectationNotMet.new(error_msg, caller)
  end
end