capybara-screenshot-diff 1.10.3.1 → 1.13.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +77 -0
- data/CODE_OF_CONDUCT.md +129 -0
- data/Rakefile +29 -1
- data/capybara-screenshot-diff.gemspec +4 -3
- data/docs/RELEASE_PREP.md +42 -0
- data/docs/UPGRADING.md +390 -0
- data/docs/architecture.md +270 -0
- data/docs/ci-integration.md +238 -0
- data/docs/configuration.md +395 -0
- data/docs/docker-testing.md +24 -0
- data/docs/drivers.md +102 -0
- data/docs/framework-setup.md +87 -0
- data/docs/images/snap_diff_annotated.png +0 -0
- data/docs/images/snap_diff_web_ui.png +0 -0
- data/docs/migration-guide.md +286 -0
- data/docs/organization.md +204 -0
- data/docs/reporters.md +46 -0
- data/docs/thread_safety.md +97 -0
- data/gems.rb +2 -1
- data/lib/capybara/screenshot/diff/area_calculator.rb +1 -1
- data/lib/capybara/screenshot/diff/browser_helpers.rb +14 -1
- data/lib/capybara/screenshot/diff/comparison.rb +3 -0
- data/lib/capybara/screenshot/diff/difference.rb +40 -3
- data/lib/capybara/screenshot/diff/difference_finder.rb +97 -0
- data/lib/capybara/screenshot/diff/drivers/base_driver.rb +4 -0
- data/lib/capybara/screenshot/diff/drivers/chunky_png_driver.rb +22 -24
- data/lib/capybara/screenshot/diff/drivers/vips_driver.rb +40 -27
- data/lib/capybara/screenshot/diff/image_compare.rb +112 -123
- data/lib/capybara/screenshot/diff/image_preprocessor.rb +72 -0
- data/lib/capybara/screenshot/diff/reporters/default.rb +10 -11
- data/lib/capybara/screenshot/diff/screenshot_matcher.rb +77 -36
- data/lib/capybara/screenshot/diff/screenshoter.rb +9 -8
- data/lib/capybara/screenshot/diff/stable_screenshoter.rb +7 -9
- data/lib/capybara/screenshot/diff/vcs.rb +19 -52
- data/lib/capybara/screenshot/diff/version.rb +1 -1
- data/lib/capybara_screenshot_diff/backtrace_filter.rb +20 -0
- data/lib/capybara_screenshot_diff/cucumber.rb +11 -0
- data/lib/capybara_screenshot_diff/dsl.rb +123 -7
- data/lib/capybara_screenshot_diff/error_with_filtered_backtrace.rb +15 -0
- data/lib/capybara_screenshot_diff/minitest.rb +10 -5
- data/lib/capybara_screenshot_diff/reporters/html.rb +137 -0
- data/lib/capybara_screenshot_diff/reporters/templates/report.html.erb +463 -0
- data/lib/capybara_screenshot_diff/rspec.rb +20 -4
- data/lib/capybara_screenshot_diff/screenshot_assertion.rb +73 -22
- data/lib/capybara_screenshot_diff/screenshot_namer.rb +81 -0
- data/lib/capybara_screenshot_diff/snap.rb +14 -3
- data/lib/capybara_screenshot_diff/snap_manager.rb +10 -2
- data/lib/capybara_screenshot_diff/static.rb +11 -0
- data/lib/capybara_screenshot_diff.rb +31 -5
- metadata +51 -8
- data/lib/capybara/screenshot/diff/test_methods.rb +0 -157
data/docs/UPGRADING.md
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
# Upgrading to v1.12.0
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Version 1.12.0 is a **minor release** with new features, performance improvements, and default behavior changes. This guide will help you upgrade smoothly.
|
|
6
|
+
|
|
7
|
+
**Estimated upgrade time:** 5-15 minutes depending on your setup
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Quick Upgrade Path (Most Users)
|
|
12
|
+
|
|
13
|
+
For **most users**, upgrading is as simple as:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
# In your Gemfile
|
|
17
|
+
gem 'capybara-screenshot-diff', '~> 1.12.0'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
bundle update capybara-screenshot-diff
|
|
22
|
+
bundle exec rake test # Verify tests still pass
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**That's it!** The zero-config setup still works out of the box. Your existing screenshot comparisons will continue to work with v1.12.0.
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Breaking Changes & Migration Steps
|
|
30
|
+
|
|
31
|
+
### 1. Default Behavior Changes (Most Important)
|
|
32
|
+
|
|
33
|
+
Three settings now have different defaults. This is the most likely source of unexpected test failures.
|
|
34
|
+
|
|
35
|
+
#### `blur_active_element` — Now defaults to `true`
|
|
36
|
+
|
|
37
|
+
**Before (v1.11.x):** Cursor blinking could delay screenshots
|
|
38
|
+
**After (v1.12.0):** Cursor is automatically hidden
|
|
39
|
+
|
|
40
|
+
**Action required:** Only if you want the old behavior
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
# To restore v1.x behavior:
|
|
44
|
+
Capybara::Screenshot.blur_active_element = false
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### `hide_caret` — Now defaults to `true`
|
|
48
|
+
|
|
49
|
+
**Before (v1.11.x):** Input caret visible in screenshots
|
|
50
|
+
**After (v1.12.0):** Caret is transparent for stable screenshots
|
|
51
|
+
|
|
52
|
+
**Action required:** Only if you want the old behavior
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
# To restore v1.x behavior:
|
|
56
|
+
Capybara::Screenshot.hide_caret = false
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### `fail_if_new` — Now defaults to `true` in CI
|
|
60
|
+
|
|
61
|
+
**Before (v1.11.x):** New screenshots allowed in CI
|
|
62
|
+
**After (v1.12.0):** New screenshots fail tests in CI (when `ENV['CI']` is set)
|
|
63
|
+
|
|
64
|
+
**Action required:** Only if you want to allow new screenshots in CI
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
# To allow new screenshots in CI:
|
|
68
|
+
Capybara::Screenshot::Diff.fail_if_new = false
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Why this changed:** This prevents accidental baseline additions in CI pipelines. Most teams want this behavior.
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
### 2. SVN Support Removed
|
|
76
|
+
|
|
77
|
+
**Before (v1.11.x):** Could use SVN for version control
|
|
78
|
+
**After (v1.12.0):** Git only
|
|
79
|
+
|
|
80
|
+
**Action required:** If using SVN, migrate to Git
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# Check if you're using SVN for screenshots
|
|
84
|
+
git grep svn test/ # Look for svn commands in your tests
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
If you find SVN usage:
|
|
88
|
+
1. Export your SVN repository to Git
|
|
89
|
+
2. Update your CI/CD to use Git
|
|
90
|
+
3. Re-commit all screenshot baselines with Git
|
|
91
|
+
|
|
92
|
+
**Why this changed:** SVN support was rarely used and added maintenance burden.
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
### 3. ActiveSupport No Longer Required
|
|
97
|
+
|
|
98
|
+
**Before (v1.11.x):** ActiveSupport was a runtime dependency
|
|
99
|
+
**After (v1.12.0):** Pure Ruby, no ActiveSupport required
|
|
100
|
+
|
|
101
|
+
**Action required:** None (this is a positive change!)
|
|
102
|
+
|
|
103
|
+
If your project only had ActiveSupport because of this gem, you can now remove it:
|
|
104
|
+
|
|
105
|
+
```ruby
|
|
106
|
+
# In your Gemfile — can likely be removed if only used for this gem
|
|
107
|
+
# gem 'activesupport' # ← Remove if not used elsewhere
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**Why this changed:** Lighter installations, faster boot times.
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
### 4. Internal API Changes
|
|
115
|
+
|
|
116
|
+
**Before (v1.11.x):** Could use internal classes like `CaptureStrategy`, `ComparisonLoader`
|
|
117
|
+
**After (v1.12.0):** These have been inlined/refactored
|
|
118
|
+
|
|
119
|
+
**Action required:** Only if using internal APIs
|
|
120
|
+
|
|
121
|
+
Check your codebase:
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
# Search for internal API usage
|
|
125
|
+
grep -r "CaptureStrategy" test/ lib/
|
|
126
|
+
grep -r "ComparisonLoader" test/ lib/
|
|
127
|
+
grep -r "ScreenshotCoordinator" test/ lib/
|
|
128
|
+
grep -r "ImagePreprocessor" test/ lib/
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
If you find usage, these were never part of the public API and should be replaced with the documented public API.
|
|
132
|
+
|
|
133
|
+
**Why this changed:** Simplified architecture, better performance, easier maintenance.
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## New Features to Try
|
|
138
|
+
|
|
139
|
+
### HTML Reporter (Recommended)
|
|
140
|
+
|
|
141
|
+
Get an interactive dashboard showing all screenshot differences:
|
|
142
|
+
|
|
143
|
+
```ruby
|
|
144
|
+
# Add to test_helper.rb or spec_helper.rb
|
|
145
|
+
require 'capybara_screenshot_diff/reporters/html'
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
After running tests:
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
open doc/screenshots/snap_diff_report.html
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
**Features:**
|
|
155
|
+
- Side-by-side comparison with diff toggle
|
|
156
|
+
- Thumbnail sidebar for navigation
|
|
157
|
+
- Search functionality
|
|
158
|
+
- Summary statistics
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
### Standalone Image Comparison
|
|
163
|
+
|
|
164
|
+
Compare any two images without Capybara or a browser:
|
|
165
|
+
|
|
166
|
+
```ruby
|
|
167
|
+
result = Capybara::Screenshot::Diff.compare("baseline.png", "current.png")
|
|
168
|
+
result.quick_equal? # => true if byte-identical
|
|
169
|
+
result.different? # => true if visually different
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
**Use cases:**
|
|
173
|
+
- PDF regression testing
|
|
174
|
+
- Generated image validation
|
|
175
|
+
- CI artifact verification
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
### Perceptual Color Distance (Anti-aliasing Fix)
|
|
180
|
+
|
|
181
|
+
Eliminate false positives from font rendering differences:
|
|
182
|
+
|
|
183
|
+
```ruby
|
|
184
|
+
# Global configuration
|
|
185
|
+
Capybara::Screenshot::Diff.perceptual_threshold = 2.0
|
|
186
|
+
|
|
187
|
+
# Or per-screenshot
|
|
188
|
+
screenshot 'dashboard', perceptual_threshold: 2.0
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**dE00 Scale Reference:**
|
|
192
|
+
- `< 1.0` — Not perceptible by human eyes
|
|
193
|
+
- `1-2` — Perceptible through close observation (anti-aliasing, font hinting)
|
|
194
|
+
- `2-10` — Perceptible at a glance (color shifts, layout changes)
|
|
195
|
+
- `> 10` — Clearly different colors
|
|
196
|
+
|
|
197
|
+
**Why use this:** If you see false positives from font rendering differences across CI environments.
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
### `assert_no_screenshot_changes`
|
|
202
|
+
|
|
203
|
+
Assert that an action produces no visual change:
|
|
204
|
+
|
|
205
|
+
```ruby
|
|
206
|
+
test "clicking cancel doesn't change page" do
|
|
207
|
+
visit '/edit'
|
|
208
|
+
screenshot 'before_cancel'
|
|
209
|
+
|
|
210
|
+
click_button 'Cancel'
|
|
211
|
+
|
|
212
|
+
assert_no_screenshot_changes 'after_cancel'
|
|
213
|
+
end
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
### Simplified Configuration
|
|
219
|
+
|
|
220
|
+
Use the new `Diff.configure` block:
|
|
221
|
+
|
|
222
|
+
```ruby
|
|
223
|
+
# In test_helper.rb — one line, that's it
|
|
224
|
+
Capybara::Screenshot::Diff.configure do |screenshot, diff|
|
|
225
|
+
screenshot.window_size = [1280, 1024]
|
|
226
|
+
screenshot.stability_time_limit = 1
|
|
227
|
+
diff.driver = :vips
|
|
228
|
+
diff.tolerance = 0.0005
|
|
229
|
+
end
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## Performance Improvements
|
|
235
|
+
|
|
236
|
+
Enjoy faster screenshot comparisons:
|
|
237
|
+
|
|
238
|
+
- **ChunkyPNG:** Eliminated array allocations in shift-detection (~30% faster for large images)
|
|
239
|
+
- **VIPS:** Cached computations at construction (~15% faster)
|
|
240
|
+
- **General:** Memoized region area size, replaced closures with blocks
|
|
241
|
+
|
|
242
|
+
**No action required** — these are automatic improvements.
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
## Ruby & Rails Compatibility
|
|
247
|
+
|
|
248
|
+
### Supported Versions
|
|
249
|
+
|
|
250
|
+
- **Ruby:** 3.2, 3.3, 3.4, 3.5 (new!), 4.0 (new!)
|
|
251
|
+
- **Rails:** 7.1, 7.2, 8.0
|
|
252
|
+
|
|
253
|
+
### Upgrade Notes
|
|
254
|
+
|
|
255
|
+
**Ruby 4.0:** Fully compatible! If you see DSLStub ordering issues, they're fixed in v1.12.0.
|
|
256
|
+
|
|
257
|
+
**Rails 8.0:** Works out of the box with updated dependencies.
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
## Testing Your Upgrade
|
|
262
|
+
|
|
263
|
+
### Step 1: Update Gemfile
|
|
264
|
+
|
|
265
|
+
```ruby
|
|
266
|
+
gem 'capybara-screenshot-diff', '~> 1.12.0'
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Step 2: Bundle Update
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
bundle update capybara-screenshot-diff
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### Step 3: Run Tests
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
bundle exec rake test
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Step 4: Check for New Screenshot Failures
|
|
282
|
+
|
|
283
|
+
If tests fail with new screenshot errors in CI:
|
|
284
|
+
|
|
285
|
+
1. **Option A:** Commit the new baselines (recommended if changes are intentional)
|
|
286
|
+
2. **Option B:** Set `fail_if_new = false` temporarily (not recommended long-term)
|
|
287
|
+
|
|
288
|
+
### Step 5: Enable HTML Reporter (Optional)
|
|
289
|
+
|
|
290
|
+
```ruby
|
|
291
|
+
require 'capybara_screenshot_diff/reporters/html'
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
Run tests and open `doc/screenshots/snap_diff_report.html` to review differences.
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
## Troubleshooting
|
|
299
|
+
|
|
300
|
+
### "Tests fail with new screenshots in CI"
|
|
301
|
+
|
|
302
|
+
**Cause:** `fail_if_new` now defaults to `true` in CI
|
|
303
|
+
|
|
304
|
+
**Solution:**
|
|
305
|
+
|
|
306
|
+
```bash
|
|
307
|
+
# Commit the new baselines
|
|
308
|
+
git add doc/screenshots/
|
|
309
|
+
git commit -m "Add screenshot baselines for v1.12.0 upgrade"
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
Or temporarily allow them:
|
|
313
|
+
|
|
314
|
+
```ruby
|
|
315
|
+
Capybara::Screenshot::Diff.fail_if_new = false
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### "Screenshots look different after upgrade"
|
|
319
|
+
|
|
320
|
+
**Cause:** `blur_active_element` and `hide_caret` now default to `true`
|
|
321
|
+
|
|
322
|
+
**Solution:** Restore v1.x behavior temporarily:
|
|
323
|
+
|
|
324
|
+
```ruby
|
|
325
|
+
Capybara::Screenshot.blur_active_element = false
|
|
326
|
+
Capybara::Screenshot.hide_caret = false
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
Then re-record baselines with the new defaults (recommended):
|
|
330
|
+
|
|
331
|
+
```bash
|
|
332
|
+
# Delete old baselines
|
|
333
|
+
rm doc/screenshots/*.png
|
|
334
|
+
|
|
335
|
+
# Run tests to generate new baselines
|
|
336
|
+
bundle exec rake test
|
|
337
|
+
|
|
338
|
+
# Commit new baselines
|
|
339
|
+
git add doc/screenshots/
|
|
340
|
+
git commit -m "Re-record baselines with v1.12.0 defaults"
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### "NoMethodError on internal class"
|
|
344
|
+
|
|
345
|
+
**Cause:** Using internal APIs that were refactored
|
|
346
|
+
|
|
347
|
+
**Solution:** Use the public API instead. Check the documentation for the correct interface.
|
|
348
|
+
|
|
349
|
+
---
|
|
350
|
+
|
|
351
|
+
## Rollback Plan
|
|
352
|
+
|
|
353
|
+
If you need to rollback:
|
|
354
|
+
|
|
355
|
+
```ruby
|
|
356
|
+
# Pin to previous version
|
|
357
|
+
gem 'capybara-screenshot-diff', '~> 1.12.0'
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
```bash
|
|
361
|
+
bundle update capybara-screenshot-diff
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
All screenshot baselines are compatible — no data loss.
|
|
365
|
+
|
|
366
|
+
---
|
|
367
|
+
|
|
368
|
+
## Need Help?
|
|
369
|
+
|
|
370
|
+
- **Documentation:** [README.md](README.md)
|
|
371
|
+
- **Changelog:** [CHANGELOG.md](CHANGELOG.md)
|
|
372
|
+
- **Issues:** [GitHub Issues](https://github.com/snap-diff/snap_diff-capybara/issues)
|
|
373
|
+
- **DeepWiki:** [Code Documentation](https://deepwiki.com/snap-diff/snap_diff-capybara)
|
|
374
|
+
|
|
375
|
+
---
|
|
376
|
+
|
|
377
|
+
## Summary Checklist
|
|
378
|
+
|
|
379
|
+
- [ ] Update gem version to `~> 1.12.0`
|
|
380
|
+
- [ ] Run `bundle update capybara-screenshot-diff`
|
|
381
|
+
- [ ] Run test suite
|
|
382
|
+
- [ ] Check for new screenshot failures in CI
|
|
383
|
+
- [ ] Decide on `fail_if_new` behavior
|
|
384
|
+
- [ ] Decide on `blur_active_element` and `hide_caret` defaults
|
|
385
|
+
- [ ] Enable HTML reporter (optional)
|
|
386
|
+
- [ ] Re-record baselines if needed
|
|
387
|
+
- [ ] Commit changes
|
|
388
|
+
- [ ] Review upgrade issues in [GitHub Issues](https://github.com/snap-diff/snap_diff-capybara/issues)
|
|
389
|
+
|
|
390
|
+
**Congratulations!** You're now running v1.12.0 🎉
|
|
@@ -0,0 +1,270 @@
|
|
|
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
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
┌─────────────────────────────────────────────────────────┐
|
|
9
|
+
│ Test Framework │
|
|
10
|
+
│ (Minitest / RSpec / Cucumber / Custom) │
|
|
11
|
+
└──────────────┬──────────────────────────────┬────────────┘
|
|
12
|
+
│ │
|
|
13
|
+
▼ ▼
|
|
14
|
+
┌──────────────────────────┐ ┌──────────────────────────┐
|
|
15
|
+
│ CapybaraScreenshotDiff │ │ CapybaraScreenshotDiff │
|
|
16
|
+
│ ::DSL (screenshot, etc) │ │ ::AssertionRegistry │
|
|
17
|
+
└──────────────┬───────────┘ └──────────────┬───────────┘
|
|
18
|
+
│ │
|
|
19
|
+
▼ ▼
|
|
20
|
+
┌──────────────────────────┐ ┌──────────────────────────┐
|
|
21
|
+
│ ScreenshotMatcher │ │ ScreenshotAssertion │
|
|
22
|
+
│ (capture + compare) │ │ (validate results) │
|
|
23
|
+
└──────┬───────────┬────────┘ └──────────────┬───────────┘
|
|
24
|
+
│ │ │
|
|
25
|
+
▼ ▼ ▼
|
|
26
|
+
┌──────────┐ ┌──────────┐ ┌──────────────────────┐
|
|
27
|
+
│Screenshot│ │Stable │ │ Reporters │
|
|
28
|
+
│er │ │Screenshot│ │ (Default / HTML / │
|
|
29
|
+
│ │ │er │ │ Custom) │
|
|
30
|
+
└─────┬────┘ └──────────┘ └──────────────────────┘
|
|
31
|
+
│
|
|
32
|
+
▼
|
|
33
|
+
┌──────────────────────────────────────────┐
|
|
34
|
+
│ ImageCompare (layered comparison) │
|
|
35
|
+
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
|
|
36
|
+
│ │1. Byte │ │2. Pixel │ │3. Region │ │
|
|
37
|
+
│ │ compare │ │ compare │ │ analyze │ │
|
|
38
|
+
│ └──────────┘ └──────────┘ └──────────┘ │
|
|
39
|
+
└─────────────────────┬────────────────────┘
|
|
40
|
+
│
|
|
41
|
+
▼
|
|
42
|
+
┌──────────────────────────────────────────┐
|
|
43
|
+
│ Drivers (image processing backends) │
|
|
44
|
+
│ ┌──────────┐ ┌──────────────────────┐ │
|
|
45
|
+
│ │ Vips │ │ ChunkyPNG │ │
|
|
46
|
+
│ │ (fast) │ │ (no native deps) │ │
|
|
47
|
+
│ └──────────┘ └──────────────────────┘ │
|
|
48
|
+
└──────────────────────────────────────────┘
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Component Breakdown
|
|
52
|
+
|
|
53
|
+
### 1. DSL Layer (`lib/capybara_screenshot_diff/dsl.rb`)
|
|
54
|
+
|
|
55
|
+
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.
|
|
56
|
+
|
|
57
|
+
**Flow:**
|
|
58
|
+
1. Checks if screenshots are `active?` (returns `false` if disabled)
|
|
59
|
+
2. Builds a full screenshot name via `ScreenshotNamer` (handles sections, groups, counters)
|
|
60
|
+
3. Delegates to `ScreenshotMatcher` to capture and prepare comparison
|
|
61
|
+
4. Creates a `ScreenshotAssertion` — either adds it to the thread-local registry (delayed validation) or validates immediately
|
|
62
|
+
|
|
63
|
+
### 2. ScreenshotMatcher (`lib/capybara/screenshot/diff/screenshot_matcher.rb`)
|
|
64
|
+
|
|
65
|
+
The orchestrator that coordinates capture and comparison:
|
|
66
|
+
|
|
67
|
+
1. **Window size check** — verifies browser window is the expected size
|
|
68
|
+
2. **Area calculation** — resolves crop regions and skip areas (supports CSS selectors and coordinates)
|
|
69
|
+
3. **Base screenshot checkout** — retrieves the committed baseline from git via `Vcs.checkout_vcs`
|
|
70
|
+
4. **Capture** — delegates to `Screenshoter` or `StableScreenshoter` depending on `stability_time_limit`
|
|
71
|
+
5. **Comparison** — creates an `ImageCompare` object (lazy — actual comparison happens on first access)
|
|
72
|
+
6. **Assertion** — returns a `ScreenshotAssertion` with the comparison attached
|
|
73
|
+
|
|
74
|
+
**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.
|
|
75
|
+
|
|
76
|
+
### 3. Screenshoter & StableScreenshoter (`lib/capybara/screenshot/diff/screenshoter.rb`, `lib/capybara/screenshot/diff/stable_screenshoter.rb`)
|
|
77
|
+
|
|
78
|
+
**Screenshoter:** The basic capture flow:
|
|
79
|
+
1. Prepares the page (blur active element, hide caret, disable animations, wait for images)
|
|
80
|
+
2. Takes a browser screenshot via `Capybara.current_session.save_screenshot`
|
|
81
|
+
3. Processes the screenshot (resize for retina, apply crop)
|
|
82
|
+
|
|
83
|
+
**StableScreenshoter:** Wraps `Screenshoter` with stability detection:
|
|
84
|
+
1. Takes sequential screenshots at `stability_time_limit` intervals
|
|
85
|
+
2. Compares consecutive attempts for byte-level equality
|
|
86
|
+
3. Returns once two consecutive screenshots are identical
|
|
87
|
+
4. Fails with `UnstableImage` if timeout (`wait`) is reached, generating annotated attempt images for debugging
|
|
88
|
+
|
|
89
|
+
### 4. ImageCompare (`lib/capybara/screenshot/diff/image_compare.rb`)
|
|
90
|
+
|
|
91
|
+
The comparison engine uses a **layered optimization strategy** to balance speed and accuracy:
|
|
92
|
+
|
|
93
|
+
| Layer | Method | What it checks | Speed | When it returns |
|
|
94
|
+
|-------|--------|----------------|-------|-----------------|
|
|
95
|
+
| 1 | `quick_equal?` | File size + byte-level comparison | Fastest | Equal → `true`, Different → Layer 2 |
|
|
96
|
+
| 2 | `quick_equal?` continues | Dimension check + pixel comparison | Fast | Different dimensions → `false`, Same pixels → `true` |
|
|
97
|
+
| 3 | `processed` / `different?` | Full region analysis with tolerance | Slower | Detailed diff with region, heatmap, annotations |
|
|
98
|
+
|
|
99
|
+
**Key details:**
|
|
100
|
+
- `quick_equal?` is designed for fast rejection — it early-returns as soon as a difference is found
|
|
101
|
+
- `different?` triggers the full comparison if not already processed
|
|
102
|
+
- `processed` guarantees the comparison is complete and returns the result with all metadata
|
|
103
|
+
- The `DifferenceFinder` handles the actual pixel analysis, delegating to the driver
|
|
104
|
+
|
|
105
|
+
### 5. Drivers (`lib/capybara/screenshot/diff/drivers/`)
|
|
106
|
+
|
|
107
|
+
Drivers abstract image processing operations. Each driver implements:
|
|
108
|
+
|
|
109
|
+
| Operation | VipsDriver | ChunkyPNGDriver |
|
|
110
|
+
|-----------|-----------|-----------------|
|
|
111
|
+
| `load_images` | Vips::Image from file | ChunkyPNG::Image from blob |
|
|
112
|
+
| `same_dimension?` | Compare width × height | Same |
|
|
113
|
+
| `same_pixels?` | Pixel-level equality | Same |
|
|
114
|
+
| `find_difference_region` | Difference mask → Region | Row-by-row scan → Region |
|
|
115
|
+
| `crop` | Vips image crop | ChunkyPNG crop |
|
|
116
|
+
| `save_image_to` | Vips write_to_file | PNG save |
|
|
117
|
+
| `filter_image_with_median` | Vips median filter | Not supported |
|
|
118
|
+
| `add_black_box` | Draw filled rect | No-op (handled differently) |
|
|
119
|
+
| `merge` | Composite images | Not applicable |
|
|
120
|
+
| `highlight_mask` | Conditional color overlay | Not applicable |
|
|
121
|
+
|
|
122
|
+
**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.
|
|
123
|
+
|
|
124
|
+
### 6. Difference Region Detection
|
|
125
|
+
|
|
126
|
+
**VipsDriver** uses a **difference mask** approach:
|
|
127
|
+
1. Compute absolute difference between images: `(new - base).abs`
|
|
128
|
+
2. Optional: apply perceptual color distance (CIE dE00) instead of raw RGB
|
|
129
|
+
3. Project the mask to find the bounding region of non-zero pixels
|
|
130
|
+
4. Return the tight bounding box of all differences
|
|
131
|
+
|
|
132
|
+
**ChunkyPNGDriver** uses **row-by-row scanning**:
|
|
133
|
+
1. Scan top-to-bottom, left-to-right for first differing pixel
|
|
134
|
+
2. Expand left/right boundaries within each differing row
|
|
135
|
+
3. Extend bottom boundary to cover all differing rows
|
|
136
|
+
4. Supports shift detection (expensive neighbor pixel search)
|
|
137
|
+
|
|
138
|
+
### 7. SnapManager & Snap (`lib/capybara_screenshot_diff/snap_manager.rb`, `lib/capybara_screenshot_diff/snap.rb`)
|
|
139
|
+
|
|
140
|
+
**Snap** represents a single screenshot file with path management:
|
|
141
|
+
- `path` — the actual screenshot file
|
|
142
|
+
- `base_path` — the `.base.ext` VCS checkout
|
|
143
|
+
- `attempt_path` — stability attempt files (`.attempt_00.png`, etc.)
|
|
144
|
+
- Provides cleanup of diff artifacts (`.diff.png`, `.heatmap.diff.png`)
|
|
145
|
+
|
|
146
|
+
**SnapManager** is the factory and path manager:
|
|
147
|
+
- Creates `Snap` instances
|
|
148
|
+
- Handles VCS checkout of baselines
|
|
149
|
+
- Manages file operations (copy, move, cleanup)
|
|
150
|
+
|
|
151
|
+
### 8. VCS (`lib/capybara/screenshot/diff/vcs.rb`)
|
|
152
|
+
|
|
153
|
+
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).
|
|
154
|
+
|
|
155
|
+
### 9. Reporters (`lib/capybara/screenshot/diff/reporters/`)
|
|
156
|
+
|
|
157
|
+
**Default reporter:** Generates annotated diff images:
|
|
158
|
+
- `image.diff.png` — new screenshot with diff region outlined in red
|
|
159
|
+
- `image.base.diff.png` — baseline with diff region outlined in red
|
|
160
|
+
- `image.heatmap.diff.png` — heatmap overlay of pixel differences
|
|
161
|
+
|
|
162
|
+
**HTML reporter:** Generates an interactive dashboard (`snap_diff_report.html`) with:
|
|
163
|
+
- Sidebar with thumbnails and search
|
|
164
|
+
- 4 view modes (both/base/new/heatmap)
|
|
165
|
+
- Annotated toggle for showing diff outlines
|
|
166
|
+
- Per-image zoom with synchronized panning across side-by-side views
|
|
167
|
+
- Keyboard navigation and shortcuts
|
|
168
|
+
- Responsive layout for mobile
|
|
169
|
+
|
|
170
|
+
**Custom reporters:** Implement `record(assertions)` and `finalize` methods, then add to `CapybaraScreenshotDiff.reporters`.
|
|
171
|
+
|
|
172
|
+
### 10. Assertion Lifecycle
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
Test begins
|
|
176
|
+
│
|
|
177
|
+
├─ setup: resize_window_if_needed
|
|
178
|
+
│
|
|
179
|
+
├─ test body:
|
|
180
|
+
│ └─ screenshot("name")
|
|
181
|
+
│ ├─ ScreenshotMatcher builds assertion
|
|
182
|
+
│ ├─ delayed=true → add to Thread-local registry
|
|
183
|
+
│ └─ delayed=false → validate immediately
|
|
184
|
+
│
|
|
185
|
+
├─ teardown:
|
|
186
|
+
│ └─ CapybaraScreenshotDiff.verify
|
|
187
|
+
│ ├─ iterates thread-local assertions
|
|
188
|
+
│ ├─ calls validate on each
|
|
189
|
+
│ ├─ raises ExpectationNotMet on first failure
|
|
190
|
+
│ └─ notifies reporters via mutex-protected snapshot
|
|
191
|
+
│
|
|
192
|
+
└─ at_exit:
|
|
193
|
+
└─ CapybaraScreenshotDiff.finalize_reporters!
|
|
194
|
+
├─ Generates HTML report (if reporter registered)
|
|
195
|
+
└─ Prints summary
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 11. Thread Safety
|
|
199
|
+
|
|
200
|
+
| Concern | Mechanism |
|
|
201
|
+
|---------|-----------|
|
|
202
|
+
| Assertion registry | Thread-local storage (`Thread.current[:capybara_screenshot_diff_registry]`) |
|
|
203
|
+
| Reporter notification | Mutex-protected snapshot of reporter list before iteration |
|
|
204
|
+
| HTML reporter internals | Mutex protecting `@failures`, `@total`, `@finalized` |
|
|
205
|
+
| Screenshot naming | Per-thread `ScreenshotNamer` instance |
|
|
206
|
+
| Global configuration | `mattr_accessor` — must be set before tests run, not mutated during parallel execution |
|
|
207
|
+
| File system | Atomic `FileUtils.mv`, unique paths per screenshot name + counter, thread-safe `mkpath` |
|
|
208
|
+
|
|
209
|
+
### 12. Configuration System
|
|
210
|
+
|
|
211
|
+
Configuration uses Ruby's `mattr_accessor` (from ActiveSupport, or pure Ruby fallback) and is organized into two namespaces:
|
|
212
|
+
|
|
213
|
+
**`Capybara::Screenshot`** — capture settings:
|
|
214
|
+
- `window_size`, `stability_time_limit`, `blur_active_element`, `hide_caret`, `disable_animations`
|
|
215
|
+
- `save_path`, `root`, `screenshot_format`, `add_driver_path`, `add_os_path`
|
|
216
|
+
- `enabled`, `capybara_screenshot_options`
|
|
217
|
+
|
|
218
|
+
**`Capybara::Screenshot::Diff`** — comparison settings:
|
|
219
|
+
- `driver`, `tolerance`, `color_distance_limit`, `perceptual_threshold`, `shift_distance_limit`
|
|
220
|
+
- `area_size_limit`, `skip_area`, `fail_if_new`, `fail_on_difference`, `delayed`
|
|
221
|
+
|
|
222
|
+
The `Diff.configure` block helper provides a convenient way to set both namespaces at once.
|
|
223
|
+
|
|
224
|
+
## File Layout
|
|
225
|
+
|
|
226
|
+
```
|
|
227
|
+
lib/
|
|
228
|
+
capybara_screenshot_diff.rb # Core module, mattr_accessor definitions
|
|
229
|
+
capybara/screenshot/diff.rb # Convenience require (loads minitest)
|
|
230
|
+
capybara_screenshot_diff/
|
|
231
|
+
dsl.rb # screenshot(), screenshot_group(), etc.
|
|
232
|
+
minitest.rb # Minitest assertions integration
|
|
233
|
+
rspec.rb # RSpec matcher integration
|
|
234
|
+
cucumber.rb # Cucumber World integration
|
|
235
|
+
static.rb # Non-Rails static site serving
|
|
236
|
+
snap_manager.rb # Screenshot file management
|
|
237
|
+
snap.rb # Single screenshot file abstraction
|
|
238
|
+
screenshot_namer.rb # Name/path generation with sections/groups
|
|
239
|
+
screenshot_assertion.rb # Assertion + registry objects
|
|
240
|
+
attempts_reporter.rb # Debug reporting for unstable captures
|
|
241
|
+
error_with_filtered_backtrace.rb # Error with filtered stack
|
|
242
|
+
backtrace_filter.rb # Library/gem frame filtering
|
|
243
|
+
reporters/
|
|
244
|
+
html.rb # Interactive HTML report reporter
|
|
245
|
+
templates/report.html.erb # HTML report template
|
|
246
|
+
capybara/screenshot/diff/
|
|
247
|
+
version.rb # VERSION constant
|
|
248
|
+
utils.rb # Driver detection
|
|
249
|
+
drivers.rb # Driver factory
|
|
250
|
+
drivers/
|
|
251
|
+
base_driver.rb # Abstract base driver
|
|
252
|
+
vips_driver.rb # VIPS image processing
|
|
253
|
+
chunky_png_driver.rb # ChunkyPNG image processing
|
|
254
|
+
screenshoter.rb # Basic browser screenshot capture
|
|
255
|
+
stable_screenshoter.rb # Stability detection wrapper
|
|
256
|
+
screenshot_matcher.rb # Orchestrator for capture + compare
|
|
257
|
+
image_compare.rb # Layered comparison engine
|
|
258
|
+
comparison.rb # Comparison value object
|
|
259
|
+
difference.rb # Difference result value object
|
|
260
|
+
difference_finder.rb # Core diff analysis logic
|
|
261
|
+
image_preprocessor.rb # Pre-processing (skip areas, median filter)
|
|
262
|
+
area_calculator.rb # Crop/skip area coordinate resolution
|
|
263
|
+
region.rb # Bounding box region value object
|
|
264
|
+
browser_helpers.rb # DOM manipulation helpers
|
|
265
|
+
vcs.rb # Git baseline checkout
|
|
266
|
+
os.rb # OS detection
|
|
267
|
+
cucumber.rb # Deprecated Cucumber entry point
|
|
268
|
+
reporters/
|
|
269
|
+
default.rb # Default annotated-image reporter
|
|
270
|
+
```
|