Handle file downloads and uploads in Playwright tests

domain: playwright.dev · 5 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. To capture a download, wrap the action that triggers it in `page.waitForEvent('download')`: `const [download] = await Promise.all([page.waitForEvent('download'), page.getByText('Export').click()])` — this resolves to a `Download` object.
  2. Save the file with `await download.saveAs('/tmp/export.csv')` or read its stream with `download.createReadStream()` for in-memory assertions.
  3. For file uploads with an `<input type='file'>` element, obtain the locator and call `await fileInput.setInputFiles('/path/to/file.pdf')` — Playwright handles the file-chooser dialog internally.
  4. For drag-and-drop upload zones or cases where clicking a button opens a system file chooser, use `page.waitForEvent('filechooser')` before the click, then call `fileChooser.setFiles([...paths])` on the resolved chooser object.
  5. Assert upload success by waiting for a confirmation element, e.g., `await expect(page.getByText('Upload complete')).toBeVisible()`.

Known gotchas

Related routes

Capture and analyze network requests during a Playwright test using request and response events
playwright.dev · 5 steps · unrated
Automate file upload via a hidden file input element in Playwright
playwright.dev · 5 steps · unrated
Intercept, inspect, and mock network requests in Playwright tests
playwright.dev · 5 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans