how to test file downloads across browsers
Explains how to test file downloads across browsers: intercept the download event, read the file, and assert on contents. Use when e2e tests cover CSV, PDF, or export downloads. Not for upload testing.
TL;DR
Do not assert on real files landing in a downloads folder; it is slow and platform-dependent. Instead, use the framework's download event (Playwright's download event, Cypress's download handling via intercepted responses or plugins), wait for the download to complete, read the file from its temp path, and assert on the contents. Test across Chromium, Firefox, and WebKit because filename encoding and content-disposition handling differ between browsers.
The query
how to test file downloads across browsersUse this when
- E2E tests cover export, report, or receipt downloads.
- Downloads behave differently per browser.
Not for
- File upload testing.
- Testing the download server itself; use API tests for that.
Steps
- Set up the framework's download interception and point downloads at a temp directory. Expected output: downloads are captured, not written to the real folder.
- Trigger the download in the test and wait for the download event to complete. Expected output: the test proceeds only when the file is fully written.
- Read the file and assert on its contents (parse the CSV, check PDF text, compare bytes). Expected output: content assertions, not just "a file appeared".
- Run the same test on Chromium, Firefox, and WebKit. Expected output: all three pass, or you have a documented browser difference.
- Clean up temp files in an after-hook. Expected output: no leftover files between runs.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_aWFjPG4zE1j3oavmX4QvbQ
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.