New tabs, popups and iframes
A new tab is a new Page in the same BrowserContext, and the popup event fires once and is not stored. So the waitForEvent promise is created before the click and awaited after it. An iframe is a separate document that an ordinary locator does not see, so it is entered through frameLocator, which works with cross-origin frames too.
Playwright · Interaction
A click opened a new tab, yet the test waits for it until the timeout. Has the event already passed?
The "Contract" link opens in a new tab. The test clicks, then writes await page.waitForEvent('popup'), and 30 seconds later it fails with a timeout, even though in the video the tab opened at once. Another test types a card number into a payment widget and cannot find the field: it is visible on the screen, yet the locator "sees nothing". Both problems share a root: a new tab and an iframe live in their own "spaces", and Playwright has to be told to go there explicitly. Below: how this is built, why the order "listener, click, await" is not negotiable, and how to enter an iframe.
How it works and why. A new tab in Playwright is a new Page in the same BrowserContext: it shares cookies and storage with the opener, so the login is kept. Playwright emits the event about the new page once, at the moment the browser creates it. It is an ordinary event emitter with no "memory": whoever did not subscribe in advance does not get the event and cannot read it later. A click runs fast, and by the time the test reaches the next line the event has already passed. Hence the rule: create the promise before the click and await it after.
The popup event fires once the tab has navigated to its initial URL, but the document may still be loading. Locators and web-first assertions (toHaveURL, element visibility) wait on their own, so waitForLoadState() is rarely needed: before steps that do not auto-wait, such as evaluate.
Order of steps when a tab opens
Example: from a hang to a stable test. A test written "in sequence", the way it reads:
// before: the listener is created after the click
await page.getByRole('link', { name: 'Contract' }).click();
const popup = await page.waitForEvent('popup'); // may wait until the timeout
await expect(popup).toHaveURL(/contract/);
There is no log with an answer here: the test just waits until the timeout and fails with a message about waiting for the popup event. That very lack of a hint gives the cause away: the event happened, but there was no listener at that moment. The correct version:
const popupPromise = page.waitForEvent('popup');
await page.getByRole('link', { name: 'Contract' }).click();
const popup = await popupPromise;
await popup.waitForLoadState();
await expect(popup).toHaveURL(/contract/);
await expect(popup.getByRole('heading', { name: 'Contract' })).toBeVisible();
page.waitForEvent('popup') catches tabs opened from a specific page (window.open, target="_blank"). context.waitForEvent('page') catches any new page of the context, including one opened by another tab. The Promise.all variant is also correct, because the listener is created before the click. Do not "simplify" the test by removing target="_blank" in the DOM: then it no longer checks the real behavior.
// any new page of the context, not only one opened by this page
const [newPage] = await Promise.all([
context.waitForEvent('page'),
page.getByRole('button', { name: 'Open report' }).click(),
]);
await newPage.waitForLoadState('domcontentloaded');
// every page in the context, e.g. to check how many tabs are open
expect(context.pages()).toHaveLength(2);
Iframe. An iframe is not a separate page but a separate document inside a page. Ordinary locators search the main document and do not go into a frame, so page.getByText(…) will not find text that is visible on the screen. An explicit step is needed: frameLocator returns an object on which you call the usual getByRole, getByLabel, and so on. locator.contentFrame() does the same from the locator of the iframe element, and the way back is frameLocator.owner(). Both are lazy like locators: they wait until the frame appears and loads, so no sleep is needed.
Where a locator searches
page.getByRole('button', { name: 'Pay' })
Searches the main document. The button inside the iframe is visible on screen, but to the locator it does not exist: "element not found".
page.frameLocator('#payment-iframe').getByRole(…)
First enters the iframe's document, then searches in it. Waits until the frame appears and loads.
const card = page.frameLocator('#payment-iframe');
await card.getByLabel('Card number').fill('4242424242424242');
await card.getByRole('button', { name: 'Pay' }).click();
// the same through a locator of the iframe element
const frame = page.locator('#payment-iframe').contentFrame();
await expect(frame.getByText('Payment accepted')).toBeVisible();
// nested frames: a chain
const inner = page.frameLocator('#outer').frameLocator('#inner');
await inner.getByRole('button', { name: 'Confirm' }).click();
// the iframe element itself lives in the main document
await expect(page.locator('#payment-iframe')).toBeVisible();
Cross-origin frames, such as a payment widget from another domain, work the same way, because Playwright drives the browser from outside and not from the page's JavaScript, so the same-origin policy does not apply to it.
Limits and alternatives. The old page.frame({ name }) returns a Frame object with methods like frame.locator. It does not wait and returns null if the frame is not there yet, so frameLocator is better for new tests. In Selenium an iframe needs switchTo().frame(), and afterwards all commands go into that frame until you switch back: easy to forget to leave. In Playwright the step is local to a single locator, so there is no "current frame". In Cypress iframe support is limited: a same-origin frame can be queried through its document, but there is no command to switch into a frame, so a plugin is often used. A new tab in Cypress does not work like in Playwright either: the test stays in one tab. Playwright's strong side is that a tab is a full Page that you work with like the main one.
Common mistakes.
- A click before
waitForEvent: the event is missed and the test waits until the timeout. - Working with the
popupwithoutwaitForLoadStateor an assertion: the first check sees an empty page. - A main-page locator for an element inside an iframe.
- Your own
setTimeoutwaits for the frame to appear instead of the lazyframeLocator. - Forgotten open tabs: each test gets a new context, so they usually do not interfere, but in a shared context they must be closed.
Check yourself
A test first does click() on a link, and the next line is await page.waitForEvent('popup'). In the video the tab opens, yet the test fails with a timeout. Why?
Answer
The popup event arrived during the click, when there was no listener, and Playwright does not keep past events. The next waitForEvent waits for a new event that never comes. Create the promise before the click (const p = page.waitForEvent('popup')), click, and then await p, or use Promise.all.
The payment form is visible on screen, but page.getByLabel('Card number') does not find the field. What do you do, and does it matter that the frame is from another domain?
Answer
The field lives in another document, so enter the frame: page.frameLocator('#payment-iframe').getByLabel('Card number'). The other domain does not matter: Playwright drives the browser from outside, and the same-origin policy does not apply to it.
A test opens a popup and immediately checks popup.getByRole('heading'), but sometimes fails on an empty page. Which line do you add and why?
Answer
At the moment of the event the tab's document may still be loading. Add await popup.waitForLoadState() or rely on a web-first assertion such as expect(popup).toHaveURL(…), which retries until the page has loaded.
waitForEvent('popup') promise, then click, then await the promise. For an iframe I use frameLocator. Middle/Senior: The event arrives once and is not stored, so the listener must exist before the click, otherwise the test hangs until the timeout. A new tab belongs to the same context and shares cookies. An iframe is a separate document, so an ordinary locator does not see into it. frameLocator is lazy and works with cross-origin frames because Playwright drives the browser from outside. Unlike Selenium there is no "current frame" that is easy to forget to reset.