Chapter 46: Upload and download files
Automation learning lab
Playwright Automation
Self Study Guide
Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.
Upload and download files
setInputFiles selects files for a file input without using a native OS dialog. A download event provides a Download object whose saveAs method persists the file. Always register the download wait before the action and use non-sensitive test fixtures.
How it works
Step-by-step walkthrough
- Create a file input
- Upload an in-memory text file
- Verify the selected filename
Example
import { test, expect } from '@playwright/test';
test('chapter 46 practice', async ({ page }) => {
await page.setContent('<label>Upload <input type="file"></label>');
await page.getByLabel('Upload').setInputFiles({
name: 'sample.txt', mimeType: 'text/plain', buffer: Buffer.from('Hello')
});
expect(await page.getByLabel('Upload').evaluate((el: HTMLInputElement) => el.files?.[0]?.name)).toBe('sample.txt');
});Use temporary output directories and do not upload personal files.
✎ Your practice task
For a controlled download link, use const pending = page.waitForEvent('download') before clicking, then await (await pending).saveAs('sample.txt').
Knowledge check
Which method selects a file for an input?
🌐 Continue learning
60-chapter learning path
Start at chapter 1, or choose any topic. Your progress is tracked separately for each chapter.
JavaScript fundamentals
TypeScript fundamentals
Playwright • setup & locators
Playwright • interactions & assertions
Playwright • framework & debugging
Playwright • real-world automation
Interactive practice lab
Try the controls below and observe the corresponding Playwright locator instructions. This browser demo does not execute Node.js or Playwright Test; copy the code to your local Playwright project to automate it.
Task manager demo
Suggested automation
Actions on the demo update these examples. In your local test use a page that contains the same controls.
Download a standalone lab page, then serve it locally (for example with Python's http.server). Navigate to it in a Playwright test.