← Back to library
Playwrightauthadvancedv1.20+
Handle OAuth popup / new page
Intercept a new browser tab that opens during OAuth and authenticate inside it.
1test('OAuth login via popup', async ({ page, context }) => {
2 await page.goto('/login');
3
4 // Listen for the new tab BEFORE clicking — race-condition safe
5 const [popup] = await Promise.all([
6 context.waitForEvent('page'),
7 page.getByRole('button', { name: 'Sign in with Google' }).click(),
8 ]);
9
10 await popup.waitForLoadState('domcontentloaded');
11 await popup.fill('[name="email"]', 'user@gmail.com');
12 await popup.fill('[name="password"]', 'secret');
13 await popup.getByRole('button', { name: 'Next' }).click();
14
15 // Wait for OAuth to complete and the popup to close
16 await popup.waitForEvent('close');
17
18 await expect(page.getByText('Welcome')).toBeVisible();
19});#oauth#popup#new page#multi-tab