Module: SnapDiff::DSL
Overview
DSL for taking screenshots and making assertions in Capybara tests. This module provides methods for taking screenshots, comparing them against baselines, and managing the comparison process with various configuration options.
The DSL is designed to be included in your test context (e.g., RSpec, Minitest) to provide screenshot comparison capabilities.
Instance Method Summary collapse
-
#assert_matches_screenshot(name, skip_stack_frames: 0, **options) { ... } ⇒ Boolean
Takes a screenshot and compares it against a baseline image.
-
#assert_no_screenshot_changes(name, skip_stack_frames: 0, **opts, &readiness) ⇒ Object
Asserts the current page has no visual changes from the baseline.
-
#capture_screenshot(name, **options) { ... } ⇒ Boolean
Captures a screenshot without comparing it to a baseline.
-
#screenshot(name, skip_stack_frames: 0, compare: true, **options) { ... } ⇒ Object
Convenience wrapper around #assert_matches_screenshot and #capture_screenshot.
- #screenshot_group(name) ⇒ Object
- #screenshot_section(name) ⇒ Object
Instance Method Details
#assert_matches_screenshot(name, skip_stack_frames: 0, **options) { ... } ⇒ Boolean
Takes a screenshot and compares it against a baseline image.
The method follows a layered optimization strategy for comparison:
- First checks if screenshot functionality is active
- Builds a full screenshot name using the current context
- Creates a screenshot assertion object
- Either validates immediately or defers validation based on options
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/snap_diff/dsl.rb', line 83 def assert_matches_screenshot(name, skip_stack_frames: 0, **) return false unless SnapDiff.config.active? yield if block_given? # Get the full name with section and group information full_name = SnapDiff.session.screenshot_namer.full_name(name) # Build the screenshot assertion; the actual comparison is deferred # until ScreenshotAssertion#validate! runs. assertion = SnapDiff::ScreenshotMatcher .new(full_name, ) .build_screenshot_assertion(skip_stack_frames: skip_stack_frames + 1) return false unless assertion # Determine if validation should be delayed or immediate delayed = .fetch(:delayed, SnapDiff.config.delayed) if delayed SnapDiff.session.add_assertion(assertion) else assertion.validate! end true end |
#assert_no_screenshot_changes(name, skip_stack_frames: 0, **opts, &readiness) ⇒ Object
Asserts the current page has no visual changes from the baseline. Override in your base test class to add project-specific behavior (e.g., waiting for Turbo, default skip areas).
145 146 147 |
# File 'lib/snap_diff/dsl.rb', line 145 def assert_no_screenshot_changes(name, skip_stack_frames: 0, **opts, &readiness) assert_matches_screenshot(name, skip_stack_frames: skip_stack_frames + 1, **opts, &readiness) end |
#capture_screenshot(name, **options) { ... } ⇒ Boolean
Captures a screenshot without comparing it to a baseline.
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/snap_diff/dsl.rb', line 131 def capture_screenshot(name, **) return false unless SnapDiff.config.active? yield if block_given? full_name = SnapDiff.session.screenshot_namer.full_name(name) SnapDiff::ScreenshotMatcher.new(full_name, ).capture true end |
#screenshot(name, skip_stack_frames: 0, compare: true, **options) { ... } ⇒ Object
Convenience wrapper around #assert_matches_screenshot and #capture_screenshot.
118 119 120 121 122 123 124 |
# File 'lib/snap_diff/dsl.rb', line 118 def screenshot(name, skip_stack_frames: 0, compare: true, **, &readiness) if compare assert_matches_screenshot(name, skip_stack_frames: skip_stack_frames + 1, **, &readiness) else capture_screenshot(name, **, &readiness) end end |