Playwright + Java · Lesson 24 of 38

0/38 done

Module 24: File Handling

24. Automated File Uploads & Download Verifications

15 minlesson

Your task

Implement uploadDocument:

  1. Accept Page page, String selector, String filePath.
  2. Call page.locator(selector).setInputFiles(Paths.get(filePath)).

Write it in the editor, then press Check answer (Ctrl + Enter).

File Upload & Download

java
import java.nio.file.Paths;

page.locator("input[type='file']").setInputFiles(Paths.get("test-data/invoice.pdf"));

Download download = page.waitForDownload(() -> {
    page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Export CSV")).click();
});
download.saveAs(Paths.get("downloads/" + download.suggestedFilename()));

Key Takeaways & Walkthrough Summary

Uploading profile pictures and downloading monthly statement PDFs are essential enterprise workflows. In this lesson, you will master setInputFiles and Playwright's Download event listener to inspect downloaded files. Let's automate files!

Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Can Playwright upload multiple files at once?

Yes, pass an array of Paths: setInputFiles(new Path[]{ path1, path2 }).

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip