Implementation:Microsoft Playwright Server Artifact
| Knowledge Sources | |
|---|---|
| Domains | Server, FileSystem |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for managing downloadable artifacts such as screenshots, videos, and traces on the server side provided by the Playwright library.
Description
The `Artifact` class extends `SdkObject` and represents a server-side file artifact (download, video, trace, etc.) with lifecycle management. It tracks a local file path, supports asynchronous completion via a `ManualPromise`, and provides `saveAs` and `cancel` operations with callback support. The artifact can be in an unaccessible state (e.g., when remote), finished or unfinished, and deleted. Multiple `saveAs` callbacks can be registered, and the artifact handles failure errors, deletion of temporary files, and coordination of save operations after the artifact finishes writing.
Usage
Use Server Artifact when managing file-based outputs from browser operations, including downloads, recorded videos, HAR files, and trace archives.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/server/artifact.ts
Signature
export class Artifact extends SdkObject {
constructor(parent: SdkObject, localPath: string, unaccessibleErrorMessage?: string, cancelCallback?: CancelCallback);
finishedPromise(): ManualPromise<void>;
localPath(): string;
async localPathAfterFinished(): Promise<string>;
async saveAs(saveCallback: SaveCallback): Promise<void>;
async failureError(): Promise<Error | undefined>;
async cancel(): Promise<void>;
async delete(): Promise<void>;
async deleteOnContextClose(): Promise<void>;
}
Import
import { Artifact } from '../server/artifact';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| parent | SdkObject | Yes | Parent SDK object for instrumentation hierarchy |
| localPath | string | Yes | Path to the artifact file on disk |
| unaccessibleErrorMessage | string | No | Error message if the artifact cannot be accessed directly |
| cancelCallback | CancelCallback | No | Async callback invoked when the artifact is cancelled |
Outputs
| Name | Type | Description |
|---|---|---|
| localPath | string | The file system path to the artifact |
| finishedPromise | ManualPromise<void> | Promise that resolves when the artifact has finished writing |
Usage Examples
import { Artifact } from '../server/artifact';
const artifact = new Artifact(parentSdkObject, '/tmp/video.webm', undefined, async () => {
// cancel recording logic
});
// Wait for the artifact to finish and retrieve the path
const path = await artifact.localPathAfterFinished();
// Save the artifact to a user-specified location
await artifact.saveAs(async (localPath) => {
await fs.promises.copyFile(localPath, '/user/downloads/video.webm');
});