Capture screenshots automatically
Doc Detective builds a visual record of every test without a screenshot step in sight. By default, it saves an image after every step that drives a browser, a native app, or a device. New to capturing screenshots? Start with Capture screenshots.
Before you begin
Make sure you have the following:
- Doc Detective installed.
- A test with steps that drive a browser, a native app, or a device, such as
goTo,click,find, ortype.
The steps in this guide work the same in every authoring style. Write them in a standalone .spec.json, inline in a doc source file, or let Doc Detective detect them from prose.
What you get by default
With no setting at all, Doc Detective saves a screenshot after every step that drives a UI:
- Browser steps capture the page, headed or headless.
- Native app steps, on Windows and macOS, capture the app window.
- Device steps, on Android and iOS apps or mobile web, capture the device screen.
Steps that drive no UI, such as runShell, httpRequest, and wait, get no screenshot. Neither does a step with its own screenshot, since it already makes one. A step that fails still gets one, because the failure frame is usually the most useful one for debugging.
Screenshots land in the run folder, on a stable path for each step:
The number is the step’s place in the test, so the first step you wrote is 01-…. The IDs come from file paths and content hashes, not random values. So the same step lands on the same relative path in every run. Diff two run folders to spot UI changes over time.
While screenshots are on, the runFolder reporter runs even if reporters leaves it out. See Reporters and artifacts.
To look through the screenshots, open testResults.html in the run folder. The run prints its path after Run report:. The report loads the images from the run folder, so in CI, upload the whole folder. See Find the HTML report and Collect the run folder and artifacts.
Each reporter points at the images its own way:
Like screenshot steps, automatic browser screenshots blur elements whose text or form value holds a secret environment variable value. The blur appears in the saved image. Text inside iframes, shadow DOM, or a canvas isn’t blurred, and neither are native app and device screenshots. See Screenshots and recordings.
Automatic screenshots don’t compare against a reference. To gate on drift, add a screenshot step with maxVariation, as described in Detect visual regressions.
Turn them off
Set autoScreenshot to false at the config, spec, or test level. A test’s setting overrides a spec’s, which overrides the config’s. A step can’t set it: autoScreenshot on a step is an unknown key, so the spec fails validation.
For a single run, use the flag:
The flag sets the config level, so a spec or test that sets autoScreenshot to true still takes screenshots. To keep screenshots for one test and drop them everywhere else:
Verify it works
- Each UI step in the results has an
autoScreenshotPath. - The run folder holds a
screenshots/folder per context.
Troubleshooting
A step has no automatic screenshot
Cause: the step drives no UI, it defines its own screenshot, or autoScreenshot is false at some level.
Solution: this is expected. Check the setting at the test, spec, and config levels, in that order.
No step has an automatic screenshot
Cause: Doc Detective couldn’t install the image packages it needs, and printed Warning: Not taking automatic screenshots: couldn't install sharp (…).
Solution: run doc-detective install runtime sharp pngjs pixelmatch, then run the tests again. Or set autoScreenshot to false to stop trying.
What’s next
- Capture screenshots of the full viewport, a cropped element, or an element with padding.
- Detect visual regressions by comparing a capture against a reference image.
- Record video automatically, which is also on by default.
- Look up
autoScreenshotin theconfigschema. - Run in CI and at scale to see where artifacts land.