Skip to navigation

Screenshots and recordings, written as tests

Each example shows one procedure as prose, as detected tests, as inline tests, and as a standalone spec. For what each tab holds, see Navigation and interaction.

In a detected variant, a step statement holds each step that default detection can’t produce, such as a cropped screenshot or a record step. Each of those statements also works on its own, because a run with detection off reads only the statements. That’s why each detected variant also opens its page with a goTo statement.

The examples write their images and videos to a .dd-output folder next to the example files, and Git ignores that folder.

Take screenshots of a sign-in form

This procedure uses screenshot as a path and as an object with path, directory, maxVariation, overwrite, crop, and annotations. The test sets annotationDefaults and autoScreenshot.

prose.md
# Take screenshots of a sign-in form
1. Go to [the sample form](http://localhost:8092/enhanced-elements.html).
2. The form opens.
![The sample form](.dd-output/capture-form.png){.screenshot}
3. Find the **Submit by ID** button at the top of the form.
4. Fill in the sign-in fields in the order the numbers show.

What detection finds

An image with the screenshot class becomes a screenshot step that saves to the image’s path. Each format marks the class its own way:

FormatImage with the screenshot class
Markdown![The sample form](path.png){.screenshot}
MDX<img src="path.png" alt="The sample form" className="screenshot" />
AsciiDocimage:path.png[The sample form,role=screenshot]
HTML<img src="path.png" alt="The sample form" class="screenshot">
reStructuredText.. image:: path.png with :class: screenshot
DITA<image href="path.png" outputclass="screenshot">

The link also becomes a checkLink step, and the bold Submit by ID becomes a find step. The cropped and annotated screenshots are step statements, because detection only produces a screenshot of the whole viewport.

In AsciiDoc, the image inside a list item uses the inline image: macro. Detection doesn’t read a block image:: macro that follows a list continuation.

Screenshot forms in this example

StepWhat it does
"screenshot": ".dd-output/capture-form.png"Saves the viewport to the path. If a file is already there, compares against it with the default maxVariation of 0.05.
crop with elementText and per-side paddingSaves only the Submit by ID button. maxVariation of 0.2 with overwrite set to aboveVariation replaces the reference only when more than 20% of the pixels change.
crop with selector and a single paddingSaves the inputs section, then draws annotations on the image. overwrite set to true replaces the image on every run.

The annotations cover every annotation type, and each type’s target as a selector string:

AnnotationTarget and placement
badgeA selector string, nudged left with a position offset, and an elementTestId object with its own timeout.
calloutA selector string, placed to the right with a named position. Its style sets maxWidth, fontSize, background, and padding.
outlineA selector string, and an elementAttribute object whose style sets color, radius, and opacity.
blurA selector string. Its style sets the blur intensity.
arrowA selector string with a position point (x and y), and a target that’s only a point in the image.
textA selector string with the named position bottom, and a target that’s only a named region.

annotationDefaults on the test sets the color for every annotation type and the badge colors, and an annotation’s own style wins over it. The example’s detected run also sets annotationDefaults and overwriteScreenshots in its config, in docs/docs-as-tests/examples/capture-screenshots/doc-detective.json. A test’s theme wins over the config’s, and overwriteScreenshots replaces every step’s overwrite.

The test sets autoScreenshot to true, so Doc Detective also saves a screenshot after every step to the run folder. The standalone specs set it on the spec, where it applies to every test in the file. A test’s own autoScreenshot overrides the spec’s.

Point out the controls on a toolbar

This procedure uses annotate to add, update, and clear annotations that stay on the page across steps, and a screenshot that shows them.

prose.md
# Point out the controls on a toolbar
1. Go to [the toolbar page](http://localhost:8092/find-image.html).
2. Find the save button, marked 1, and the green check icon, outlined.
3. The status line at the top is hidden, because it changes on every visit.
4. Compare your toolbar with the screenshot.
5. Next, find the check icon, now marked 2.
6. That's every control on the toolbar.

Annotations in this example

AnnotationFields
save-badgeA badge on a selector string. The update step moves it to the check icon and changes its label.
check-outlineAn outline on an elementImage target, check-icon.svg in the example folder, with a threshold and a timeout. track keeps it on the icon if the page moves, and the draw transition sets how it appears.
hintA callout on an elementId with a position and fade transitions. clear with a list of ids removes it.
bannerA text box in the top-right region that clears itself after a duration of 3000 milliseconds.
redactA blur with all, so it hides every match, and the none transition, so the hidden text never shows.

clear set to true removes every annotation that’s left. The test’s annotationDefaults sets the font size, a background, opacity, radius, padding, maxWidth, and blur intensity for every type, the callout colors, and a default transition. The standalone specs set annotationDefaults on the spec instead, where it applies to every test in the file. A test’s theme wins over the spec’s. It sets autoScreenshot to false, so the run saves only the screenshot the procedure asks for.

track, transition, and duration change how annotations behave in a recording. A screenshot shows each annotation at the moment the screenshot is taken.

What detection finds

The link becomes a goTo step and a checkLink step. No default rule produces annotate or a screenshot without an image, so those are statements in every detected variant.

Record a walkthrough

This procedure uses record as true, as a path, and as an object with path, directory, name, surface, engine, overwrite, checkpoints, and verify. It stops recordings with stopRecord by name, with a name object, and with true, after a wait so each video has something in it.

prose.md
# Record a walkthrough
## Record the browser tab
1. Go to [the type echo page](http://localhost:8092/type-echo.html).
2. Start recording the tab.
3. Click **Type here:**.
4. Type "Hello, Doc Detective".
5. Wait a moment, then stop the recording.
6. Record a few seconds more with the default settings.
## Record the screen
1. Go to [the type echo page](http://localhost:8092/type-echo.html).
2. Start a screen recording.
3. Click **Type here:**.
4. Type "Recorded on screen".
5. Wait a moment, then stop the recording.
6. Record only the browser window.
7. Record only the page, and compare each step with the last run.
8. Record the screen again with the default ffmpeg settings.
9. Record a short clip of the result.
10. Wait a second, then stop the clip.

Where each test runs

Recording the screen needs a display, so each test sets runOn:

TestRuns onElsewhere
Record the browser tabHeaded Chrome on Windows and macOS, and headless Chrome on LinuxSKIPPED
Record the screenHeaded Chrome on Windows and macOSSKIPPED

The browser engine records the Chrome tab in a headed or a headless browser. The ffmpeg engine records the screen, so it needs a headed browser.

Recording forms in this example

StepWhat it does
record with name, surface, engine browser, checkpoints, and verifyRecords the Chrome tab to capture-walkthrough.webm. checkpoints saves a screenshot after each step and compares it with the last run’s. verify fails the stop step if the video is shorter than half a second, longer than two minutes, or black. With resolution set to true, it also fails if the video isn’t the tab’s size.
"stopRecord": { "name": "walkthrough" }Stops the recording named walkthrough.
"record": trueRecords to <stepId>.mp4 in outputDir, with the engine Doc Detective picks for the context.
record with name and an engine object, target displayRecords the whole screen to capture-screen.mp4 with ffmpeg at 15 frames a second.
"stopRecord": "screen"Stops the recording named screen.
engine object with target windowRecords only the active window.
engine object with target viewport, and "checkpoints": trueRecords only the browser’s content area. It compares a screenshot after each step with the last run’s, with the default settings.
"engine": "ffmpeg"Records with ffmpeg and its default target, the whole screen.
"record": ".dd-output/capture-quick.gif"Records a GIF. With no overwrite, it skips the recording when the file already exists.
"stopRecord": trueStops the most recent recording.

Every test sets autoRecord to false, so the run records only what the procedure asks for. The standalone specs set it once on the spec.

The detected, inline, and standalone variants write the same files. The first run of the GIF step writes capture-quick.gif, and later runs skip that step, which doesn’t fail the test. "record": true also skips when its file exists.

What detection finds

The link becomes a goTo step and a checkLink step. Type here: after “Click” becomes a click step and a find step, and the quoted text after “Type” becomes a type step. No default rule produces record, wait, or stopRecord, so those are statements in every detected variant.

Check pages for visual changes

This procedure compares new screenshot images with reference images. It uses threshold, maxDiffPixels, and mask to ignore what changes on every visit, crop as a selector string, surface, style, stable, animations, and fullPage. It also compares against a reference image at a URL, and takes a screenshot with true.

prose.md
# Check pages for visual changes
1. Go to [the billing page](http://localhost:8092/mask.html).
2. The page shows the next invoice date and when the account last synced. Those times change on every visit.
3. The billing panel sits at the top of the page.
4. Go to [the banner page](http://localhost:8092/screenshot-banner.html).
5. With the cookie banner hidden, the page shows only its content.
6. With the banner showing, it covers the bottom of the page.
7. Go to [the long page](http://localhost:8092/screenshot-tall.html).
8. Scroll to the red box at the bottom of the page.
9. Go to [the toolbar page](http://localhost:8092/find-image.html).
10. Compare the sample chart with the published chart image.

Screenshot options in this example

StepWhat it does
mask with a selector string and a selector objectPaints #clock and every .stamp element the same color in both images before comparing, so the times that change on every visit don’t count. threshold sets how different a pixel’s color must be to count as changed, and maxDiffPixels caps the changed pixels alongside maxVariation.
"crop": "#panel"Saves only the element that matches the selector or display text.
surface, style, stable, and animationsTakes the screenshot in the chrome browser surface with the cookie banner hidden by CSS. It waits until two images in a row match, and stops animations while it takes the screenshot.
"fullPage": trueSaves the whole page, including the parts you scroll to, not only the viewport.
path set to a URLCompares the chart on the page with find-image-chart.png on the test server. Doc Detective never writes to a URL reference, so the new image goes to the run folder.
"screenshot": trueSaves the viewport to <stepId>.png in outputDir.

The test runs in headless Chrome on Windows, macOS, and Linux. The first run of the masked step has no reference to compare with, so it saves one. Later runs compare with it.

What detection finds

Each link becomes a goTo step and a checkLink step. No default rule produces these screenshot options, so the screenshots are statements in every detected variant.

Permutations

Each example below varies one setting, so you can compare it with its neighbors. For the full procedures that combine them, see the sections above. For the other options, see Annotation options and Recording options.

screenshot permutations

Each example opens a test-server page and takes one screenshot. The pages whose content keeps changing open with a goTo that doesn’t wait for the page to stop changing.

String path

Varies: screenshot · Value: ".dd-output/banner.png" · Effect: saves the viewport to the path, and compares it with the file already there, if any.

prose.md
# Save a screenshot to a path
1. Go to [the banner page](http://localhost:8092/screenshot-banner.html).
2. The page opens with a cookie banner at the bottom.

Object with path and directory

Varies: screenshot · Value: { "path", "directory" } · Effect: saves the viewport to banner.png in the .dd-output folder.

prose.md
# Save a screenshot with a path and a folder
1. Go to [the banner page](http://localhost:8092/screenshot-banner.html).
2. The page opens with a cookie banner at the bottom.

true

Varies: screenshot · Value: true · Effect: saves the viewport to <stepId>.png in outputDir.

prose.md
# Save a screenshot with the default name
1. Go to [the banner page](http://localhost:8092/screenshot-banner.html).
2. The page opens with a cookie banner at the bottom.

overwrite: "true"

Varies: screenshot.overwrite · Value: "true" · Effect: replaces the saved image on every run without comparing.

prose.md
# Save a screenshot with overwrite true
1. Go to [the banner page](http://localhost:8092/screenshot-banner.html).
2. The page opens with a cookie banner at the bottom.

overwrite: "false"

Varies: screenshot.overwrite · Value: "false" · Effect: keeps the saved image, and skips the step when the file already exists.

prose.md
# Save a screenshot with overwrite false
1. Go to [the banner page](http://localhost:8092/screenshot-banner.html).
2. The page opens with a cookie banner at the bottom.

overwrite: "aboveVariation"

Varies: screenshot.overwrite · Value: "aboveVariation" · Effect: replaces the saved image only when the new one differs by more than maxVariation.

prose.md
# Save a screenshot with overwrite aboveVariation
1. Go to [the banner page](http://localhost:8092/screenshot-banner.html).
2. The page opens with a cookie banner at the bottom.

maxVariation: 0.01

Varies: screenshot.maxVariation · Value: 0.01 · Effect: warns when more than 1% of the pixels differ from the saved image.

prose.md
# Compare a screenshot with low tolerance
1. Go to [the banner page](http://localhost:8092/screenshot-banner.html).
2. The page opens with a cookie banner at the bottom.

maxVariation: 0.5

Varies: screenshot.maxVariation · Value: 0.5 · Effect: warns only when more than half of the pixels differ from the saved image.

prose.md
# Compare a screenshot with high tolerance
1. Go to [the banner page](http://localhost:8092/screenshot-banner.html).
2. The page opens with a cookie banner at the bottom.

crop as a string

Varies: screenshot.crop · Value: "#submit-button" · Effect: saves only the element that matches the selector or display text.

prose.md
# Save a screenshot of one button
1. Go to [the sample form](http://localhost:8092/enhanced-elements.html).
2. The **Submit by ID** button is at the top of the form.

crop.padding as a number

Varies: screenshot.crop.padding · Value: 10 · Effect: adds 10 pixels on every side of the element.

prose.md
# Save a screenshot of one button with even padding
1. Go to [the sample form](http://localhost:8092/enhanced-elements.html).
2. The **Submit by ID** button is at the top of the form.

crop.padding as an object

Varies: screenshot.crop.padding · Value: { "top": 8, "right": 16, "bottom": 8, "left": 16 } · Effect: adds a different amount of space on each side of the element.

prose.md
# Save a screenshot of one button with uneven padding
1. Go to [the sample form](http://localhost:8092/enhanced-elements.html).
2. The **Submit by ID** button is at the top of the form.

fullPage: true

Varies: screenshot.fullPage · Value: true · Effect: saves the whole page, including the parts you scroll to.

prose.md
# Save a screenshot of a long page with fullPage true
1. Open the page at `http://localhost:8092/screenshot-tall.html`.
2. The page ends with a red box at the bottom.

fullPage: false

Varies: screenshot.fullPage · Value: false · Effect: saves only what fits in the viewport.

prose.md
# Save a screenshot of a long page with fullPage false
1. Open the page at `http://localhost:8092/screenshot-tall.html`.
2. The page ends with a red box at the bottom.

mask with strings

Varies: screenshot.mask · Value: ["#clock", "#stamps"] · Effect: hides the first match of each selector in both images before comparing, so the times don’t count.

prose.md
# Compare a screenshot with string masks
1. Go to [the billing page](http://localhost:8092/mask.html).
2. The page shows the next invoice date and when the account last synced.

mask with an element object

Varies: screenshot.mask · Value: [{ "selector": ".stamp" }, "#clock"] · Effect: hides every match of the object’s selector, and the first match of the string.

prose.md
# Compare a screenshot with an element mask
1. Go to [the billing page](http://localhost:8092/mask.html).
2. The page shows the next invoice date and when the account last synced.

mask with elementClass

Varies: screenshot.mask · Value: [{ "elementClass": "stamp" }, "#clock"] · Effect: hides every element with the stamp class, and the first match of the string. A mask object takes the same locator fields as find.

prose.md
# Compare a screenshot with an element mask
1. Go to [the billing page](http://localhost:8092/mask.html).
2. The page shows the next invoice date and when the account last synced.

stable: true

Varies: screenshot.stable · Value: true · Effect: takes screenshots until two in a row match, so the loading bar has finished.

prose.md
# Wait for a page to settle before the screenshot
1. Open the page at `http://localhost:8092/screenshot-late-reveal.html`.
2. A loading bar fills, then the page shows its content.

animations: "disabled"

Varies: screenshot.animations · Value: "disabled" · Effect: stops the spinner during the screenshot, so every run saves the same image.

prose.md
# Save a screenshot of an animated page with animations disabled
1. Open the page at `http://localhost:8092/screenshot-loop.html`.
2. A spinner turns in the middle of the page.

animations: "allow"

Varies: screenshot.animations · Value: "allow" · Effect: leaves the spinner running, so the image shows it wherever it happens to be.

prose.md
# Save a screenshot of an animated page with animations allow
1. Open the page at `http://localhost:8092/screenshot-loop.html`.
2. A spinner turns in the middle of the page.

An image with the screenshot class

Varies: detection: image class · Value: screenshot · Effect: default detection turns the image into a screenshot step that saves to the image’s path.

prose.md
# Mark an image as a screenshot
1. Go to [the banner page](http://localhost:8092/screenshot-banner.html).
2. The page opens with a cookie banner at the bottom.
![The banner page](.dd-output/detected-banner.png){.screenshot}