Playwright with Java Day 22: Upload and Download Files Without Flaky Tests (setInputFiles + waitForDownload)
Where Day 21 left off
- Day 21 handled frames, alerts and new tabs on the-internet.herokuapp.com.
- Many real apps also take files in and hand files out.
- Think of a profile photo, a bank statement or an invoice PDF.
- Day 22 tests both directions on the same practice site.
How the practice pages work
- Real-life example:
/uploadhas a file input and an Upload button. - After upload, it shows "File Uploaded!" and the file name.
/downloadlists every file people have uploaded to the site.- That list changes all day, because other learners upload files too.
- So the test uploads its own file with a unique name, then downloads that exact file.
- A full round trip, with nothing left to chance.
- Real apps work the same way: you rarely control what else sits in shared storage.
Step 1: Upload a file
- Create
src/test/java/tests/FileTransferTest.java. - Disclaimer: match the package and BaseFixture import to your project.
package tests;
import com.microsoft.playwright.Download;
import com.microsoft.playwright.options.FilePayload;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
public class FileTransferTest extends BaseFixture {
private static final String SITE = "https://the-internet.herokuapp.com";
private static final String CONTENT = "Hello from Kathmandu";
private String uploadSample() {
String fileName = "twl-" + System.currentTimeMillis() + ".txt";
page.navigate(SITE + "/upload");
page.locator("#file-upload").setInputFiles(new FilePayload(
fileName, "text/plain", CONTENT.getBytes(StandardCharsets.UTF_8)));
page.locator("#file-submit").click();
assertThat(page.locator("h3")).hasText("File Uploaded!");
assertThat(page.locator("#uploaded-files")).hasText(fileName);
return fileName;
}
@Test
public void uploadsAFile() {
uploadSample();
}
}
setInputFilesfills the file input directly. No Windows file dialog ever opens.FilePayloadbuilds the file in memory, so the repo needs no sample file.- For a real file on disk, pass a Path:
setInputFiles(Paths.get("src/test/resources/files/photo.png")). - The time in the name keeps your file apart from everyone else's.
- The imports cover both steps, so nothing changes when you add Step 2.
- The upload assertion checks the server's answer, not only that the click happened.
Step 2: Download the same file
- Add this method to the same class.
@Test
public void downloadsTheFileItUploaded() throws IOException {
String fileName = uploadSample();
page.navigate(SITE + "/download");
Download download = page.waitForDownload(() -> page.getByText(fileName).click());
Path saved = Paths.get("target", "downloads", download.suggestedFilename());
download.saveAs(saved);
Assert.assertEquals(download.suggestedFilename(), fileName);
Assert.assertEquals(Files.readString(saved), CONTENT);
}
waitForDownloadruns the click and waits for the file to arrive.suggestedFilenameis the name the server sent.saveAscopies it intotarget/downloads, creating the folder if needed.- Reading it back proves the bytes survived the round trip.
Files.readStringneeds Java 11, which Day 4 already set.- Real-life example: an e-banking app's Download statement button works the same way.
- Downloads are allowed by default in Playwright, so no extra option is needed.
Step 3: Run it in the suite
- Add FileTransferTest to your Day 13 staging suite file.
- Leave it out of the prod suite. It writes a file to a server, and Day 13 keeps prod read-only.
- Each run adds one tiny text file to a shared server, so don't loop this test.
- On GitHub Actions the download lands in
target/downloadson the runner. - Upload that folder too if you ever need to inspect a bad download.
Mistakes that break this setup
- Clicking the Upload button without
setInputFilesfirst: the site shows an error page. - Hardcoding a name from the download list: someone else's file may be gone tomorrow.
- Calling
download.path()and expecting your file name: it returns a random temp name. - Leaving out
throws IOException:Files.readStringwon't compile without it. - Saving downloads to the project root: they end up in git. Keep them under
target. - The site is a free shared server. If your file is missing from the list, run the test again.
What carries forward
- FramesAndAlertsTest, FruitMockTest and InventoryTest are unchanged.
- FileTransferTest uploads a file and downloads it back.
Day 23 runs the whole suite on Chromium, Firefox and WebKit.