Class: SnapDiff::ScreenshotAssertion
- Inherits:
-
Object
- Object
- SnapDiff::ScreenshotAssertion
- Defined in:
- lib/snap_diff/screenshot_assertion.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#caller ⇒ Object
Returns the value of attribute caller.
-
#compare ⇒ Object
Returns the value of attribute compare.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.verify_screenshots!(screenshots) ⇒ Array?
Verifies that all scheduled screenshots do not show any unintended differences.
Instance Method Summary collapse
-
#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.
-
#initialize(name, **args) ⇒ ScreenshotAssertion
constructor
A new instance of ScreenshotAssertion.
-
#inspect ⇒ Object
One-line debugging summary.
- #validate ⇒ Object
- #validate! ⇒ Object
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
#args ⇒ Object (readonly)
Returns the value of attribute args.
42 43 44 |
# File 'lib/snap_diff/screenshot_assertion.rb', line 42 def args @args end |
#caller ⇒ Object
Returns the value of attribute caller.
43 44 45 |
# File 'lib/snap_diff/screenshot_assertion.rb', line 43 def caller @caller end |
#compare ⇒ Object
Returns the value of attribute compare.
43 44 45 |
# File 'lib/snap_diff/screenshot_assertion.rb', line 43 def compare @compare end |
#name ⇒ Object (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?
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.
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 |
#inspect ⇒ Object
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 |
#validate ⇒ Object
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.}\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 |