← Back to library
Playwrightnavigationintermediatev1.20+
Switch between browser tabs
Open a new tab, interact with it, then return focus to the original tab.
1test('switches between tabs', async ({ page, context }) => {
2 await page.goto('/home');
3
4 // Capture the new tab at the same time as the click that opens it
5 const [newTab] = await Promise.all([
6 context.waitForEvent('page'),
7 page.getByRole('link', { name: 'Open in new tab' }).click(),
8 ]);
9
10 await newTab.waitForLoadState('domcontentloaded');
11 await expect(newTab).toHaveURL(//guide/);
12 await newTab.getByRole('button', { name: 'Accept cookies' }).click();
13
14 // Return focus to the original tab
15 await page.bringToFront();
16 await expect(page).toHaveURL(//home/);
17});#tab#new tab#context#bringToFront#popup