Testing iframes and Shadow DOM with Playwright
Run a Playwright example against an iframe editor and nested open Shadow DOM. Understand frame scope and the closed-root boundary.
Identify the document boundary before choosing how your locator should reach the control.
An iframe contains a separate document. A shadow root belongs to a component inside a document. Both can make an element look reachable in a screenshot while an ordinary DOM query misses it. The first debugging step is to identify which boundary you are looking at.
Two live exercises
Start with the iframe editor, whose iframe has the stable id iframe-editor. Its editable element is labelled Headline editor. The parent page receives updates through postMessage and displays the resulting HTML in iframe-preview.
Then open Shadow DOM. The open core contains a labelled memory input and another custom element with a nested open root. A second, sealed core uses a closed root.
A frameLocator scopes subsequent locators to the iframe document. Playwright's usual locators can reach through open shadow roots. XPath does not pierce them. A closed root is outside the supported locator path, so test an exposed behavior or public interface when a real product deliberately uses that boundary.
Run the example
Use the Playwright starter with Node.js 22.12 or newer. Save the complete example as tests/guide.spec.js. Run npm ci, then npx playwright install chromium, then npm test -- tests/guide.spec.js. The starter runs desktop Chromium and a Pixel 7 Chromium profile. Each test creates a fresh browser context. Examples target the public practice site and require no account.
Verified on 4 November 2026 with Playwright 1.62.0.
import { test, expect } from '@playwright/test';
test("Frames and open Shadow DOM", async ({ page }) => {
await page.addInitScript(() => localStorage.setItem('funui-consent', 'denied'));
await page.goto('https://funui.dev/challenges/iframe');
const editor = page.frameLocator('#iframe-editor').getByLabel('Headline editor', { exact: true });
await editor.fill('Automation crosses a document boundary');
await expect(editor).toHaveText('Automation crosses a document boundary');
await expect(page.getByTestId('iframe-preview')).toContainText('Automation crosses a document boundary');
await page.goto('https://funui.dev/challenges/shadow-dom');
const core = page.getByTestId('shadow-dom-open');
const memory = core.getByLabel('Write to memory', { exact: true });
await memory.fill('Remember the test contract');
await expect(memory).toHaveValue('Remember the test contract');
await expect(core.locator('#bank-secret')).toHaveText("SECRET: the droid's real name is Kevin");
const sealed = page.getByTestId('shadow-dom-closed');
await expect(sealed).toBeVisible();
expect(await sealed.evaluate(element => element.shadowRoot)).toBeNull();
});
Expected result
The editor contains the headline and the parent mirror receives it. The open-core input holds the new memory, and the nested bank exposes its documented secret. The sealed host is visible, while its shadowRoot property is null. Both starter project runs pass.
The final assertion establishes the closed-root boundary. It does not inspect the sealed content or validate its behavior. A screenshot showing sealed text and a null shadowRoot can both be correct observations.
Keep the test scoped
If the editor cannot be found, first check the iframe host selector and its loaded document. A page-level getByLabel does not enter the iframe. If an open-root control cannot be found, check whether it has a useful accessible label and whether the host locator is unique.
The parent mirror assertion retries until the message has reached React. Reading the mirror immediately with textContent would take a one-time snapshot and could race that update. Filling the editable region keeps this example focused on document scope. Toolbar formatting deserves a separate test because selection and formatting add another contract.
Practice deeper document nesting in Nested Frames. Compare this boundary model with locators and waits. Reference the official frame guide and Shadow DOM locator guidance.
Keep reading
- Practice this topic
- How to Test Drag and Drop (and Why It Fails)
- Playwright Locators and Waits: A Practical Guide
- QA Interview Bug Hunts: A Playwright Walkthrough
- Five Stable-Locator Anti-Patterns
- Network Idle Is Not “Ready”
- Testing a Custom Combobox Without Sleeps
- Deterministic Test Data Without Boring Tests
- Visibility vs. Presence: The Wait Condition Bug
Updated
Fun UI Playground — all challenges, bug hunts, getting started, glossary, about.