Testing / Playwright Interview questions
How do you handle file uploads in Playwright?
Playwright interacts with the native file input directly, without needing to drive the operating system's file picker dialog.
await page.getByLabel('Upload resume').setInputFiles('resume.pdf'); // Multiple files await page.getByLabel('Attachments').setInputFiles(['a.png', 'b.png']); // Clear a selection await page.getByLabel('Upload resume').setInputFiles([]);
setInputFiles() works even if the actual <input type="file"> element is visually hidden and a custom-styled button triggers it, since Playwright sets the files on the underlying DOM element rather than simulating clicks through a real OS dialog. For uploads triggered without any visible input element, page.waitForEvent('filechooser') can capture the file chooser event and set files on it directly.
For drag-and-drop upload widgets that don't use a standard file input at all, Playwright's dragTo() or manual dispatchEvent() calls simulating drop events are the usual fallback, since there's no native input element to target directly in that case.
More Related questions...