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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +77 -0
  3. data/CODE_OF_CONDUCT.md +129 -0
  4. data/Rakefile +29 -1
  5. data/capybara-screenshot-diff.gemspec +4 -3
  6. data/docs/RELEASE_PREP.md +42 -0
  7. data/docs/UPGRADING.md +390 -0
  8. data/docs/architecture.md +270 -0
  9. data/docs/ci-integration.md +238 -0
  10. data/docs/configuration.md +395 -0
  11. data/docs/docker-testing.md +24 -0
  12. data/docs/drivers.md +102 -0
  13. data/docs/framework-setup.md +87 -0
  14. data/docs/images/snap_diff_annotated.png +0 -0
  15. data/docs/images/snap_diff_web_ui.png +0 -0
  16. data/docs/migration-guide.md +286 -0
  17. data/docs/organization.md +204 -0
  18. data/docs/reporters.md +46 -0
  19. data/docs/thread_safety.md +97 -0
  20. data/gems.rb +2 -1
  21. data/lib/capybara/screenshot/diff/area_calculator.rb +1 -1
  22. data/lib/capybara/screenshot/diff/browser_helpers.rb +14 -1
  23. data/lib/capybara/screenshot/diff/comparison.rb +3 -0
  24. data/lib/capybara/screenshot/diff/difference.rb +40 -3
  25. data/lib/capybara/screenshot/diff/difference_finder.rb +97 -0
  26. data/lib/capybara/screenshot/diff/drivers/base_driver.rb +4 -0
  27. data/lib/capybara/screenshot/diff/drivers/chunky_png_driver.rb +22 -24
  28. data/lib/capybara/screenshot/diff/drivers/vips_driver.rb +40 -27
  29. data/lib/capybara/screenshot/diff/image_compare.rb +112 -123
  30. data/lib/capybara/screenshot/diff/image_preprocessor.rb +72 -0
  31. data/lib/capybara/screenshot/diff/reporters/default.rb +10 -11
  32. data/lib/capybara/screenshot/diff/screenshot_matcher.rb +77 -36
  33. data/lib/capybara/screenshot/diff/screenshoter.rb +9 -8
  34. data/lib/capybara/screenshot/diff/stable_screenshoter.rb +7 -9
  35. data/lib/capybara/screenshot/diff/vcs.rb +19 -52
  36. data/lib/capybara/screenshot/diff/version.rb +1 -1
  37. data/lib/capybara_screenshot_diff/backtrace_filter.rb +20 -0
  38. data/lib/capybara_screenshot_diff/cucumber.rb +11 -0
  39. data/lib/capybara_screenshot_diff/dsl.rb +123 -7
  40. data/lib/capybara_screenshot_diff/error_with_filtered_backtrace.rb +15 -0
  41. data/lib/capybara_screenshot_diff/minitest.rb +10 -5
  42. data/lib/capybara_screenshot_diff/reporters/html.rb +137 -0
  43. data/lib/capybara_screenshot_diff/reporters/templates/report.html.erb +463 -0
  44. data/lib/capybara_screenshot_diff/rspec.rb +20 -4
  45. data/lib/capybara_screenshot_diff/screenshot_assertion.rb +73 -22
  46. data/lib/capybara_screenshot_diff/screenshot_namer.rb +81 -0
  47. data/lib/capybara_screenshot_diff/snap.rb +14 -3
  48. data/lib/capybara_screenshot_diff/snap_manager.rb +10 -2
  49. data/lib/capybara_screenshot_diff/static.rb +11 -0
  50. data/lib/capybara_screenshot_diff.rb +31 -5
  51. metadata +51 -8
  52. data/lib/capybara/screenshot/diff/test_methods.rb +0 -157
data/docs/drivers.md ADDED
@@ -0,0 +1,102 @@
1
+ # Image Processing Drivers
2
+
3
+ ## Perceptual color comparison (VIPS only)
4
+
5
+ By default, color differences are measured using raw RGB channel distance. This can produce
6
+ false positives from anti-aliasing and sub-pixel font rendering — the same page rendered on
7
+ different OS versions or browsers will have slightly different pixel values at text edges.
8
+
9
+ The `perceptual_threshold` option uses the CIE dE00 formula instead, which measures color
10
+ difference the way human eyes perceive it. Anti-aliasing artifacts typically score below 2.0
11
+ on the dE00 scale and are automatically ignored.
12
+
13
+ ```ruby
14
+ # Per-screenshot: ignore anti-aliasing, catch real visual changes
15
+ screenshot 'dashboard', perceptual_threshold: 2.0
16
+
17
+ # Global: apply to all screenshots
18
+ Capybara::Screenshot::Diff.perceptual_threshold = 2.0
19
+
20
+ # dE00 scale reference:
21
+ # < 1.0 — not perceptible by human eyes
22
+ # 1-2 — perceptible through close observation (anti-aliasing, font hinting)
23
+ # 2-10 — perceptible at a glance (color shifts, layout changes)
24
+ # > 10 — clearly different colors
25
+ ```
26
+
27
+ Use `perceptual_threshold` when you see false positives from font rendering differences across
28
+ CI environments, or when `color_distance_limit` with raw RGB requires frequent tuning.
29
+
30
+ **⚠️ Important:** `perceptual_threshold` and `color_distance_limit` are **mutually exclusive**.
31
+ If you set both, `perceptual_threshold` takes priority and `color_distance_limit` is silently ignored.
32
+
33
+ These options use different scales and algorithms:
34
+ - `perceptual_threshold` → CIE dE00 perceptual distance (0-100+)
35
+ - `color_distance_limit` → Euclidean RGBA distance (0-510)
36
+
37
+ **Choose one based on your driver setup:**
38
+ - VIPS with `ruby-vips` gem → prefer `perceptual_threshold`
39
+ - ChunkyPNG (no native dependencies) → use `color_distance_limit`
40
+
41
+ ## Available Image Processing Drivers
42
+
43
+ There are several image processing supported by this gem.
44
+ There are several options to setup active driver: `:auto`, `:chunky_png` and `:vips`.
45
+
46
+ * `:auto` - will try to load `:vips` if there is gem `ruby-vips`, in other cases will load `:chunky_png`
47
+ * `:chunky_png` and `:vips` will load correspondent driver
48
+
49
+ ## Enable VIPS image processing
50
+
51
+ [Vips](https://www.rubydoc.info/gems/ruby-vips/Vips/Image) driver provides a faster comparison,
52
+ and could be enabled by adding `ruby-vips` to `Gemfile`.
53
+
54
+ If need to setup explicitly Vips driver, there are several ways to do this:
55
+
56
+ * Globally: `Capybara::Screenshot::Diff.driver = :vips`
57
+ * Per screenshot option: `screenshot 'index', driver: :vips`
58
+
59
+ With enabled VIPS there are new alternatives to process differences, which are easier to find and support.
60
+ For example, `shift_distance_limit` is a very heavy operation. Instead, use `median_filter_window_size`.
61
+
62
+ ## Tolerance level (vips only)
63
+
64
+ You can set a "tolerance" anywhere from 0% to 100%. This is the amount of change that's allowable.
65
+ If the screenshot has changed by more than that amount, it'll flag it as a failure.
66
+
67
+ This is alternative to "Allowed difference size", only the difference that area calculates including valid pixels.
68
+ But "tolerance" compares only different pixels.
69
+
70
+ You can use the `tolerance` option to the `screenshot` method to set level:
71
+
72
+ ```ruby
73
+ test 'unstable area' do
74
+ visit '/'
75
+ # tolerance: 0.01 allows 1% of pixels to differ (use for noisy pages)
76
+ screenshot 'index', tolerance: 0.01
77
+ end
78
+ ```
79
+
80
+ You can also set this globally:
81
+
82
+ ```ruby
83
+ # Default for VIPS is 0.001 (0.1% pixel difference allowed)
84
+ Capybara::Screenshot::Diff.tolerance = 0.001
85
+ ```
86
+
87
+ ## Median filter size (vips only)
88
+
89
+ This is an alternative to "Allowed shift distance", but much faster.
90
+ You can find more about this strategy on [Median Filter](https://en.wikipedia.org/wiki/Median_filter).
91
+ Think about this like smoothing of the image, before comparison.
92
+
93
+ You can use the `median_filter_window_size` option to the `screenshot` method to set level:
94
+
95
+ ```ruby
96
+ test 'unstable area' do
97
+ visit '/'
98
+ screenshot 'index', median_filter_window_size: 2
99
+ end
100
+ ```
101
+
102
+ [← Back to README](../README.md)
@@ -0,0 +1,87 @@
1
+ # Framework Setup
2
+
3
+ ## Including DSL
4
+
5
+ To use the screenshot capturing and change detection features in your tests, include the `CapybaraScreenshotDiff::DSL` in your test classes. It provides the `screenshot` method to capture and compare screenshots.
6
+
7
+ There are different modules for different testing frameworks integrations.
8
+
9
+ ## Minitest
10
+
11
+ For Minitest, need to require `capybara_screenshot_diff/minitest`.
12
+ In your test class, include the `CapybaraScreenshotDiff::Minitest::Assertions` module:
13
+
14
+ ```ruby
15
+ require 'capybara_screenshot_diff/minitest'
16
+
17
+ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
18
+ # Make the Capybara & Capybara Screenshot Diff DSLs available in tests
19
+ include CapybaraScreenshotDiff::DSL
20
+ # Make `assert_*` methods behave like Minitest assertions
21
+ include CapybaraScreenshotDiff::Minitest::Assertions
22
+
23
+ def test_my_feature
24
+ visit '/'
25
+ assert_matches_screenshot 'index'
26
+ end
27
+ end
28
+ ```
29
+
30
+ ## RSpec
31
+
32
+ To use the screenshot capturing and change detection features in your tests,
33
+ include the `CapybaraScreenshotDiff::DSL` in your test classes.
34
+ It adds `match_screenshot` matcher to RSpec.
35
+
36
+ > **Important**:
37
+ > The `CapybaraScreenshotDiff::DSL` is automatically included in all feature and system tests by default.
38
+
39
+
40
+ ```ruby
41
+ require 'capybara_screenshot_diff/rspec'
42
+
43
+ describe 'Permissions admin', type: :feature do
44
+ it 'works with permissions' do
45
+ visit('/')
46
+ expect(page).to match_screenshot('home_page')
47
+ end
48
+ end
49
+
50
+
51
+ describe 'Permissions admin', type: :non_feature do
52
+ include CapybaraScreenshotDiff::DSL
53
+
54
+ it 'works with permissions' do
55
+ visit('/')
56
+ expect(page).to match_screenshot('home_page')
57
+ end
58
+ end
59
+ ```
60
+
61
+ ## Cucumber
62
+
63
+ Load Cucumber support by adding the following line (typically to your `features/support/env.rb` file):
64
+
65
+ ```ruby
66
+ require 'capybara_screenshot_diff/cucumber'
67
+ ```
68
+
69
+ And in the steps you can use:
70
+
71
+ ```ruby
72
+ Then('I should not see any visual difference') do
73
+ screenshot 'homepage'
74
+ end
75
+ ```
76
+
77
+ ## Custom Test Frameworks
78
+
79
+ Minitest, RSpec, and Cucumber are supported out of the box. For other frameworks, call `finalize_reporters!` in your framework's "after suite" hook:
80
+
81
+ ```ruby
82
+ CapybaraScreenshotDiff.finalize_reporters!
83
+ ```
84
+
85
+ This generates the HTML report and prints the summary.
86
+
87
+ [← Back to README](../README.md)
Binary file
Binary file
@@ -0,0 +1,286 @@
1
+ # Migration Guide
2
+
3
+ Migrate your visual regression testing from other tools to `capybara-screenshot-diff`. This guide covers the most common migration paths.
4
+
5
+ ## Why Switch?
6
+
7
+ | Factor | Percy / Chromatic | BackstopJS | `capybara-screenshot-diff` |
8
+ |--------|-------------------|------------|--------------------------|
9
+ | Pricing | Paid SaaS (snapshot limits) | Free | Free (MIT) |
10
+ | Infrastructure | Cloud service, API tokens | Node + Puppeteer | Ruby gem, no external services |
11
+ | Baselines | Hosted on their servers | Local files | Git (committed to repo) |
12
+ | Review | Web dashboard | HTML report | HTML report + GitHub PR comments |
13
+ | PR integration | GitHub app | Manual CI steps | Reusable GitHub Action |
14
+ | Offline | ❌ Requires internet | ✅ | ✅ |
15
+ | Diff in PRs | Screenshot in comment | Manual | Upload artifact + PR comment |
16
+
17
+ ## From Percy
18
+
19
+ ### Setup changes
20
+
21
+ **Before (Percy):**
22
+ ```ruby
23
+ # Gemfile
24
+ gem 'percy-capybara'
25
+
26
+ # test helper
27
+ require 'percy/capybara'
28
+
29
+ # test
30
+ def test_homepage
31
+ visit '/'
32
+ Percy::Capybara.screenshot('homepage')
33
+ end
34
+ ```
35
+
36
+ **After (capybara-screenshot-diff):**
37
+ ```ruby
38
+ # Gemfile
39
+ gem 'capybara-screenshot-diff'
40
+
41
+ # test helper
42
+ require 'capybara_screenshot_diff/minitest'
43
+
44
+ # test class
45
+ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
46
+ include CapybaraScreenshotDiff::Minitest::Assertions
47
+
48
+ test "homepage" do
49
+ visit '/'
50
+ screenshot 'homepage'
51
+ end
52
+ end
53
+ ```
54
+
55
+ ### What changes
56
+
57
+ | Concept | Percy | capybara-screenshot-diff |
58
+ |---------|-------|-------------------------|
59
+ | Baseline storage | Percy cloud | Committed to git (`doc/screenshots/`) |
60
+ | First run | Uploads to Percy | Saves locally, passes automatically |
61
+ | CI setup | `PERCY_TOKEN` env var | GitHub Action (3 lines) |
62
+ | Diff review | Percy dashboard | `snap_diff_report.html` or PR artifacts |
63
+ | Update baselines | Percy's "Approve" button | Delete file, re-run tests, commit |
64
+ | Snapshot limits | Paid plan dependent | Unlimited |
65
+ | Parallel builds | Built-in | Thread-safe with t-locals + mutex |
66
+
67
+ ### CI migration
68
+
69
+ **Before (Percy GitHub Action):**
70
+ ```yaml
71
+ - name: Percy Test
72
+ run: PERCY_TOKEN=${{ secrets.PERCY_TOKEN }} bundle exec rake test
73
+ ```
74
+
75
+ **After (capybara-screenshot-diff):**
76
+ ```yaml
77
+ - uses: snap-diff/snap_diff-capybara/.github/actions/setup-ruby-and-dependencies@master
78
+ with:
79
+ ruby-version: '4.0'
80
+ - run: bundle exec rake test
81
+ - uses: snap-diff/snap_diff-capybara/.github/actions/upload-screenshots@master
82
+ if: failure()
83
+ with:
84
+ name: screenshots
85
+ pr-comment: 'true'
86
+ ```
87
+
88
+ ### Migration steps
89
+
90
+ 1. **Remove Percy gem and configuration**
91
+ 2. **Add `capybara-screenshot-diff`** to your Gemfile
92
+ 3. **Replace `Percy::Capybara.screenshot` calls** with `screenshot` (or `match_screenshot` for RSpec)
93
+ 4. **Run tests once** to generate baselines
94
+ 5. **Commit baselines** (`git add doc/screenshots/`)
95
+ 6. **Set up CI** with the GitHub Actions upload step
96
+ 7. **Remove Percy integration** from CI
97
+
98
+ ---
99
+
100
+ ## From Chromatic
101
+
102
+ ### Setup changes
103
+
104
+ **Before (Chromatic + Storybook):**
105
+ ```js
106
+ // .storybook/preview.js
107
+ import { withScreenshot } from 'chromatic';
108
+
109
+ export const decorators = [withScreenshot];
110
+ ```
111
+
112
+ **After (capybara-screenshot-diff):**
113
+ ```ruby
114
+ # test/system/stories_test.rb
115
+ class StoriesTest < ApplicationSystemTestCase
116
+ test "landing page story" do
117
+ visit '/iframe.html?id=pages-landing--default'
118
+ screenshot 'stories/landing-page'
119
+ end
120
+ end
121
+ ```
122
+
123
+ ### Key differences
124
+
125
+ | Concept | Chromatic | capybara-screenshot-diff |
126
+ |---------|-----------|-------------------------|
127
+ | Focus | Storybook components | Full-page system tests |
128
+ | Baseline | Chromatic cloud | Git-committed |
129
+ | Review | Chromatic web UI | HTML report + PR artifacts |
130
+ | CI integration | Chromatic GitHub App | GitHub Actions + PR comments |
131
+ | Thresholds | Visual catch (AI) | Configurable tolerance (numeric) |
132
+
133
+ ### Migration approach
134
+
135
+ Chromatic is primarily for Storybook component testing. If you want to continue testing individual components:
136
+
137
+ 1. **Replace with Capybara system tests** that visit each component's rendered page
138
+ 2. **Use `crop:` option** to isolate specific elements: `screenshot 'button', crop: '.my-button'`
139
+ 3. **Use `skip_area:` option** to ignore dynamic regions: `screenshot 'dashboard', skip_area: ['.timestamp']`
140
+
141
+ ---
142
+
143
+ ## From BackstopJS
144
+
145
+ ### Setup changes
146
+
147
+ **Before (BackstopJS):**
148
+ ```json
149
+ // backstop.json
150
+ {
151
+ "id": "homepage",
152
+ "viewports": [{"width": 1280, "height": 1024}],
153
+ "scenarios": [{
154
+ "label": "Homepage",
155
+ "url": "http://localhost:3000",
156
+ "referenceUrl": "http://localhost:3000",
157
+ "selectors": ["document"]
158
+ }],
159
+ "paths": {
160
+ "bitmaps_reference": "backstop_data/bitmaps_reference",
161
+ "bitmaps_test": "backstop_data/bitmaps_test",
162
+ "html_report": "backstop_data/html_report"
163
+ }
164
+ }
165
+ ```
166
+
167
+ **After (capybara-screenshot-diff):**
168
+ ```ruby
169
+ class HomepageTest < ApplicationSystemTestCase
170
+ test "homepage" do
171
+ visit '/'
172
+ screenshot 'homepage'
173
+ end
174
+ end
175
+ ```
176
+
177
+ ### Key differences
178
+
179
+ | Concept | BackstopJS | capybara-screenshot-diff |
180
+ |---------|-----------|-------------------------|
181
+ | Language | JavaScript + Node | Ruby (runs in test suite) |
182
+ | Dependencies | Node, Puppeteer/Chromium | Ruby gems + optional libvips |
183
+ | Test runner | Standalone CLI | Minitest, RSpec, Cucumber |
184
+ | Selectors | CSS selectors for scenarios | CSS selectors for crop/skip_area |
185
+ | Viewports | Per-scenario config | Global `window_size` setting |
186
+ | CI report | HTML report | HTML report + GitHub Actions |
187
+ | Stability | `misMatchThreshold` + `delay` | `tolerance` + `stability_time_limit` |
188
+
189
+ ### Configuration mapping
190
+
191
+ | BackstopJS option | capybara-screenshot-diff equivalent |
192
+ |-------------------|-------------------------------------|
193
+ | `misMatchThreshold` | `tolerance` (0.0-1.0 scale, e.g. `0.01` = 1%) |
194
+ | `delay` | `stability_time_limit` (seconds) |
195
+ | `selectors` | `crop:` option with CSS selector |
196
+ | `hideSelectors` | `skip_area:` option with CSS selectors |
197
+ | `removeSelectors` | N/A — use `skip_area` or modify DOM before screenshot |
198
+ | `waitTimeout` | `wait:` option (defaults to `Capybara.default_max_wait_time`) |
199
+ | `viewports` | `window_size: [width, height]` |
200
+ | `onReadyScript` | Custom setup in your test's `setup` block |
201
+
202
+ ### CI migration
203
+
204
+ **Before (BackstopJS in CI):**
205
+ ```yaml
206
+ - run: npx backstop test --config=backstop.json
207
+ ```
208
+
209
+ **After (capybara-screenshot-diff in CI):**
210
+ ```yaml
211
+ - uses: snap-diff/snap_diff-capybara/.github/actions/setup-ruby-and-dependencies@master
212
+ - run: bundle exec rake test
213
+ - uses: snap-diff/snap_diff-capybara/.github/actions/upload-screenshots@master
214
+ if: failure()
215
+ with:
216
+ name: screenshots
217
+ pr-comment: 'true'
218
+ ```
219
+
220
+ ### Migration steps
221
+
222
+ 1. **Remove BackstopJS configuration** (`backstop.json`, npm dependencies)
223
+ 2. **Convert scenarios to Capybara tests** — each scenario becomes a `screenshot` call
224
+ 3. **Map threshold and delay settings** to `tolerance` and `stability_time_limit`
225
+ 4. **Run tests** to generate baselines
226
+ 5. **Commit baselines** (`git add doc/screenshots/`)
227
+ 6. **Update CI** to use the GitHub Actions setup
228
+
229
+ ---
230
+
231
+ ## General Migration Checklist
232
+
233
+ - [ ] Remove old gem/npm dependencies
234
+ - [ ] Add `capybara-screenshot-diff` to Gemfile
235
+ - [ ] Require the appropriate adapter (`minitest`, `rspec`, or `cucumber`)
236
+ - [ ] Replace screenshot calls with `screenshot` / `match_screenshot`
237
+ - [ ] Configure `window_size` for consistent viewport dimensions
238
+ - [ ] Set `tolerance` or `perceptual_threshold` if your previous tool had a mismatch threshold
239
+ - [ ] Add `.gitignore` patterns for diff artifacts
240
+ - [ ] Run tests to generate baseline screenshots
241
+ - [ ] Commit baselines to git
242
+ - [ ] Set up CI with artifact upload
243
+ - [ ] Optional: add HTML reporter and PR commenting
244
+
245
+ ## Common Gotchas
246
+
247
+ ### "My baselines are on Percy/Chromatic servers"
248
+
249
+ You'll need to take fresh screenshots. Either:
250
+ - Visit each page and capture manually
251
+ - Run tests with `RECORD_SCREENSHOTS=1` to generate all baselines at once
252
+
253
+ ### "I had hundreds of BackstopJS scenarios"
254
+
255
+ Start small. Migrate one test file at a time. The `screenshot_group` feature helps organize related screenshots:
256
+ ```ruby
257
+ screenshot_group 'checkout'
258
+ screenshot 'step1'
259
+ screenshot 'step2'
260
+ # Produces: doc/screenshots/checkout/00_step1.png, 01_step2.png
261
+ ```
262
+
263
+ ### "My tests are slow now"
264
+
265
+ Use the VIPS driver for ~50ms comparisons per image:
266
+ ```ruby
267
+ gem 'ruby-vips'
268
+ Capybara::Screenshot::Diff.driver = :vips
269
+ ```
270
+
271
+ ### "The diffs look different from what I'm used to"
272
+
273
+ Each tool uses different comparison algorithms:
274
+ - **Percy:** Proprietary pixel-level comparison with AI smoothing
275
+ - **Chromatic:** Visual catch algorithm (structure-aware)
276
+ - **BackstopJS:** Resemble.js pixel comparison
277
+ - **capybara-screenshot-diff:** Raw pixel difference with configurable tolerance
278
+
279
+ Start with default settings, then adjust `tolerance` or `perceptual_threshold` based on your needs.
280
+
281
+ ## Need Help?
282
+
283
+ - [Architecture Overview](docs/architecture.md) — understanding how comparisons work
284
+ - [Configuration Reference](docs/configuration.md) — all available options
285
+ - [CI Integration](docs/ci-integration.md) — setting up in CI
286
+ - [GitHub Issues](https://github.com/snap-diff/snap_diff-capybara/issues) — ask questions
@@ -0,0 +1,204 @@
1
+ # Screenshot Organization
2
+
3
+ ## Taking screenshots
4
+
5
+ Add `screenshot '<my_feature>'` to your tests. The screenshot will be saved in
6
+ the `doc/screenshots` directory.
7
+
8
+ Change your existing `save_screenshot` calls to `screenshot`
9
+
10
+ ```ruby
11
+ test 'my useful feature' do
12
+ visit '/'
13
+ screenshot 'welcome_index'
14
+ click_button 'Useful feature'
15
+ screenshot 'feature_index'
16
+ click_button 'Perform action'
17
+ screenshot 'action_performed'
18
+ end
19
+ ```
20
+
21
+ This will produce a sequence of images like this
22
+
23
+ ```
24
+ doc
25
+ screenshots
26
+ action_performed
27
+ feature_index
28
+ welcome_index
29
+ ```
30
+
31
+ To store the screenshot history, add the `doc/screenshots` directory to your
32
+ version control system (git).
33
+
34
+ Screenshots are compared to the previously COMMITTED version of the same screenshot.
35
+
36
+ **Note:** When a screenshot differs, diff artifacts (`.diff.png`, `.heatmap.diff.png`, etc.) are generated alongside the baseline. Add `*.diff.png`, `*.base.png`, `*.diff.webp`, `*.base.webp`, and `snap_diff_report.html` to your `.gitignore`. Clean up artifacts with `rake snap_diff:clean`.
37
+
38
+ ## Screenshot groups
39
+
40
+ Commonly it is useful to group screenshots around a feature, and record them as
41
+ a sequence. To do this, add a `screenshot_group` call to the start of your
42
+ test.
43
+
44
+ ```ruby
45
+ test 'my useful feature' do
46
+ screenshot_group 'useful_feature'
47
+ visit '/'
48
+ screenshot 'welcome_index'
49
+ click_button 'Useful feature'
50
+ screenshot 'feature_index'
51
+ click_button 'Perform action'
52
+ screenshot 'action_performed'
53
+ end
54
+ ```
55
+
56
+ This will produce a sequence of images like this
57
+
58
+ ```
59
+ doc
60
+ screenshots
61
+ useful_feature
62
+ 00_welcome_index
63
+ 01_feature_index
64
+ 02_action_performed
65
+ ```
66
+
67
+ **Note:** `screenshot_group` sets the group name for organizing screenshots. It does not delete existing files.
68
+
69
+
70
+ ## Screenshot sections
71
+
72
+ You can introduce another level above the screenshot group called a
73
+ `screenshot_section`. The section name is inserted just before the group name
74
+ in the save path. If called in the setup of the test, all screenshots in
75
+ that test will get the same prefix:
76
+
77
+ ```ruby
78
+ setup do
79
+ screenshot_section 'my_feature'
80
+ end
81
+
82
+ test 'my subfeature' do
83
+ screenshot_group 'subfeature'
84
+ visit '/feature'
85
+ click_button 'Interesting button'
86
+ screenshot 'subfeature_index'
87
+ click_button 'Perform action'
88
+ screenshot 'action_performed'
89
+ end
90
+ ```
91
+
92
+ This will produce a sequence of images like this
93
+
94
+ ```
95
+ doc
96
+ screenshots
97
+ my_feature
98
+ subfeature
99
+ 00_subfeature_index
100
+ 01_action_performed
101
+ ```
102
+
103
+
104
+ ## Setting `screenshot_section` and/or `screenshot_group` for all tests
105
+
106
+ Setting the `screenshot_section` and/or `screenshot_group` for all tests can be
107
+ done in the super class setup:
108
+
109
+ ```ruby
110
+ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
111
+ setup do
112
+ screenshot_section class_name.underscore.sub(/(_feature|_system)?_test$/, '')
113
+ screenshot_group name[5..-1]
114
+ end
115
+ end
116
+ ```
117
+
118
+ `screenshot_section` and/or `screenshot_group` can still be overridden in each
119
+ test.
120
+
121
+
122
+ ## Capturing one area instead of the whole page
123
+
124
+ You can crop images before comparison to be run, by providing region to crop as `[left, top, right, bottom]` or by css selector like `body .tag`
125
+
126
+ ```ruby
127
+ test 'the cool' do
128
+ visit '/feature'
129
+ screenshot 'cool_element', crop: '#my_element'
130
+ end
131
+ ```
132
+
133
+ **Note:** When using a retina device screenshots dimensions might be off. If
134
+ you are using (headless) chrome you can prevent this by setting the
135
+ `force-device-scale-factor` argument to `1`.
136
+
137
+ For Rails system specs using selenium you can do so for example by using the
138
+ following snippet:
139
+
140
+ ```ruby
141
+ driven_by :selenium, using: :chrome_headless do |options|
142
+ options.args << '--force-device-scale-factor=1'
143
+ end
144
+ ```
145
+
146
+ ## Multiple Capybara drivers
147
+
148
+ Often it is useful to test your app using different browsers. To avoid the
149
+ screenshots for different Capybara drivers to overwrite each other, set
150
+
151
+ ```ruby
152
+ Capybara::Screenshot.add_driver_path = true
153
+ ```
154
+
155
+ The example above will then save your screenshots like this
156
+ (for poltergeist and selenium):
157
+
158
+ ```
159
+ doc
160
+ screenshots
161
+ poltergeist
162
+ useful_feature
163
+ 00_welcome_index
164
+ 01_feature_index
165
+ 02_action_performed
166
+ selenium
167
+ useful_feature
168
+ 00_welcome_index
169
+ 01_feature_index
170
+ 02_action_performed
171
+ ```
172
+
173
+ ## Multiple OSs
174
+
175
+ If you run your tests on multiple operating systems, you will most likely find
176
+ the screen shots differ. To avoid the screenshots for different OSs to
177
+ overwrite each other, set
178
+
179
+ ```ruby
180
+ Capybara::Screenshot.add_os_path = true
181
+ ```
182
+
183
+ The example above will then save your screenshots like this
184
+ (for Linux and Windows):
185
+
186
+ ```
187
+ doc
188
+ screenshots
189
+ linux
190
+ useful_feature
191
+ 00_welcome_index
192
+ 01_feature_index
193
+ 02_action_performed
194
+ windows
195
+ useful_feature
196
+ 00_welcome_index
197
+ 01_feature_index
198
+ 02_action_performed
199
+ ```
200
+
201
+ If you combine this config with the `add_driver_path` config, the driver will be
202
+ put in front of the OS name.
203
+
204
+ [← Back to README](../README.md)