Part 05 · Operations & Advanced Card 36
File Uploads & Downloads
Card 36: File Uploads & Downloads
What This Pattern Solves
Forms upload files; pages generate downloads; users paste content. Testing these patterns requires simulating file selection, capturing download events, and reading clipboard.
How It Works
setInputFiles(): Set file(s) on<input type="file">with in-memory buffers.page.waitForEvent('download'): Capture a download triggered by a click.- Clipboard: Use
navigator.clipboard.readText()/writeText()for copy/paste.
Code Example
// Upload an in-memory file (no file on disk needed)
await page.getByTestId('file-input').setInputFiles({
name: 'test-document.txt',
mimeType: 'text/plain',
buffer: Buffer.from('Hello, Playwright file upload!'),
});
// Download: register waitForEvent before the click that triggers it
const [download] = await Promise.all([
page.waitForEvent('download'),
page.getByTestId('download-btn').click(),
]);
expect(download.suggestedFilename()).toBe('users.csv');
Run This Example
pnpm test src/36-file-uploads-downloads
Key Concepts
locator.setInputFiles(files): Accepts file paths or{ name, mimeType, buffer }.page.waitForEvent('download'): Must register before the trigger.Download.suggestedFilename(),.url(),.path(): Inspect the download.- Clipboard permissions: Requires
clipboard-read/clipboard-writegrant.
Common Mistakes
- Forgetting
Promise.allfor download capture. - Using real files instead of in-memory buffers.
- Not granting clipboard permissions for clipboard tests.
Related Patterns
- Previous: Card 35 (Multi-Tab & Multi-Context)
- Next: Card 37 (Global Setup & Teardown)
- Complementary: Card 31 (Network HAR)
Live Demo
👇 This component is what the Playwright test interacts with:
Loading…
Run This Example
pnpm test src/36-file-uploads-downloads