Embedded Widgets
Frames as real apps use them: loaded late, navigating, talking to the page, opening dialogs, sandboxed, and mocked.
A frame that appears later
Frame locators wait for the frame and for the element inside it, so a late frame needs nothing special. The Frame API does not wait: page.frame() returns null until the frame is attached.
await page.getByRole('button', { name: 'Open chat' }).click();
const chat = page.getByTitle('Support chat').contentFrame();
await chat.getByLabel('Message').fill('Hi'); // waits for the frameA frame that navigates
Each checkout step loads a new URL inside the frame, while the page's own URL stays the same. A frame locator keeps working across those navigations. To check the frame's URL, use the Frame object:
const checkout = page.frame({ name: 'checkout' })!;
await expect.poll(() => checkout.url()).toContain('step=2');
await expect(page).toHaveURL('/frames/embeds'); // unchangedFrames talking to the page
Widgets report results with postMessage. Test the whole loop: act inside the frame, then assert what the page shows.
Dialogs from inside a frame
alert() and confirm() inside a frame still reach page.on('dialog'). Register the handler before the click, as on the JavaScript Dialogs page.
Sandboxed frames
sandbox="allow-scripts" gives the frame an opaque origin, like a widget from another site. The page's own scripts cannot read it, but Playwright controls the browser and can. Your test does not change.
Mocking what a frame loads
A frame's document is a network request like any other, so page.route() can replace it. Set the route up before the page loads:
await page.route('**/embeds/weather/', (route) =>
route.fulfill({ contentType: 'text/html', body: '<p>-5°C, snow</p>' })
);
await page.goto('/frames/embeds');Support chat
The chat widget loads 1.5 seconds after you open it.
Checkout
A three-step checkout. Each step is a new page inside the frame.
Account settings
The button inside this frame asks for confirmation.
Sandboxed rating widget
Page script access to this frame: checking
Weather
A third-party embed. Replace it with your own content.
Related exercises
- FramesIntermediateNested & Multiple FramesReach a frame inside a frame, choose between identical frames, and find a frame with no title.
- Network & APIAdvancedNetwork MockingControl network responses to test states that are hard to get from a real backend.
- Dialogs & WindowsBeginnerJavaScript DialogsHandle native alert, confirm and prompt dialogs, including the ones that arrive in pairs, late, or when you leave.
Read the guide: Testing iframes with Playwright