How do you work with Shadow DOM?
Cypress does not see inside a shadow root because Shadow DOM encapsulates the subtree and cy.get works like querySelector, stopping at that boundary. There are two ways across: the .shadow() command on the host, after which find and assertions work, or the includeShadowDom: true option on a command or in the config. The option widens queries, so keep selectors narrow. A closed shadow root is unreachable by design.
Cypress · Practice and details
cy.get('[data-cy=pay]') finds the button in DevTools but not in the test. Where is it?
A payment widget is built as a web component: the button is in DevTools, but cy.get('[data-cy=pay]') reports «Timed out retrying: Expected to find element». The button lives inside a shadow root, and a normal selector query does not enter it. This lesson explains why and how to get around it.
How it works and why
Shadow DOM encapsulates a subtree: styles and selectors from outside do not see it, and neither does document.querySelector. This is a deliberate platform design: a component should not leak into the page. cy.get works like querySelector, so by default it stops at the shadow root boundary. Cypress offers two ways across.
Two ways into a shadow root
.shadow()
The command enters the shadow root of exactly this element; then an ordinary find.
includeShadowDom
An option in the config (or in command options): queries pass through every shadow boundary.
Example: before → after
// before: not found, the button is in a shadow root
cy.get('[data-cy=pay]').click();
// after, targeted
cy.get('payment-widget').shadow().find('[data-cy=pay]').click();
// or once through a command option
cy.get('[data-cy=pay]', { includeShadowDom: true }).click();
// cypress.config.ts
export default defineConfig({
e2e: { includeShadowDom: true }, // globally for all queries
});
After .shadow() Cypress treats the content as an ordinary subtree: find, contains and assertions retry as usual. Nested components are walked in a chain: .shadow().find('inner-el').shadow().find(...).
Limits and alternatives
includeShadowDom: trueis convenient when there are many shadow components, but queries become wider: a selector may match an element deep inside someone else's component. Narrow selectors (data-cy) reduce that risk.- This is about an open shadow root. A closed one (
mode: 'closed') is by design unreachable to outside code, so for it agree test hooks (data-cyon the host itself, a separate build mode) with the developers. cy.containsalso enters a shadow root only with the option:cy.contains('Pay', { includeShadowDom: true }). The option applies to a single command, so a few places do not need the global setting.- Nested components and slots: content passed into a
<slot>physically lives in the host light DOM, socy.getfinds it without.shadow(). You only need to enter the shadow root for elements the component creates itself. - In Playwright locators (
getByRole, CSS) pierce open Shadow DOM by default; nothing to enable.
Common mistakes
- Raising the timeout or adding
cy.waitwhen an element «is not found»: the cause is the shadow boundary, not timing. - Calling
.shadow()on an element without a shadow root: the command fails. First make sure it is a custom-element host. - Enabling
includeShadowDom«just in case» and not noticing that old selectors now match duplicates.
Check yourself
An element is visible in DevTools but cy.get times out. What is the first hypothesis and how do you check it?
Answer
The element is in a shadow root. In DevTools it sits under a #shadow-root node. Check with cy.get(host).shadow().find(...) or { includeShadowDom: true }.
When is .shadow() better and when the global option?
Answer
.shadow() for a few places, where it is visible in code. The global option when the app is almost entirely web components and .shadow() would be needed everywhere.
shadow() command. Middle/Senior: If there are many Shadow DOM components I enable includeShadowDom in the config, but keep selectors narrow. Closed shadow roots are unreachable by design, which needs an agreement with development. In Playwright open Shadow DOM works with no setup.