TestingDocs.com logoTestingDocs.com
Automation learning lab
TestingDocs • Self-paced course

Playwright Automation
Self Study Guide

Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.

60 chaptersBeginner friendlyHands-on practice
Playwright / Chapter 46 of 60~18 min • Beginner
Chapter 46Playwright

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

  1. Create a file input
  2. Upload an in-memory text file
  3. Verify the selected filename

Example

TypeScript
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');
});
Expected result: sample.txt selected
💡 Point to remember
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