← Back to library
Playwrightfileintermediatev1.20+

Download a file and verify content

Trigger a download, save it locally, and assert the filename and file content.

1import path from 'path';
2import fs from 'fs';
3
4test('downloads a CSV report', async ({ page }) => {
5  await page.goto('/reports');
6
7  const [download] = await Promise.all([
8    page.waitForEvent('download'),
9    page.getByRole('button', { name: 'Export CSV' }).click(),
10  ]);
11
12  const filePath = path.join(__dirname, 'downloads', download.suggestedFilename());
13  await download.saveAs(filePath);
14
15  expect(download.suggestedFilename()).toMatch(/\.csv$/);
16
17  const content = fs.readFileSync(filePath, 'utf-8');
18  expect(content).toContain('email,name,role'); // assert CSV header row
19});
#download#csv#save#readFileSync