How to fix Playwright download tests that hang waiting for files?

Playwright

Downloads hang when listeners are attached after the click or when the app opens downloads in a new flow.

Register waitForEvent('download') before triggering the action.

const downloadPromise = page.waitForEvent('download');
await page.getByRole('button', { name: 'Export CSV' }).click();
const download = await downloadPromise;
await download.saveAs('test-results/report.csv');

Do not await the click first if it instantly starts the download.