snap_diff-capybara 0.0.1 → 2.0.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +190 -0
  3. data/CODE_OF_CONDUCT.md +129 -0
  4. data/LICENSE.txt +21 -0
  5. data/Rakefile +65 -0
  6. data/capybara-screenshot-diff.gemspec +29 -0
  7. data/docs/RELEASE_PREP.md +44 -0
  8. data/docs/UPGRADING.md +702 -0
  9. data/docs/architecture.md +289 -0
  10. data/docs/ci-integration.md +238 -0
  11. data/docs/configuration.md +395 -0
  12. data/docs/docker-testing.md +24 -0
  13. data/docs/drivers.md +102 -0
  14. data/docs/framework-setup.md +87 -0
  15. data/docs/images/snap_diff_annotated.png +0 -0
  16. data/docs/images/snap_diff_web_ui.png +0 -0
  17. data/docs/migration-guide.md +286 -0
  18. data/docs/organization.md +204 -0
  19. data/docs/reporters.md +46 -0
  20. data/docs/thread_safety.md +112 -0
  21. data/gems.rb +39 -0
  22. data/lib/capybara/screenshot/diff/annotation_service.rb +7 -0
  23. data/lib/capybara/screenshot/diff/area_calculator.rb +7 -0
  24. data/lib/capybara/screenshot/diff/browser_helpers.rb +7 -0
  25. data/lib/capybara/screenshot/diff/config_legacy.rb +113 -0
  26. data/lib/capybara/screenshot/diff/cucumber.rb +3 -0
  27. data/lib/capybara/screenshot/diff/difference.rb +8 -0
  28. data/lib/capybara/screenshot/diff/drivers/base_driver.rb +9 -0
  29. data/lib/capybara/screenshot/diff/drivers/chunky_png_driver.rb +7 -0
  30. data/lib/capybara/screenshot/diff/drivers/vips_driver.rb +7 -0
  31. data/lib/capybara/screenshot/diff/drivers.rb +9 -0
  32. data/lib/capybara/screenshot/diff/image_compare.rb +12 -0
  33. data/lib/capybara/screenshot/diff/image_preprocessor.rb +7 -0
  34. data/lib/capybara/screenshot/diff/os.rb +14 -0
  35. data/lib/capybara/screenshot/diff/region.rb +107 -0
  36. data/lib/capybara/screenshot/diff/reporters/default.rb +109 -0
  37. data/lib/capybara/screenshot/diff/screenshot_matcher.rb +7 -0
  38. data/lib/capybara/screenshot/diff/screenshoter.rb +7 -0
  39. data/lib/capybara/screenshot/diff/stable_screenshoter.rb +7 -0
  40. data/lib/capybara/screenshot/diff/utils.rb +7 -0
  41. data/lib/capybara/screenshot/diff/vcs.rb +7 -0
  42. data/lib/capybara/screenshot/diff/version.rb +15 -0
  43. data/lib/capybara/screenshot/diff.rb +3 -0
  44. data/lib/capybara-screenshot-diff.rb +3 -0
  45. data/lib/capybara_screenshot_diff/attempts_reporter.rb +7 -0
  46. data/lib/capybara_screenshot_diff/cucumber.rb +8 -0
  47. data/lib/capybara_screenshot_diff/dsl.rb +12 -0
  48. data/lib/capybara_screenshot_diff/error_with_filtered_backtrace.rb +8 -0
  49. data/lib/capybara_screenshot_diff/minitest.rb +14 -0
  50. data/lib/capybara_screenshot_diff/reporters/html.rb +7 -0
  51. data/lib/capybara_screenshot_diff/rspec.rb +8 -0
  52. data/lib/capybara_screenshot_diff/screenshot_assertion.rb +71 -0
  53. data/lib/capybara_screenshot_diff/screenshot_namer.rb +7 -0
  54. data/lib/capybara_screenshot_diff/snap.rb +7 -0
  55. data/lib/capybara_screenshot_diff/snap_manager.rb +7 -0
  56. data/lib/capybara_screenshot_diff/static.rb +9 -0
  57. data/lib/capybara_screenshot_diff.rb +50 -0
  58. data/lib/snap_diff/annotation_service.rb +84 -0
  59. data/lib/snap_diff/area_calculator.rb +56 -0
  60. data/lib/snap_diff/attempts_reporter.rb +51 -0
  61. data/lib/snap_diff/browser_helpers.rb +121 -0
  62. data/lib/snap_diff/capture/viewport.rb +40 -0
  63. data/lib/snap_diff/comparison.rb +238 -0
  64. data/lib/snap_diff/comparison_result.rb +104 -0
  65. data/lib/snap_diff/config.rb +78 -0
  66. data/lib/snap_diff/deprecation.rb +84 -0
  67. data/lib/snap_diff/driver.rb +37 -0
  68. data/lib/snap_diff/drivers/chunky_png_driver.rb +298 -0
  69. data/lib/snap_diff/drivers/vips_driver.rb +171 -0
  70. data/lib/snap_diff/drivers.rb +14 -0
  71. data/lib/snap_diff/dsl.rb +143 -0
  72. data/lib/snap_diff/error_with_filtered_backtrace.rb +32 -0
  73. data/lib/snap_diff/image_preprocessor.rb +68 -0
  74. data/lib/snap_diff/integrations/cucumber.rb +22 -0
  75. data/lib/snap_diff/integrations/minitest.rb +70 -0
  76. data/lib/snap_diff/integrations/rspec.rb +68 -0
  77. data/lib/snap_diff/legacy_shims.rb +99 -0
  78. data/lib/snap_diff/os.rb +17 -0
  79. data/lib/snap_diff/reporters/html.rb +143 -0
  80. data/lib/snap_diff/reporters/templates/report.html.erb +463 -0
  81. data/lib/snap_diff/reporting.rb +53 -0
  82. data/lib/snap_diff/screenshot_assertion.rb +143 -0
  83. data/lib/snap_diff/screenshot_matcher.rb +113 -0
  84. data/lib/snap_diff/screenshot_namer.rb +81 -0
  85. data/lib/snap_diff/screenshoter.rb +129 -0
  86. data/lib/snap_diff/snap.rb +66 -0
  87. data/lib/snap_diff/snap_manager.rb +125 -0
  88. data/lib/snap_diff/stable_screenshoter.rb +103 -0
  89. data/lib/snap_diff/static.rb +11 -0
  90. data/lib/snap_diff/utils.rb +38 -0
  91. data/lib/snap_diff/vcs.rb +35 -0
  92. data/lib/snap_diff/version.rb +5 -0
  93. data/lib/snap_diff.rb +65 -0
  94. metadata +160 -10
  95. data/README.md +0 -3
@@ -0,0 +1,289 @@
1
+ # Architecture
2
+
3
+ This document describes the internal architecture of `capybara-screenshot-diff` — how screenshots are captured, compared, and reported, and how the components fit together.
4
+
5
+ Since the v2 namespace move (ADR-004), the implementation lives in `lib/snap_diff/` under the `SnapDiff` namespace. The old file paths (`lib/capybara/screenshot/diff/`, `lib/capybara_screenshot_diff/`) remain as thin forwarders, and the old constants resolve to the same objects via `lib/snap_diff/legacy_shims.rb` with a one-time deprecation warning. Class names below use the canonical `SnapDiff::` names, with legacy names noted where they differ.
6
+
7
+ ## Overview
8
+
9
+ ```
10
+ ┌─────────────────────────────────────────────────────────┐
11
+ │ Test Framework │
12
+ │ (Minitest / RSpec / Cucumber / Custom) │
13
+ └──────────────┬──────────────────────────────┬────────────┘
14
+ │ │
15
+ ▼ ▼
16
+ ┌──────────────────────────┐ ┌──────────────────────────┐
17
+ │ CapybaraScreenshotDiff │ │ CapybaraScreenshotDiff │
18
+ │ ::DSL (screenshot, etc) │ │ ::AssertionRegistry │
19
+ └──────────────┬───────────┘ └──────────────┬───────────┘
20
+ │ │
21
+ ▼ ▼
22
+ ┌──────────────────────────┐ ┌──────────────────────────┐
23
+ │ ScreenshotMatcher │ │ ScreenshotAssertion │
24
+ │ (capture + compare) │ │ (validate results) │
25
+ └──────┬───────────┬────────┘ └──────────────┬───────────┘
26
+ │ │ │
27
+ ▼ ▼ ▼
28
+ ┌──────────┐ ┌──────────┐ ┌──────────────────────┐
29
+ │Screenshot│ │Stable │ │ Reporters │
30
+ │er │ │Screenshot│ │ (Default / HTML / │
31
+ │ │ │er │ │ Custom) │
32
+ └─────┬────┘ └──────────┘ └──────────────────────┘
33
+
34
+
35
+ ┌──────────────────────────────────────────┐
36
+ │ SnapDiff::Comparison (layered compare) │
37
+ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
38
+ │ │1. Byte │ │2. Pixel │ │3. Region │ │
39
+ │ │ compare │ │ compare │ │ analyze │ │
40
+ │ └──────────┘ └──────────┘ └──────────┘ │
41
+ └─────────────────────┬────────────────────┘
42
+
43
+
44
+ ┌──────────────────────────────────────────┐
45
+ │ Drivers (image processing backends) │
46
+ │ ┌──────────┐ ┌──────────────────────┐ │
47
+ │ │ Vips │ │ ChunkyPNG │ │
48
+ │ │ (fast) │ │ (no native deps) │ │
49
+ │ └──────────┘ └──────────────────────┘ │
50
+ └──────────────────────────────────────────┘
51
+ ```
52
+
53
+ ## Component Breakdown
54
+
55
+ ### 1. DSL Layer (`lib/snap_diff/dsl.rb`)
56
+
57
+ `SnapDiff::DSL` — `CapybaraScreenshotDiff::DSL` remains an eager same-object alias.
58
+
59
+ The entry point for test code. `assert_matches_screenshot` is the primary assertion method (captures and compares). `screenshot` is a convenience wrapper with a `compare:` option — `compare: true` (default) delegates to `assert_matches_screenshot`, while `compare: false` delegates to the new `capture_screenshot` method. `capture_screenshot` takes screenshots without assertions. `assert_no_screenshot_changes` keeps its behavior but now delegates to `assert_matches_screenshot` (that redirect is part of the #191 fix). Users can safely override `screenshot` in their test classes without affecting internal gem flow.
60
+
61
+ **Flow:**
62
+ 1. Checks if screenshots are `active?` (returns `false` if disabled)
63
+ 2. Builds a full screenshot name via `ScreenshotNamer` (handles sections, groups, counters)
64
+ 3. Delegates to `ScreenshotMatcher` to capture and prepare comparison
65
+ 4. Creates a `ScreenshotAssertion` — either adds it to the thread-local registry (delayed validation) or validates immediately
66
+
67
+ ### 2. ScreenshotMatcher (`lib/snap_diff/screenshot_matcher.rb`)
68
+
69
+ The orchestrator that coordinates capture and comparison:
70
+
71
+ 1. **Viewport preparation** — `SnapDiff::Capture::Viewport.prepare!` (`lib/snap_diff/capture/viewport.rb`) verifies the browser window is the expected size (raise-only, never resizes); runs once per capture, outside any stability retry loop
72
+ 2. **Area calculation** — resolves crop regions and skip areas (supports CSS selectors and coordinates)
73
+ 3. **Base screenshot checkout** — retrieves the committed baseline from git via `Vcs.checkout_vcs`
74
+ 4. **Capture** — delegates to `Screenshoter` or `StableScreenshoter` depending on `stability_time_limit`
75
+ 5. **Comparison** — creates a `SnapDiff::Comparison` object (lazy — actual comparison happens on first access)
76
+ 6. **Assertion** — returns a `ScreenshotAssertion` with the comparison attached
77
+
78
+ **Key design decision:** The "new" screenshot is taken *first*, then compared against the baseline. This means if no baseline exists (first run), we skip comparison entirely and the test passes.
79
+
80
+ ### 3. Screenshoter & StableScreenshoter (`lib/snap_diff/screenshoter.rb`, `lib/snap_diff/stable_screenshoter.rb`)
81
+
82
+ **Screenshoter:** The basic capture flow:
83
+ 1. Prepares the page (blur active element, hide caret, disable animations, wait for images)
84
+ 2. Takes a browser screenshot via `Capybara.current_session.save_screenshot`
85
+ 3. Processes the screenshot (resize for retina, apply crop)
86
+
87
+ **StableScreenshoter:** Wraps `Screenshoter` with stability detection:
88
+ 1. Takes sequential screenshots at `stability_time_limit` intervals
89
+ 2. Compares consecutive attempts for byte-level equality
90
+ 3. Returns once two consecutive screenshots are identical
91
+ 4. Fails with `UnstableImage` if timeout (`wait`) is reached, generating annotated attempt images for debugging
92
+
93
+ ### 4. Comparison (`lib/snap_diff/comparison.rb`)
94
+
95
+ `SnapDiff::Comparison` (legacy name: `ImageCompare`); its result value object is `SnapDiff::ComparisonResult` (`lib/snap_diff/comparison_result.rb`, legacy name: `Difference`).
96
+
97
+ The comparison engine uses a **layered optimization strategy** to balance speed and accuracy:
98
+
99
+ | Layer | Method | What it checks | Speed | When it returns |
100
+ |-------|--------|----------------|-------|-----------------|
101
+ | 1 | `quick_equal?` | File size + byte-level comparison | Fastest | Equal → `true`, Different → Layer 2 |
102
+ | 2 | `quick_equal?` continues | Dimension check + pixel comparison | Fast | Different dimensions → `false`, Same pixels → `true` |
103
+ | 3 | `processed` / `different?` | Full region analysis with tolerance | Slower | Detailed diff with region, heatmap, annotations |
104
+
105
+ **Key details:**
106
+ - `quick_equal?` is designed for fast rejection — it early-returns as soon as a difference is found
107
+ - `different?` triggers the full comparison if not already processed
108
+ - `processed` guarantees the comparison is complete and returns the result with all metadata
109
+ - `Comparison#analyze_difference` handles the actual pixel analysis, delegating to the driver
110
+
111
+ ### 5. Drivers (`lib/snap_diff/drivers/`)
112
+
113
+ Drivers abstract image processing operations. Shared default behavior lives in the `SnapDiff::Driver` mixin (`lib/snap_diff/driver.rb`) — it replaced the old `Drivers::BaseDriver` superclass, so concrete drivers `include SnapDiff::Driver` instead of inheriting. Each driver implements:
114
+
115
+ | Operation | VipsDriver | ChunkyPNGDriver |
116
+ |-----------|-----------|-----------------|
117
+ | `load_images` | Vips::Image from file | ChunkyPNG::Image from blob |
118
+ | `same_dimension?` | Compare width × height | Same |
119
+ | `same_pixels?` | Pixel-level equality | Same |
120
+ | `find_difference_region` | Difference mask → Region | Row-by-row scan → Region |
121
+ | `crop` | Vips image crop | ChunkyPNG crop |
122
+ | `save_image_to` | Vips write_to_file | PNG save |
123
+ | `filter_image_with_median` | Vips median filter | Not supported |
124
+ | `add_black_box` | Draw filled rect | No-op (handled differently) |
125
+ | `merge` | Composite images | Not applicable |
126
+ | `highlight_mask` | Conditional color overlay | Not applicable |
127
+
128
+ **Auto-detection:** `Utils.detect_available_drivers` tries to load `:vips` first (via `ruby-vips` gem), then `:chunky_png`. The `:auto` driver mode picks the first available.
129
+
130
+ ### 6. Difference Region Detection
131
+
132
+ **VipsDriver** uses a **difference mask** approach:
133
+ 1. Compute absolute difference between images: `(new - base).abs`
134
+ 2. Optional: apply perceptual color distance (CIE dE00) instead of raw RGB
135
+ 3. Project the mask to find the bounding region of non-zero pixels
136
+ 4. Return the tight bounding box of all differences
137
+
138
+ **ChunkyPNGDriver** uses **row-by-row scanning**:
139
+ 1. Scan top-to-bottom, left-to-right for first differing pixel
140
+ 2. Expand left/right boundaries within each differing row
141
+ 3. Extend bottom boundary to cover all differing rows
142
+ 4. Supports shift detection (expensive neighbor pixel search)
143
+
144
+ ### 7. SnapManager & Snap (`lib/snap_diff/snap_manager.rb`, `lib/snap_diff/snap.rb`)
145
+
146
+ **Snap** represents a single screenshot file with path management:
147
+ - `path` — the actual screenshot file
148
+ - `base_path` — the `.base.ext` VCS checkout
149
+ - `attempt_path` — stability attempt files (`.attempt_00.png`, etc.)
150
+ - Provides cleanup of diff artifacts (`.diff.png`, `.heatmap.diff.png`)
151
+
152
+ **SnapManager** is the factory and path manager:
153
+ - Creates `Snap` instances
154
+ - Handles VCS checkout of baselines
155
+ - Manages file operations (copy, move, cleanup)
156
+
157
+ ### 8. VCS (`lib/snap_diff/vcs.rb`)
158
+
159
+ Handles baseline retrieval from git. Uses `git show HEAD:<path>` to extract the committed version. Supports Git LFS via `git lfs smudge`. Returns `false` if the file doesn't exist in VCS (first-run scenario).
160
+
161
+ ### 9. Reporters (`lib/capybara/screenshot/diff/reporters/default.rb`, `lib/snap_diff/reporters/html.rb`)
162
+
163
+ **Default reporter:** Generates annotated diff images:
164
+ - `image.diff.png` — new screenshot with diff region outlined in red
165
+ - `image.base.diff.png` — baseline with diff region outlined in red
166
+ - `image.heatmap.diff.png` — heatmap overlay of pixel differences
167
+
168
+ **HTML reporter:** Generates an interactive dashboard (`snap_diff_report.html`) with:
169
+ - Sidebar with thumbnails and search
170
+ - 4 view modes (both/base/new/heatmap)
171
+ - Annotated toggle for showing diff outlines
172
+ - Per-image zoom with synchronized panning across side-by-side views
173
+ - Keyboard navigation and shortcuts
174
+ - Responsive layout for mobile
175
+
176
+ **Custom reporters:** Implement `record(assertions)` and `finalize` methods, then add to `CapybaraScreenshotDiff.reporters`. The process-global reporter lifecycle (registration, notification, finalization) is owned by `SnapDiff::Reporting` (`lib/snap_diff/reporting.rb`); `CapybaraScreenshotDiff.reporters` / `.finalize_reporters!` are thin public shims over it.
177
+
178
+ ### 10. Assertion Lifecycle
179
+
180
+ ```
181
+ Test begins
182
+
183
+ ├─ setup: resize_window_if_needed
184
+
185
+ ├─ test body:
186
+ │ └─ screenshot("name")
187
+ │ ├─ ScreenshotMatcher builds assertion
188
+ │ ├─ delayed=true → add to Thread-local registry
189
+ │ └─ delayed=false → validate immediately
190
+
191
+ ├─ teardown:
192
+ │ └─ CapybaraScreenshotDiff.verify
193
+ │ ├─ iterates thread-local assertions
194
+ │ ├─ calls validate on each
195
+ │ ├─ raises ExpectationNotMet on first failure
196
+ │ └─ notifies reporters via mutex-protected snapshot
197
+
198
+ └─ at_exit:
199
+ └─ CapybaraScreenshotDiff.finalize_reporters!
200
+ ├─ Generates HTML report (if reporter registered)
201
+ └─ Prints summary
202
+ ```
203
+
204
+ ### 11. Thread Safety
205
+
206
+ | Concern | Mechanism |
207
+ |---------|-----------|
208
+ | Assertion registry | Thread-local storage (`Thread.current[:capybara_screenshot_diff_registry]`) |
209
+ | Reporter notification | Mutex-protected snapshot of reporter list before iteration |
210
+ | HTML reporter internals | Mutex protecting `@failures`, `@total`, `@finalized` |
211
+ | Screenshot naming | Per-thread `ScreenshotNamer` instance |
212
+ | Global configuration | `mattr_accessor` — must be set before tests run, not mutated during parallel execution |
213
+ | File system | Atomic `FileUtils.mv`, unique paths per screenshot name + counter, thread-safe `mkpath` |
214
+
215
+ ### 12. Configuration System
216
+
217
+ Configuration uses Ruby's `mattr_accessor` (pure Ruby implementation in `lib/capybara/screenshot/diff/config_legacy.rb`, deliberately kept at the old path as the single source of truth for settings storage) and is organized into two namespaces:
218
+
219
+ **`Capybara::Screenshot`** — capture settings:
220
+ - `window_size`, `stability_time_limit`, `blur_active_element`, `hide_caret`, `disable_animations`
221
+ - `save_path`, `root`, `screenshot_format`, `add_driver_path`, `add_os_path`
222
+ - `enabled`, `capybara_screenshot_options`
223
+
224
+ **`Capybara::Screenshot::Diff`** — comparison settings:
225
+ - `driver`, `tolerance`, `color_distance_limit`, `perceptual_threshold`, `shift_distance_limit`
226
+ - `area_size_limit`, `skip_area`, `fail_if_new`, `fail_on_difference`, `delayed`
227
+
228
+ The `Diff.configure` block helper provides a convenient way to set both namespaces at once. Since v2, `SnapDiff::Config` (`lib/snap_diff/config.rb`) additionally exposes all 27 settings as one flat object via `SnapDiff.config` / `SnapDiff.configure { |config| ... }` — it holds no state of its own, every accessor forwards to the legacy `mattr_accessor` storage, so both views stay consistent.
229
+
230
+ ## File Layout
231
+
232
+ ```
233
+ lib/
234
+ snap_diff.rb # SnapDiff module: compare/start/configure/config
235
+ snap_diff/ # Canonical implementation (v2)
236
+ dsl.rb # screenshot(), screenshot_group(), etc.
237
+ config.rb # SnapDiff::Config — flat view over all 27 settings
238
+ deprecation.rb # Warn-once-per-constant machinery
239
+ legacy_shims.rb # const_missing forwarders for the old namespaces
240
+ comparison.rb # Layered comparison engine (ex-ImageCompare)
241
+ comparison_result.rb # Comparison result value object (ex-Difference)
242
+ driver.rb # SnapDiff::Driver mixin (ex-BaseDriver superclass)
243
+ drivers.rb # Driver factory
244
+ drivers/
245
+ vips_driver.rb # VIPS image processing
246
+ chunky_png_driver.rb # ChunkyPNG image processing
247
+ capture/
248
+ viewport.rb # Per-capture viewport preparation seam
249
+ screenshoter.rb # Basic browser screenshot capture
250
+ stable_screenshoter.rb # Stability detection wrapper
251
+ screenshot_matcher.rb # Orchestrator for capture + compare
252
+ screenshot_assertion.rb # Assertion + registry objects
253
+ screenshot_namer.rb # Name/path generation with sections/groups
254
+ snap_manager.rb # Screenshot file management
255
+ snap.rb # Single screenshot file abstraction
256
+ reporting.rb # Process-global reporter lifecycle
257
+ reporters/
258
+ html.rb # Interactive HTML report reporter
259
+ templates/report.html.erb # HTML report template
260
+ annotation_service.rb # Diff-image annotation (RED_RGBA / ORANGE_RGBA)
261
+ image_preprocessor.rb # Pre-processing (skip areas, median filter)
262
+ area_calculator.rb # Crop/skip area coordinate resolution
263
+ browser_helpers.rb # DOM manipulation helpers
264
+ attempts_reporter.rb # Debug reporting for unstable captures
265
+ error_with_filtered_backtrace.rb # Error with filtered stack
266
+ vcs.rb # Git baseline checkout
267
+ utils.rb # Driver detection
268
+ os.rb # OS detection
269
+ static.rb # Non-Rails static site serving
270
+ version.rb # Gem version
271
+ integrations/
272
+ minitest.rb # Minitest assertions integration
273
+ rspec.rb # RSpec matcher integration
274
+ cucumber.rb # Cucumber World integration
275
+ capybara_screenshot_diff.rb # Umbrella entry point + error classes
276
+ capybara_screenshot_diff/ # Legacy paths — mostly thin forwarders
277
+ minitest.rb / rspec.rb / cucumber.rb # Legacy entry points (load the full gem)
278
+ screenshot_assertion.rb # CapybaraScreenshotDiff session/reporter shims
279
+ ... # Everything else forwards to snap_diff/
280
+ capybara/screenshot/diff.rb # Convenience require (loads minitest)
281
+ capybara/screenshot/diff/
282
+ config_legacy.rb # mattr_accessor settings storage (source of truth)
283
+ region.rb # Bounding box region value object (top-level Region)
284
+ reporters/default.rb # Default annotated-image reporter
285
+ version.rb # Capybara::Screenshot::Diff::VERSION (gemspec reads it)
286
+ ... # Everything else forwards to snap_diff/
287
+ ```
288
+
289
+ The legacy `Capybara::Screenshot::Diff::*` and `CapybaraScreenshotDiff::*` constants resolve lazily via `snap_diff/legacy_shims.rb` (`const_missing`), pointing at the same objects with a one-time deprecation warning. See [UPGRADING.md](UPGRADING.md) for the migration guide.
@@ -0,0 +1,238 @@
1
+ # CI & Non-Rails Integration
2
+
3
+ ## Non-Rails Projects (Hugo, Jekyll, Static Sites)
4
+
5
+ ```ruby
6
+ # test/test_helper.rb
7
+ require 'capybara_screenshot_diff/static'
8
+
9
+ CapybaraScreenshotDiff.serve("_site") # or "public", "build", "dist"
10
+ ```
11
+
12
+ This sets up Capybara to serve static files and configures screenshot paths automatically.
13
+
14
+ ## .gitignore Setup
15
+
16
+ See the [Quick Start section](../README.md#quick-start-5-minutes) in the README for recommended `.gitignore` patterns.
17
+
18
+ Only commit the baseline screenshots (e.g., `homepage.png`). The `.base.png`, `.diff.png`, `.heatmap.diff.png`, and report files are regenerated on every test run.
19
+
20
+ ## GitHub Actions Integration
21
+
22
+ ### 1. Enable the HTML report
23
+
24
+ Add to your test helper:
25
+
26
+ ```ruby
27
+ require 'capybara_screenshot_diff/reporters/html'
28
+ ```
29
+
30
+ ### 2. Reusable composite action (recommended)
31
+
32
+ The simplest way — one step handles artifact upload, job summary, and PR comments:
33
+
34
+ ```yaml
35
+ # .github/workflows/test.yml
36
+ jobs:
37
+ test:
38
+ runs-on: ubuntu-latest
39
+ permissions:
40
+ contents: read
41
+ pull-requests: write # Required for PR comments
42
+
43
+ steps:
44
+ - uses: actions/checkout@v6
45
+
46
+ - uses: ruby/setup-ruby@v1
47
+ with:
48
+ bundler-cache: true
49
+
50
+ - name: Run tests
51
+ run: bundle exec rake test
52
+
53
+ - name: Upload screenshot reports
54
+ if: failure()
55
+ uses: snap-diff/snap_diff-capybara/.github/actions/upload-screenshots@master
56
+ with:
57
+ name: screenshots
58
+ pr-comment: 'true'
59
+ ```
60
+
61
+ That's it. On failure, this will:
62
+ - Upload diff images + HTML report as artifacts
63
+ - Post a PR comment with links to the inline report and full artifact download
64
+ - Add a job summary with report links (visible in the Actions UI)
65
+
66
+ #### Inputs
67
+
68
+ | Input | Default | Description |
69
+ |-------|---------|-------------|
70
+ | `name` | (required) | Artifact name prefix |
71
+ | `report-path` | `doc/screenshots` | Path to HTML report directory |
72
+ | `retention-days` | `2` | Days to retain artifacts |
73
+ | `pr-comment` | `false` | Post PR comment with report link (requires `pull-requests: write`) |
74
+
75
+ #### Outputs
76
+
77
+ | Output | Description |
78
+ |--------|-------------|
79
+ | `report-url` | Direct URL to the inline HTML report artifact |
80
+ | `report-full-url` | Direct URL to the full report artifact (with images) |
81
+
82
+ ### 3. Ruby + libvips setup action
83
+
84
+ For consistent CI environments (libvips, font antialiasing disabled), use the setup action:
85
+
86
+ ```yaml
87
+ - uses: snap-diff/snap_diff-capybara/.github/actions/setup-ruby-and-dependencies@master
88
+ with:
89
+ ruby-version: '4.0'
90
+ cache-apt-packages: true
91
+ ```
92
+
93
+ This installs Ruby, libvips (with apt caching), and disables font antialiasing for consistent rendering across CI runs.
94
+
95
+ #### Inputs
96
+
97
+ | Input | Default | Description |
98
+ |-------|---------|-------------|
99
+ | `ruby-version` | (required) | Ruby version to install |
100
+ | `cache-apt-packages` | `false` | Cache libvips apt packages for faster runs |
101
+ | `ruby-cache-version` | — | Bundler cache version key |
102
+
103
+ ### 4. Full example with both actions
104
+
105
+ ```yaml
106
+ jobs:
107
+ test:
108
+ runs-on: ubuntu-latest
109
+ timeout-minutes: 10
110
+ permissions:
111
+ contents: read
112
+ pull-requests: write
113
+
114
+ steps:
115
+ - uses: actions/checkout@v6
116
+
117
+ - uses: snap-diff/snap_diff-capybara/.github/actions/setup-ruby-and-dependencies@master
118
+ with:
119
+ ruby-version: '4.0'
120
+ cache-apt-packages: true
121
+
122
+ - run: bundle exec rake test
123
+
124
+ - uses: snap-diff/snap_diff-capybara/.github/actions/upload-screenshots@master
125
+ if: failure()
126
+ with:
127
+ name: screenshots
128
+ pr-comment: 'true'
129
+ ```
130
+
131
+ ### 5. Manual setup (without composite actions)
132
+
133
+ If you prefer full control, here's the expanded YAML:
134
+
135
+ <details>
136
+ <summary>Expand manual setup</summary>
137
+
138
+ ```yaml
139
+ jobs:
140
+ test:
141
+ runs-on: ubuntu-latest
142
+ steps:
143
+ - uses: actions/checkout@v6
144
+
145
+ - uses: ruby/setup-ruby@v1
146
+ with:
147
+ bundler-cache: true
148
+
149
+ - name: Install libvips
150
+ run: sudo apt-get install -y libvips-dev
151
+
152
+ - name: Run tests
153
+ run: bundle exec rake test
154
+
155
+ - name: Upload screenshot report
156
+ if: failure()
157
+ uses: actions/upload-artifact@v7
158
+ with:
159
+ name: screenshot-report
160
+ path: doc/screenshots/snap_diff_report.html
161
+ archive: false
162
+ retention-days: 2
163
+
164
+ - name: Upload full report with images
165
+ if: failure()
166
+ uses: actions/upload-artifact@v7
167
+ with:
168
+ name: screenshot-report-full
169
+ path: doc/screenshots/
170
+ retention-days: 2
171
+ ```
172
+
173
+ </details>
174
+
175
+ ## Update Baselines in CI
176
+
177
+ When intentional UI changes are made, baselines need to be re-recorded. You can do this locally:
178
+
179
+ ```bash
180
+ RECORD_SCREENSHOTS=1 bundle exec rake test
181
+ git add test/fixtures/screenshots/
182
+ git commit -m "chore: update screenshot baselines"
183
+ ```
184
+
185
+ Or add a workflow that maintainers can trigger manually:
186
+
187
+ <details>
188
+ <summary>Expand update-baselines workflow</summary>
189
+
190
+ ```yaml
191
+ # .github/workflows/update-baselines.yml
192
+ name: Update Screenshot Baselines
193
+
194
+ on:
195
+ workflow_dispatch:
196
+ inputs:
197
+ branch:
198
+ description: 'Branch to update baselines on'
199
+ required: true
200
+ default: 'main'
201
+
202
+ permissions:
203
+ contents: write
204
+
205
+ jobs:
206
+ update:
207
+ runs-on: ubuntu-latest
208
+ steps:
209
+ - uses: actions/checkout@v6
210
+ with:
211
+ ref: ${{ inputs.branch }}
212
+
213
+ - uses: snap-diff/snap_diff-capybara/.github/actions/setup-ruby-and-dependencies@master
214
+ with:
215
+ ruby-version: '4.0'
216
+ cache-apt-packages: true
217
+
218
+ - name: Record new baselines
219
+ run: RECORD_SCREENSHOTS=1 bundle exec rake test
220
+ continue-on-error: true
221
+
222
+ - name: Commit updated baselines
223
+ run: |
224
+ git config user.name "github-actions[bot]"
225
+ git config user.email "github-actions[bot]@users.noreply.github.com"
226
+ git add test/fixtures/ doc/screenshots/
227
+ git diff --staged --quiet || git commit -m "chore: update screenshot baselines"
228
+ git push
229
+ ```
230
+
231
+ </details>
232
+
233
+ **How it works:**
234
+ 1. Go to Actions → "Update Screenshot Baselines" → "Run workflow"
235
+ 2. Enter the branch name (e.g. your PR branch)
236
+ 3. The workflow records new baselines, commits, and pushes
237
+
238
+ [← Back to README](../README.md)