Learning Path: RookieNerd Academy → Playwright with Java
Module: 10 – File Upload and File Download
Difficulty: Beginner to Intermediate
Estimated Reading Time: 25 Minutes
SEO Title
Playwright File Upload and File Download in Java – Complete Guide (2026)
Meta Description
Learn how to upload and download files in Playwright Java with practical examples. Master single file upload, multiple file uploads, download handling, verification, and best practices.
URL Slug
/academy/playwright/playwright-file-upload-download-java
Primary Keyword
Playwright File Upload Java
Secondary Keywords
Playwright File Download Java
Playwright Upload File
Playwright Download File
Playwright Java Tutorial
Playwright Automation Examples
By the end of this tutorial, you will be able to:
Upload a single file using Playwright.
Upload multiple files.
Handle file downloads.
Save downloaded files to a custom location.
Verify downloaded file names.
Apply enterprise best practices for file operations.
Uploading and downloading files are common user actions in web applications.
Examples include:
Uploading a profile picture.
Importing employee records from Excel.
Uploading invoices in PDF format.
Downloading reports.
Exporting customer data to CSV.
Downloading payment receipts.
A reliable automation framework should validate these workflows because they are often critical business functions.
Playwright provides built-in APIs for handling uploads and downloads without relying on operating system dialogs, making the automation simpler and more reliable.
Most websites use an HTML input element similar to:
<input type="file" id="resume">
Instead of opening the operating system's file chooser, Playwright directly assigns the file to the input element.
page.locator("#resume")
.setInputFiles(Paths.get("C:/TestData/resume.pdf"));
Playwright automatically uploads the specified file.
Some applications allow multiple attachments.
Example:
page.locator("#documents")
.setInputFiles(
Paths.get("C:/TestData/id.pdf"),
Paths.get("C:/TestData/address.pdf")
);
This uploads both files in a single operation.
To remove previously selected files:
page.locator("#documents")
.setInputFiles(new Path[0]);
This clears the file input.
Suppose the application provides an Export Report button.
Playwright can wait for the download event.
Download download = page.waitForDownload(() -> {
page.getByRole(
AriaRole.BUTTON,
new Page.GetByRoleOptions().setName("Export")
).click();
});
Once the download starts, Playwright returns a Download object.
Example:
Path target = Paths.get("C:/Downloads/report.xlsx");
download.saveAs(target);
This stores the downloaded file in the desired folder.
Sometimes the application generates dynamic file names.
Retrieve the suggested name:
System.out.println(download.suggestedFilename());
Example output:
EmployeeReport_July2026.xlsx
Example:
assert download.suggestedFilename().endsWith(".xlsx");
You can also verify:
File extension
File existence
File size
File contents (if required)
Imagine an online job portal.
Workflow:
Open the registration page.
Select Upload Resume.
Upload resume.pdf.
Submit the application.
Verify that a success message appears.
Example:
page.locator("#resume")
.setInputFiles(Paths.get("C:/TestData/resume.pdf"));
page.getByRole(
AriaRole.BUTTON,
new Page.GetByRoleOptions().setName("Submit")
).click();
assertThat(page.locator(".success-message"))
.hasText("Application submitted successfully");
Workflow:
Login.
Navigate to Reports.
Click Download Excel.
Save the file.
Verify the filename.
Download download = page.waitForDownload(() -> {
page.locator("#downloadExcel").click();
});
download.saveAs(Paths.get("C:/Reports/Employee.xlsx"));
Enterprise applications often require uploading:
Profile images
Excel spreadsheets
CSV files
PDF documents
ZIP archives
XML files
Using setInputFiles() keeps these scenarios straightforward.
Keep test files in a dedicated TestData folder.
Use descriptive file names.
Verify upload success with assertions.
Save downloads to a temporary folder during test execution.
Clean up downloaded files after the test if appropriate.
Avoid automating native file chooser dialogs with external tools when Playwright's APIs are sufficient.
Instead of embedding absolute paths, consider using project-relative paths to make tests portable across environments.
Uploading a file is only part of the test. Always verify the expected outcome, such as a confirmation message or successful download.
Create a Playwright script that:
Opens a sample upload page.
Uploads an image.
Verifies the success message.
Downloads a sample report.
Saves it to a local folder.
Prints the downloaded file name.
How do you upload a file in Playwright Java?
What is the purpose of setInputFiles()?
How do you upload multiple files?
How do you handle file downloads?
What does waitForDownload() do?
How do you save a downloaded file to a custom location?
How would you verify a successful file download?
Playwright typically bypasses the dialog by interacting directly with the file input element using setInputFiles().
Yes. Pass multiple file paths to setInputFiles().
Playwright doesn't rename the server-generated file automatically, but you can save it with a different name using saveAs().
Use waitForDownload(), save the file, and then verify its presence or filename.
In this lesson, you learned:
How to upload files.
How to upload multiple files.
How to clear uploaded files.
How to handle downloads.
How to save downloaded files.
Best practices for testing upload and download functionality.
File operations are common in enterprise systems such as HR portals, banking applications, insurance platforms, and e-commerce systems. Mastering these techniques will help you automate many real-world workflows with confidence.
Playwright Browser Context in Java
Playwright Multiple Tabs and Windows
Playwright Frames and iFrames
Playwright Auto Waiting
➡ Playwright Screenshots in Java – Capture Full Page, Element, and Failure Screenshots
File upload form before selecting a file.
Successful file upload confirmation.
Browser download prompt (or application download action).
Downloaded file in the destination folder.
Project structure showing the TestData directory.
Flow diagram: Upload → Verify → Download → Validate.