File Downloads and Uploads
Relevant source files
The following files were used as context for generating this wiki page:
Playwright Test provides a comprehensive system for managing test artifacts, which includes automatically generated files like screenshots, videos, and traces, as well as user-defined attachments. This system is crucial for debugging test failures, providing rich context in test reports, and handling file-based interactions within tests. Artifacts are generated based on the test configuration and can be preserved conditionally, for example, only for failed tests.
The reporting system, particularly the HTML and Blob reporters, is designed to handle these artifacts efficiently, even in distributed or sharded test environments. The Blob reporter consolidates results and artifacts from multiple shards into portable .zip archives, which can then be merged into a single, comprehensive HTML report. This report provides an interactive way to explore test results, including viewing traces, comparing screenshots, and downloading attached files.
Artifact Generation
Playwright can automatically generate several types of artifacts during a test run. The generation is controlled by options in the playwright.config.ts file.
- Screenshots: Captured automatically based on the
screenshotoption. - Videos: Recorded based on the
videooption. - Traces: Recorded based on the
traceoption, allowing for post-mortem debugging of test runs.
These artifacts are stored in the test output directory, organized by test case.
Sources: tests/playwright-test/playwright.artifacts.spec.ts, packages/playwright/types/test.d.ts:1333-1375
Configuration Options
The generation of artifacts is primarily configured within the use block of the playwright.config.ts file.
| Option | Type | Description |
|---|---|---|
screenshot | 'off' | 'on' | 'only-on-failure' | 'on-first-failure' | Controls when to take a screenshot. |
video | 'off' | 'on' | 'retain-on-failure' | 'on-first-retry' | Controls when to record a video of the test run. |
trace | 'off' | 'on' | 'retain-on-failure' | 'on-first-retry' | 'on-all-retries' | Controls when to record a Playwright Trace. |
outputDir | string | The directory where test results and artifacts are stored. |
preserveOutput | 'always' | 'never' | 'failures-only' | Whether to preserve the output directory for test runs. |
Sources: packages/playwright/types/test.d.ts:1333-1375, packages/playwright/types/test.d.ts:983-999
The following diagram illustrates the decision flow for generating a trace artifact based on the trace configuration.
This diagram shows how the trace option in the configuration determines whether a trace file is recorded and retained.
Sources: tests/playwright-test/playwright.artifacts.spec.ts:188-216, packages/playwright/types/test.d.ts:1351-1361
Custom Attachments
In addition to automatic artifact generation, users can attach custom files or buffers to the test report using testInfo.attach(). This is useful for adding logs, custom screenshots, or any other data that provides context for the test run.
testInfo.attach()
This method allows attaching a file or buffer to the current test. It takes a name for the attachment and an options object with either a path to a file or a body containing the content as a string or Buffer.
Key attach Options:
| Option | Type | Description |
|---|---|---|
name | string | The name of the attachment. |
body | string | Buffer | The content of the attachment. Mutually exclusive with path. |
path | string | Path to the file to be attached. Mutually exclusive with body. |
contentType | string | The MIME type of the attachment, e.g., image/png or application/json. |
Sources: packages/playwright/types/test.d.ts:1171-1198, tests/playwright-test/reporter-html.spec.ts:681-710
testInfo.outputPath()
To avoid file path conflicts when running tests in parallel, testInfo.outputPath() provides a safe way to get a unique file path within the test's output directory.
// tests/playwright-test/reporter-blob.spec.ts:474-484
test('first', async ({}) => {
const attachmentPath = test.info().outputPath('foo.txt');
fs.writeFileSync(attachmentPath, 'hello!');
await test.info().attach('file-attachment', {path: attachmentPath});
console.log('console info');
console.error('console error');
});
This snippet shows how to create a file in the test's output directory and then attach it to the report.
Sources: packages/playwright/types/test.d.ts:1233-1248, tests/playwright-test/reporter-blob.spec.ts:474-484
Snapshot Testing
Snapshot testing is a specialized form of artifact management where an expected state (text or image) is stored as a "golden" file. During a test run, the actual value is compared against the snapshot.
- If a snapshot does not exist, it's created.
- If the actual value does not match the snapshot, the test fails, and "actual", "expected", and "diff" artifacts are generated in the test output directory.
- The
--update-snapshotsflag can be used to update existing snapshots with the new actual values.
The following diagram shows the logic of toMatchSnapshot.
This diagram outlines the process of snapshot comparison, creation, and update.
Sources: tests/playwright-test/golden.spec.ts, packages/playwright/types/test.d.ts:2412-2479
Reporting
Playwright's reporters are responsible for processing and presenting test results and their associated artifacts.
HTML Reporter
The HTML reporter provides a rich, interactive UI for exploring test results. It displays artifacts inline and allows for easy downloading.
- Trace Viewer: Traces can be viewed directly within the report.
- Image Diffs: For failed screenshot assertions, the report shows "expected", "actual", and "diff" images side-by-side.
- Attachments: Custom attachments are listed and can be viewed or downloaded. The
attachmentsBaseURLoption can be used to serve attachments from a custom URL.
The following sequence diagram illustrates how a user interacts with attachments in an HTML report.
This diagram shows the user clicking a link in the HTML report, which triggers a download of the corresponding artifact file.
Sources: tests/playwright-test/reporter-html.spec.ts:391-413, tests/playwright-test/reporter-html.spec.ts:681-710, packages/playwright/types/test.d.ts:16-19
Blob Reporter
The Blob reporter is designed for distributed testing environments. It outputs test results and artifacts into a .zip file. When tests are sharded, each shard produces its own blob report. These individual blobs can then be collected and merged into a single report using the playwright merge-reports command.
This mechanism ensures that all artifacts, including screenshots, traces, and custom attachments from all shards, are consolidated correctly. The merge process is capable of handling reports from different root directories by mapping file paths to a new common root directory specified in the merge configuration.
The flow for sharded testing with the blob reporter is as follows:
This diagram illustrates how multiple blob reports from different shards are combined into one final HTML report.
Sources: tests/playwright-test/reporter-blob.spec.ts:100-120, tests/playwright-test/reporter-blob.spec.ts:1022-1052, packages/playwright/types/test.d.ts:14-15
File Downloads
Playwright also handles file downloads initiated by the web page under test. When a navigation or user action triggers a file download, the page emits a download event.
The Download object provides methods to interact with the downloaded file:
url(): The URL the file was downloaded from.suggestedFilename(): The filename suggested by theContent-Dispositionheader.path(): The path to the temporary file where the download is stored. This method throws an error if downloads are not enabled.saveAs(path): Saves the download to a specified path.delete(): Deletes the downloaded file.cancel(): Cancels an in-progress download.failure(): Returnsnullfor a successful download, or an error message if the download failed.createReadStream(): Returns a readable stream for the download.
To enable downloads, the acceptDownloads option must be set to true when creating a new page or context. If false, a download event is still emitted, but attempting to access the file path will result in an error.
Sources: tests/library/download.spec.ts