Skip to main content
Free plan, no cardStart free

EngineeringHow-to guide

How to Test File Uploads with Playwright: setInputFiles, the File Chooser and Proof the Server Got the Bytes

A test that selects a file has not tested an upload. The upload is tested when the server says what it received and the test checks every byte. Here is the code, run in three engines, with the timings.

Summarise with AI

Opens a new chat with only this request: Summarise the key points of this article: https://dokimore.com/blog/how-to-test-file-uploads-with-playwright

9 of 9Uploads whose server-side SHA-256 matched the bytes the test sent (3 cases × 3 engines)
63–79 msA 1 MiB upload, click to server reply, in Chromium, Firefox and WebKit
413What a 25 MiB file against a 2 MiB limit returned, in 116–438 ms
0Files left selected after setInputFiles([]) in every engine

In short

  • Upload with locator.setInputFiles(): a path, an array of paths, or an object with name, mimeType and buffer for a file built in the test. An empty array clears the selection. For an input the page creates on demand, wait for the filechooser event before clicking the button that opens it.
  • Selecting the file is the easy half. Assert on what the server did: in our run the fixture server answered with the name, the size and the SHA-256 of every file it received, and the test compared that hash with its own. All nine uploads across three engines matched.
  • Test the failure path with the same care: a 25 MiB file against a server limit of 2 MiB came back 413 in all three engines, and the page showed the server’s message. A test that only proves the happy path has not tested the limit the product promises.

About this article

Format
How-to guide: a procedure to follow, step by step.
Written for
SDETs and automation engineers · Developers who own their tests · QA leads and test managers
Where it applies
Every market. The rules that differ by country are on the country pages.

File upload is one of the few things a browser test cannot do by clicking: the native file dialog is outside the page, so the test has to hand the browser the file directly. Playwright does that with setInputFiles on the file input, and it works whether the input is visible, hidden behind a styled button, or created when a button is pressed.

This guide was run before it was written. A small fixture page with three file inputs and a fixture server that hashes what it receives were driven by Playwright 1.62.1, the version this platform pins, in Chromium 151, Firefox 153 and WebKit 26.5. The timings and the server replies below are from that run; the scripts and the raw output are linked at the end.

The four ways to give Playwright a file

  • A path on disk: `await page.setInputFiles('#file', 'fixtures/photo.bin')`. The simplest case, and the one most suites use.
  • Several paths, for an input with the multiple attribute: `await page.setInputFiles('#files', ['small.txt', 'photo.bin'])`. Both files arrived in one request in every engine, 1,024 and 1,048,576 bytes, hashes matching.
  • Bytes built in the test, with no file on disk: `{ name: 'report.csv', mimeType: 'text/csv', buffer: Buffer.from('id,name\n1,alpha\n') }`. Useful when the content matters more than the file: a CSV with a bad row, a JSON with a missing field, a file whose name has unicode in it.
  • The file chooser event, for a page whose input is hidden or created on demand: start waiting for the chooser, click the custom button, then call setFiles on the chooser. The chooser also reports isMultiple, which our fixture answered false for the single input.

The test, as it ran

The fixture page posts the selected files with fetch and prints the server’s JSON reply into an element the test can read. The server parses the multipart body and answers with each file’s field name, file name, size and SHA-256. That is the whole trick: the assertion is on the server’s account of the bytes, not on the input element’s files property, which only proves the browser selected something.

upload.spec.ts — a file built in the test, checked by hash on the server
import { test, expect } from '@playwright/test';
import { createHash } from 'node:crypto';

test('uploads a CSV built in the test and the server keeps every byte', async ({ page }) => {
  await page.goto('/upload');
  const csv = Buffer.from('id,name\n1,alpha\n2,beta\n');

  // No file on disk: name, MIME type and bytes come from the test.
  await page.setInputFiles('#file', { name: 'report.csv', mimeType: 'text/csv', buffer: csv });
  await page.getByRole('button', { name: 'Upload' }).click();

  // The page prints the server's reply: { files: [{ name, size, sha256 }] }.
  const result = page.locator('#result');
  await expect(result).toContainText('"sha256"');
  const reply = JSON.parse((await result.textContent()) ?? '{}');

  expect(reply.files).toHaveLength(1);
  expect(reply.files[0].name).toBe('report.csv');
  expect(reply.files[0].size).toBe(csv.length);
  expect(reply.files[0].sha256).toBe(createHash('sha256').update(csv).digest('hex'));
});

A custom button and a hidden input

Most applications hide the real input and show a styled button. The input still exists, so setInputFiles on it works even when it is display:none; Playwright does not need the element to be visible for this action. When the input is created only after a click, or when you want to exercise the button itself, use the file chooser event. The wait must be armed before the click, which is why the two sit in one Promise.all.

upload.spec.ts — the button that opens the chooser
test('the custom "Choose" button opens a chooser the test can answer', async ({ page }) => {
  await page.goto('/upload');
  const [chooser] = await Promise.all([
    page.waitForEvent('filechooser'),            // armed first
    page.getByRole('button', { name: 'Choose with a custom button' }).click(),
  ]);
  expect(chooser.isMultiple()).toBe(false);
  await chooser.setFiles('fixtures/small.txt');
  await page.getByRole('button', { name: 'Upload' }).click();
  await expect(page.locator('#result')).toContainText('"name":"small.txt"');
});

The failure path: a file over the limit

A size limit is a promise the product makes, and a test should prove it is kept. The fixture server rejects any file over 2 MiB with 413 and a JSON error. Sending a 25 MiB file returned 413 in 185 ms in Chromium, 116 ms in Firefox and 438 ms in WebKit, and the page displayed the server’s message. Note what the browser did not do: it did not refuse to attach a large file, and it did not time out. Limits enforced only in the UI are not limits; the test has to reach the server.

upload.spec.ts — the limit is the server’s, and the UI shows it
test('a file over the limit is refused by the server and the page says why', async ({ page }) => {
  await page.goto('/upload');
  await page.setInputFiles('#file', 'fixtures/big-25MiB.bin');
  const [response] = await Promise.all([
    page.waitForResponse((r) => r.url().endsWith('/upload')),
    page.getByRole('button', { name: 'Upload' }).click(),
  ]);
  expect(response.status()).toBe(413);
  await expect(page.locator('#result')).toContainText('file too large');
});

In the product

What the three engines did

The engines agreed on every outcome. The timings are on a local fixture server, so they measure the browser and Playwright, not a network; a real upload adds the network and the server’s own processing. The 1 MiB case is there to show the baseline: well under 100 ms end to end in all three.

Measured on 2026-11-04, Playwright 1.62.1, one run per engine; milliseconds are click-to-reply as timed by the test
CaseChromium 151Firefox 153WebKit 26.5
CSV built in the test (23 bytes), hash matchesyesyesyes
Two real files, 1 KiB + 1 MiB, both hashes matchyes, 64 msyes, 78 msyes, 96 ms
File chooser event, isMultiplefalse, hash matchesfalse, hash matchesfalse, hash matches
Files selected after setInputFiles([])000
25 MiB against a 2 MiB limit413 in 185 ms413 in 116 ms413 in 438 ms
1 MiB under the limit, click to reply72 ms79 ms63 ms

What to assert, and what not to

  • Assert on the server’s account of the file: name, size, a hash or a row count. The input’s files property proves selection, not delivery.
  • Assert the limit from the server’s status, not only from a message in the UI; the message can exist without the limit.
  • Build edge-case files in the test with a buffer: a zero-byte file, a name with spaces and unicode, the wrong MIME type for the extension, a CSV with a header only.
  • Clear between cases with setInputFiles([]) so a second test on the same page does not inherit the first selection.
  • Do not test the native dialog. It is the operating system’s, Playwright bypasses it by design, and a test that tries to click it will wait forever.

How Dokimore records an upload

A recorded flow captures the file chosen during recording and replays the upload as a step, with the network call it caused, so the replay can assert on the server’s response the same way this guide does. The File Upload node in the workflow canvas takes a file from the Test Data Factory or from a fixed path and posts it as part of a scheduled run. A limit in your application is tested by giving the node a file over it and asserting on the status, exactly as above.

What to do next

  1. Make your upload endpoint answer with what it received (name, size, hash or row count) in a test environment; it turns every upload test from "the dialog closed" into "the bytes arrived".
  2. Replace file fixtures on disk with buffers built in the test for the edge cases: zero bytes, unicode names, wrong MIME types, a header-only CSV.
  3. Add one test per limit the product promises (size, count, type) and assert the server’s status, not only the message in the UI.
  4. Run the suite in all three engines at least once; the behaviour agreed here, but your application’s drag-and-drop or chunked uploader may not.
  5. Copy the scripts linked under "How we measured this" to reproduce the run against your own upload page.

Questions people ask

How do I upload a file in Playwright without a file on disk?

Pass an object to setInputFiles with name, mimeType and buffer: `page.setInputFiles('#file', { name: 'report.csv', mimeType: 'text/csv', buffer: Buffer.from('…') })`. In our run a 23-byte CSV built this way arrived on the server with a matching SHA-256 in Chromium, Firefox and WebKit.

How do I test a hidden file input or a custom upload button?

setInputFiles works on a hidden input; Playwright does not require it to be visible for this action. To exercise the button itself, wait for the filechooser event and click the button inside one Promise.all, then call setFiles on the chooser. The chooser reports isMultiple so the test can check the input accepts what the UI claims.

How do I verify the upload actually worked?

Make the server say what it received and assert on that: the file name, the size, and a hash or a row count, compared with what the test sent. The input’s files property only proves the browser selected the file. In our fixture the server answered with the SHA-256 of each file and all nine uploads matched.

How do I test a file size limit with Playwright?

Send a file over the limit and assert the server’s status with page.waitForResponse, then check the UI shows the message. A 25 MiB file against a 2 MiB limit returned 413 in all three engines in 116 to 438 ms. A limit enforced only in the UI is not a limit.

How we measured this

  • Run on 2026-11-04 with Playwright 1.62.1 (the version this platform pins) and the browser builds it requires, Chromium 151.0.7922.34, Firefox 153.0 and WebKit 26.5, inside the image that carries those builds and their system libraries, with networking disabled; the fixture page and server ran on 127.0.0.1. Scripts and raw output: docs/marketing/keyword-research/research/playwright-guides-2026-11-04 (e1-upload.js, out/e1-upload.json) in the product repository.
  • The measurements were made with the Playwright library API; the snippets in this guide wrap the same calls in Playwright Test’s test and expect so they can be pasted into a suite. Milliseconds are Date.now() around the click and the reply as seen by the test, one run per case per engine, so they are indicative, not a benchmark.
  • Dokimore’s behaviour is the product code on the article date: the recorder replays an upload step with its network call, and the workflow canvas’s File Upload node posts a file from the Test Data Factory or a fixed path.

Sources

  1. Playwright documentation, Actions: upload files, setInputFiles with paths, buffers and an empty array, the filechooser event read
  2. Playwright documentation, Browsers: each release pins its own browser builds (1.62.1: Chromium 151, Firefox 153, WebKit 26.5 in our run) read
  3. Playwright release notes: 1.64 is the newest release on the date this guide was run (Chromium 156, Firefox 157, WebKit 27.2); the platform pins 1.62.1 read
  4. MDN, HTMLInputElement.showPicker(): the native file dialog is opened by the browser, which is why a test hands the file to the input instead read

See it on your own project

Every new account starts a 14-day Solo Pro trial, then 14 days on the Free plan. No card. Record one flow and measure the difference on your own numbers.

  • Free plan, no card
  • 14-day Solo Pro trial
  • Demo replies within one business day
  • Data hosted in India