How to fix flaky Playwright popup/new-tab tests (`page.waitForEvent('popup')`)?

Playwright

Popup tests fail when code listens for the new page too late.

Create the popup wait promise first, then click the opener.

const popupPromise = page.waitForEvent('popup');
await page.getByRole('link', { name: 'Open receipt' }).click();
const popup = await popupPromise;
await popup.waitForLoadState('domcontentloaded');

If the app uses target="_blank", validate the popup URL before assertions.