Testing / Playwright Interview questions
How does Playwright handle multiple tabs and windows?
Each new tab or window opened from the page - via a link with target="_blank" or a window.open() call - fires a 'page' event on its parent BrowserContext. Playwright doesn't switch focus automatically like some tools; instead, you capture the new page object explicitly.
const [newPage] = await Promise.all([ context.waitForEvent('page'), page.getByRole('link', { name: 'Open in new tab' }).click(), ]); await newPage.waitForLoadState();
From there, newPage is an independent Page object with its own locators and lifecycle, while still sharing the same cookies and storage as the original page because they belong to the same browser context. This makes it easy to assert on both tabs, or close one while keeping the other active.
More Related questions...