File upload and download functionality is common in modern web applications. Examples include:
- Uploading profile pictures
- Importing CSV files
- Downloading invoices
- Exporting reports
- Uploading documents and attachments
As automation engineers, we must verify these workflows efficiently and reliably.
In this article, we'll learn how to handle file uploads and downloads in Playwright using Java.
Why Playwright Makes File Handling Easy
Traditional automation frameworks often require external tools or complex workarounds for handling native file dialogs.
Playwright provides built-in APIs that make file upload and download automation straightforward and reliable.
Uploading a Single File
Suppose an application contains the following HTML element:
<input type="file" id="uploadFile">Uploading a file is simple:
page.setInputFiles(
"#uploadFile",
Paths.get("src/test/resources/sample.pdf")
);Playwright automatically handles the file selection dialog behind the scenes.
Complete Example for File Upload
page.navigate("https://example.com/upload");
page.setInputFiles(
"#uploadFile",
Paths.get("src/test/resources/resume.pdf")
);
page.locator("#submitButton").click();This uploads the file and submits the form.
Uploading Multiple Files
Many applications allow users to upload multiple files simultaneously.
Example:
page.setInputFiles(
"#documents",
new Path[] {
Paths.get("invoice.pdf"),
Paths.get("contract.pdf"),
Paths.get("report.xlsx")
}
);This uploads all three files in a single operation.
Removing Uploaded Files
Sometimes tests need to verify the remove or clear functionality.
Example:
page.setInputFiles(
"#uploadFile",
new Path[] {}
);This clears the selected files.
Waiting for File Downloads
Downloads require a slightly different approach.
Playwright provides the waitForDownload() method.
Example:
Download download =
page.waitForDownload(() -> {
page.locator("#downloadReport").click();
});Playwright waits until the file download begins and returns a download object.
Getting the Downloaded File Name
Example:
System.out.println(
download.suggestedFilename()
);Output:
SalesReport_2026.xlsxSaving Downloaded Files
By default, Playwright stores downloads in a temporary location.
To save them permanently:
download.saveAs(
Paths.get(
"downloads/" +
download.suggestedFilename()
)
);This saves the file inside the downloads folder.
Complete Download Example
Download download =
page.waitForDownload(() -> {
page.locator("#downloadInvoice").click();
});
download.saveAs(
Paths.get(
"downloads/invoice.pdf"
)
);Recommended Folder Structure
A clean project structure helps maintain large automation frameworks.
Example:
project
├── downloads
│ ├── invoice.pdf
│ └── report.xlsx
│
├── uploads
│ ├── sample.pdf
│ └── employee.csv
│
└── srcKeeping test files organized improves maintainability.
Verifying Download Success
You can verify whether the file exists after downloading.
Example:
Path filePath =
Paths.get(
"downloads/invoice.pdf"
);
assertTrue(
Files.exists(filePath)
);This confirms that the download completed successfully.
Common File Upload Issues
File Not Found Exception
Cause:
- Incorrect file path
- Missing test resource
Solution:
Paths.get(
"src/test/resources/sample.pdf"
);Use relative paths whenever possible.
Download Does Not Start
Possible reasons:
- Click event not triggered
- Browser blocked the download
- Incorrect locator
Always wrap the click action inside waitForDownload().
Best Practices
Store Test Files in Resources Folder
Recommended location:
src/test/resourcesThis ensures files are available across different environments.
Use Meaningful File Names
Good examples:
employee_data.csv
invoice_2026.pdf
profile_picture.pngAvoid:
test1.pdf
abc.csv
file.pngClean Download Folder Regularly
Old files can cause false positives during validation.
Delete old downloads before executing tests.
Validate Business Outcomes
Instead of only checking that a file exists, verify:
- File name
- File type
- File size
- File contents
This provides stronger test coverage.
Interview Questions
How do you upload a file in Playwright?
Use:
page.setInputFiles();How do you download files in Playwright?
Use:
page.waitForDownload();Can Playwright upload multiple files?
Yes.
Pass multiple file paths inside a Path[] array.
Where are downloads stored by default?
Playwright stores downloads in a temporary folder unless you explicitly save them using saveAs().
Conclusion
File upload and download functionality is a critical part of enterprise automation projects.
Playwright simplifies these workflows with built-in APIs that are easy to use and highly reliable.
In this article, you learned:
- How to upload a single file
- How to upload multiple files
- How to remove uploaded files
- How to handle downloads
- How to save downloaded files
- Best practices for production frameworks
Mastering these techniques will help you automate document management systems, financial applications, HR portals, and many other enterprise applications with confidence.
In the next article, we will learn how to handle Frames and iFrames in Playwright using Java.
No comments:
Post a Comment