Uploading files

setInputFiles(): no dialog involved

Playwright puts files straight into a file input. The file can be on disk, or made in the test from a buffer:

await page.getByLabel('Choose file').setInputFiles('fixtures/report.pdf');

await page.getByLabel('Profile photo').setInputFiles({
  name: 'avatar.png',
  mimeType: 'image/png',
  buffer: Buffer.alloc(2 * 1024 * 1024),   // 2 MB of zeros: too large
});
accept="image/png" only filters what the user sees in the operating system's dialog. setInputFiles() ignores it, as can any user. The page has to check the file itself, so test that it does.

A button instead of an input

Many sites hide the input behind a styled button. Either find the hidden input with a CSS locator, or click the button and catch the file chooser:

const chooserPromise = page.waitForEvent('filechooser');
await page.getByRole('button', { name: 'Add files' }).click();
const chooser = await chooserPromise;
await chooser.setFiles([fileA, fileB]);

Dropping files

A drop zone reads files from a drop event. Build a DataTransfer in the page and dispatch the event with it:

const dataTransfer = await page.evaluateHandle(() => {
  const transfer = new DataTransfer();
  transfer.items.add(new File(['hello'], 'hello.txt', { type: 'text/plain' }));
  return transfer;
});
await page.getByText('Drop files here').dispatchEvent('drop', { dataTransfer });

Check what reached the server

Wait for the upload's response and read what the server reports, or inspect the request it sent.

Upload a File

Profile photo

PNG or JPEG, up to 1 MB.

Attachments

Up to 5 files, 2 MB in total.

Drop files here