Playwright · Reliable end-to-end tests

How do you test file downloads?

EasydownloadsfilesartifactsUpdated Oct 2026
Quiz this track

Short answer

Wait for the download event, then save or inspect the file with saveAs() or path(). Downloads are accepted by default in Playwright contexts.

A download is not a navigation, so a click alone does not give you the file. Start waiting for the download event before the action that triggers it, then use the Download object to save or read the file.

ts
const downloadPromise = page.waitForEvent("download");
await page.getByRole("button", { name: "Export CSV" }).click();

const download = await downloadPromise;
await download.saveAs("downloads/export.csv");

expect(download.suggestedFilename()).toContain(".csv");
saveAs writes a stable copy; the temporary file may be removed when the context closes.

What you can do with a download

  • download.saveAs(path) copies the file to a location you control
  • download.path() returns the temporary file path for reading
  • download.suggestedFilename() returns the server's suggested name
  • download.failure() returns the failure reason, or null on success
  • Set acceptDownloads: false in the context if a test should reject downloads

If the test needs to verify content, read the saved file with Node's file APIs and parse it. That turns a download test from checking that a click happened into checking that the exported data is correct.

ts
import { readFile } from "node:fs/promises";

const downloadPromise = page.waitForEvent("download");
await page.getByRole("button", { name: "Export JSON" }).click();
const download = await downloadPromise;

const path = await download.path();
const raw = await readFile(path, "utf8");
const data = JSON.parse(raw);

expect(data.orders).toHaveLength(3);
Reading the downloaded file lets the test assert on content, not just the event.
Artifacts written during a test should go to a temporary or test-results directory. Do not commit generated downloads, and clean them up if the suite runs repeatedly on the same machine.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1How do you read the contents of a downloaded file?
  2. 2What is the difference between path and saveAs?
  3. 3How do you reject downloads in a context?
  4. 4Why should download artifacts not be committed?

Say it in the interview

I set up a wait for the download event before clicking the export button, then save the file with saveAs or read the temporary path. If the content matters, I parse the saved file and assert on the exported data, not just the filename. The temporary file can disappear when the context closes, so I copy it somewhere stable first. I keep generated downloads out of version control.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Playwright mock interview and get scored feedback on depth, clarity and confidence.