Recording
Screen Recording
Record screen activity with configurable FPS, buffer mode, and cursor visibility.
Overview
The @nut-tree/plugin-screenrecording plugin adds screen recording capabilities to nut.js. Record automation sessions for debugging or use buffer mode to capture only the last N seconds before a failure.
Recording
Record screen to video files
screen.startRecording(options)Buffer Mode
Keep the last N seconds in memory
bufferSeconds: 30Discard
Discard buffered recordings on success
screen.discardRecording()Installation
npm install @nut-tree/plugin-screenrecordingSubscription Required
FFmpeg Required
Quick Reference
useScreenRecorder
useScreenRecorder()Activate the screen recording plugin
screen.startRecording
screen.startRecording(options?: RecordingOptions)Start recording the screen
screen.stopRecording
screen.stopRecording(options?: StopRecordingOptions)Stop the current recording and save the video file
screen.discardRecording
screen.discardRecording()Discard the current recording without saving
Recording
Basic Recording
Record the screen to a video file:
import { screen } from "@nut-tree/nut-js";
import { useScreenRecorder } from "@nut-tree/plugin-screenrecording";
useScreenRecorder();
// Start recording
await screen.startRecording({ fps: 30, showCursor: true });
// ... perform automation ...
// Stop recording and save to file
await screen.stopRecording({ outputPath: "./recording.mp4" });Recording with Options
import { screen } from "@nut-tree/nut-js";
import type { RecordingOptions } from "@nut-tree/plugin-screenrecording";
import { useScreenRecorder } from "@nut-tree/plugin-screenrecording";
useScreenRecorder();
const options: RecordingOptions = {
fps: 30,
showCursor: true,
bufferSeconds: 60,
};
await screen.startRecording(options);
// ... perform automation ...
await screen.stopRecording({ outputPath: "./test-recording.mp4" });Buffer Mode
Buffer mode keeps the last N seconds of screen activity in memory. When the recording is stopped, only the buffered portion is written to disk. If the test succeeds, you can discard the buffer entirely with screen.discardRecording():
import { screen } from "@nut-tree/nut-js";
import { useScreenRecorder } from "@nut-tree/plugin-screenrecording";
useScreenRecorder();
// Start recording with a 30-second buffer
await screen.startRecording({
bufferSeconds: 30,
fps: 30,
showCursor: true,
});
try {
// ... perform automation ...
// Test passed — discard the recording
await screen.discardRecording();
} catch (error) {
// Test failed — save the last 30 seconds
await screen.stopRecording({ outputPath: "./failure-capture.mp4" });
throw error;
}CI/CD Use Case
Test Framework Integration
Vitest Error Videos
Capture error videos automatically for failing tests using Vitest lifecycle hooks. The buffer records continuously and is either saved on failure or discarded on success:
import { afterEach, beforeAll, beforeEach, describe, it } from "vitest";
import { down, left, mouse, right, screen, up } from "@nut-tree/nut-js";
import type { RecordingOptions } from "@nut-tree/plugin-screenrecording";
import { useScreenRecorder } from "@nut-tree/plugin-screenrecording";
const recordingOptions: RecordingOptions = {
bufferSeconds: 30,
fps: 30,
showCursor: true,
};
beforeAll(() => {
useScreenRecorder();
});
beforeEach(async () => {
await screen.startRecording(recordingOptions);
});
afterEach(async (context) => {
const failed = context.task.result?.state === "fail";
if (failed) {
const safeName = context.task.name.replace(/[^a-zA-Z0-9]+/g, "_");
await screen.stopRecording({
outputPath: `./${safeName}_error_recording.mp4`,
});
} else {
await screen.discardRecording();
}
});
describe("Screen recording", () => {
it("should move the mouse in a square", async () => {
await mouse.move(right(500));
await mouse.move(down(500));
await mouse.move(left(500));
await mouse.move(up(500));
}, 30_000);
it.fails("should detect a simulated failure", async () => {
await mouse.move(right(200));
await mouse.move(down(200));
throw new Error("Simulated failure");
}, 30_000);
});Options Reference
RecordingOptions
fps
fps?: numberFrames per second for the recording. Higher values produce smoother video but larger files.
showCursor
showCursor?: booleanWhether to show the cursor in the recording.
bufferSeconds
bufferSeconds?: numberEnable buffer mode and keep only the last N seconds. Use with discardRecording() to discard on success.
StopRecordingOptions
outputPath
outputPath?: stringFile path for the output video. If not set, a default path is generated.
Best Practices
Recording Tips
- Use buffer mode in CI/CD to capture failures without filling up disk space
- Call
discardRecording()in your test'safterEachon success to avoid accumulating recordings - Sanitize test names for file paths when using dynamic output names
- Enable
showCursor: truefor debugging to see where clicks happen
FFmpeg
brew install ffmpeg. On Ubuntu/Debian: apt-get install ffmpeg. On Windows: download from ffmpeg.org and add to PATH.