imp - Clips
imp clip record every *.clip.js file, each in its own browser
imp clip demo.clip.js record one clip file
imp clip --video public write the videos into ./public
imp clip --video-format gif write GIF files in place of MP4 files
imp clip --env app serve the app with the "app" environment of imp.json
imp clip --list print the clip files as JSON, and record nothing
A clip is a short video of your app that people watch. Use it for a demo in a pull request, a clip
on a landing page, or an animation in a README. You plan the scene in a *.clip.js file, and
imp clip records it. Time in the page moves only from frame to frame. So the video is smooth, and
the same scene gives the same frames each time.
A clip file is not a test. imp test never runs one, so a test run never writes your videos again.
To see why an e2e spec fails, record the spec with imp test --video instead. See
Testing.
How to write a clip
A clip file looks like an e2e spec. It imports test and expect from imp/test. imp serves your
app, opens a page on it, and gives each test { page, browser, baseUrl, record }.
import { test } from 'imp/test'
test('gameplay', async ({ page, record }) => {
const clip = await record({
fps: 30,
viewport: { width: 360, height: 640, deviceScaleFactor: 1.6, isMobile: true, hasTouch: true },
clip: { x: 0, y: 0, width: 360, height: 480 },
maxBytes: 195_000,
})
await page.click('#start')
await clip.until(() => game.time > 7)
clip.still(clip.seconds + 0.6)
await clip.play(2.5)
})
Save this file as gameplay.clip.js, then run it:
imp clip gameplay.clip.js --video public --env app
imp writes public/gameplay.mp4 and public/gameplay.jpg. Use them on a web page in a muted
<video autoplay loop playsinline poster="gameplay.jpg"> element.
Where imp writes the video
You do not name the file in the clip. imp names it after the clip file, and the command chooses the folder and the format. So one clip file can give a GIF for a pull request and an MP4 for a page.
- The folder is the value of
--video <dir>. Without the flag, it isclip.dirofimp.json, then.imp/clips/. - The format is the value of
--video-format,mp4orgif. Without the flag, it isclip.formatofimp.json, thenmp4. - The name is the name of the clip file without
.clip.js.gameplay.clip.jsgivesgameplay.mp4. When the file has more than one test, imp adds the name of the test:demo.clip.jswith the testopens the menugivesdemo-opens-the-menu.mp4.
imp writes the video when the test passes. A test that fails writes nothing, so a broken run does
not replace a good video. A test that does not call record() writes nothing.
{
"clip": {
"dir": "public",
"format": "mp4"
}
}
The clip uses the same served page and browser as an e2e spec: test.entry and test.browser of
imp.json. --mock and --no-mock work as for imp test.
A clip for a pull request
A GIF plays inside the markdown of a GitHub pull request, so a reviewer sees it without a download. An MP4 file is much smaller, but GitHub plays it only when you drag it into a comment box.
imp clip checkout.clip.js --video-format gif
Drag .imp/clips/checkout.gif into the comment box of the pull request. A reviewer can then check
that the feature works, and that it looks right, without running the branch.
How time moves in a clip
record() loads the page again, and gives it a clock of its own before the first script of your
app runs. From then on, time in the page moves only when the clip takes a frame. Each frame moves
the clock by exactly 1 / fps of a second. Then imp takes a screenshot. A slow screenshot does not
make the clip jerky.
The clock of the page controls these:
setTimeout,setIntervalandrequestAnimationFrame.performance.now(),Date.now()andnew Date().- CSS transitions, CSS animations, and animations that your code starts with
element.animate().
A click, a key press or a page.evaluate() acts at once, at the current time of the clock. A wait
that needs time to pass, such as page.waitForFunction() with a condition on the clock, does not
end, because the clock stands still between frames. Use clip.until() for that wait.
At the end of the test, imp gives the page its real clock again. Timers that are still due run on real time.
The options of record
| Option | Default | What it does |
|---|---|---|
fps | 30 | the frames per second of the clip, and the step of the clock |
viewport | the current page | the size of the page, as for page.setViewport() |
clip | the whole page | the part of the page to record: { x, y, width, height }, in CSS px |
bitrate | from the size | the target bitrate of an MP4 file, in bits per second |
maxBytes | no limit | the largest file that imp can write |
A viewport stays on the page for the rest of the file. The size of the video in pixels is the
size of clip times the deviceScaleFactor of the viewport. An MP4 file needs an even width and
height, so imp drops a last odd row or column of pixels.
record() runs once per test. Put each clip in a test of its own. A test of a clip file can run for
10 minutes, because a long clip takes many screenshots. Pass { timeout } to test() to change it.
The methods of a clip
| Method | What it does |
|---|---|
await clip.play(seconds) | records frames for that many seconds |
await clip.until(fn) | records frames until fn(), run in the page, returns a truthy value |
await clip.skip(seconds) | moves the clock that many seconds, and records nothing |
clip.still(seconds) | also writes the frame at that second as <name>.jpg, next to the clip |
clip.frames | the number of frames recorded |
clip.seconds | the length of the clip, in seconds |
clip.until(fn, { limit }) fails after limit seconds of clip time. The default is 30 seconds.
clip.still() without an argument selects the frame of the current time. A time later than the
recorded frames, such as clip.seconds + 0.6, is correct when the clip records past it. The last
call wins.
maxBytes keeps a file under a size budget. For an MP4 file, imp encodes the frames again at a
lower bitrate until the file fits. A GIF has no bitrate, so the test fails when a GIF is too large.
Record fewer seconds, or a smaller clip.
An MP4 file is an H.264 video, which every current browser plays. The browser under test encodes the video, so you install nothing for it.