Some websites trigger a file download, such as when exporting a report or saving a document. In a test, you might want to check whether the file was downloaded, verify its name, or inspect its contents. This page provides starter code that can be used to set up an assertion against a downloaded file.

How to set it up
Start by identifying the step in your test that triggers the file download, usually a “Click element” step that clicks a download link or button.
Before that step, add a Playwright code step to wait for the download to start.
const downloadPromise = page.waitForEvent("download");Then, after the click step, add another Playwright code step to handle and assert against the downloaded file.
Asserting the file content
The example below loads the downloaded file content into memory and asserts that the string Q2 report is contained within the file.
const download = await downloadPromise;
const downloadStream = await download.createReadStream();
const fileContent = await new Promise((resolve) => {
const chunks: Buffer[] = [];
downloadStream.on("data", (chunk) => {
chunks.push(chunk);
});
downloadStream.on("end", () => {
resolve(Buffer.concat(chunks));
});
});
// Example assertion — update this to match your use case
expect(fileContent.toString("utf-8")).toContain("Q2 report");Asserting the filename
The example below asserts that the filename includes the string Q2 report.
const download = await downloadPromise;
// Get the file name
const fileName = download.suggestedFilename();
// Example assertion for the file name
expect(fileName).toContain("Q2 report");Refer to the Playwright documentation for more ideas about other download actions you can create with Autify Nexus’s Playwright code steps.