Discover how to unlock hidden parts of web pages and test them like a pro!
Why Shadow DOM access in Cypress? - Purpose & Use Cases
Imagine testing a web page where some parts are hidden inside special containers called Shadow DOMs. You try clicking buttons or reading text manually, but they don't respond because they are inside these hidden layers.
Manually trying to find and interact with elements inside Shadow DOM is like searching for a hidden treasure without a map. Normal tools can't see inside, so tests fail or become very slow and unreliable.
Using Shadow DOM access in Cypress lets you peek inside these hidden layers easily. It gives you a clear path to find and test elements inside Shadow DOM, making your tests faster, stable, and more accurate.
cy.get('custom-element').find('button').click() // Fails because button is inside Shadow DOM
cy.get('custom-element').shadow().find('button').click() // Works by accessing Shadow DOM
It enables reliable testing of modern web components that use Shadow DOM, unlocking full control over hidden UI parts.
Testing a web app with custom dropdown menus built using Shadow DOM, ensuring the dropdown opens and options can be selected correctly.
Manual testing fails because Shadow DOM hides elements from normal selectors.
Cypress Shadow DOM access reveals hidden elements for easy testing.
This makes tests more reliable and covers modern web components fully.
Practice
Solution
Step 1: Understand Shadow DOM access in Cypress
Cypress requires theshadow()command to enter the Shadow DOM context before finding elements inside it.Step 2: Use the correct chaining method
The correct syntax iscy.get('selector').shadow().find('inner-selector')to access elements inside Shadow DOM.Final Answer:
Usecy.get('selector').shadow().find('inner-selector')-> Option AQuick Check:
Shadow DOM access = cy.get().shadow().find() [OK]
- Trying to find elements inside Shadow DOM without .shadow()
- Using non-existent commands like cy.shadow() or cy.getShadow()
- Assuming .find() works directly on Shadow DOM host
submitBtn inside a Shadow DOM hosted by my-component?Solution
Step 1: Identify the Shadow DOM host
The elementmy-componentis the Shadow DOM host, so we start withcy.get('my-component').Step 2: Access the Shadow DOM and find the button
We must use.shadow()to enter the Shadow DOM, then.find('#submitBtn')to locate the button inside.Final Answer:
cy.get('my-component').shadow().find('#submitBtn') -> Option BQuick Check:
Shadow DOM host + .shadow() + .find() = cy.get('my-component').shadow().find('#submitBtn') [OK]
- Skipping .shadow() and using .find() directly
- Using non-existent commands like cy.shadow()
- Confusing host selector with inner element selector
cy.get('custom-element').shadow().find('.title').should('have.text', 'Welcome')Solution
Step 1: Understand the command chain
The code correctly gets the Shadow DOM hostcustom-element, enters its Shadow DOM with.shadow(), then finds the element with classtitle.Step 2: Evaluate the assertion
The assertion checks if the found element's text is exactly 'Welcome'. If true, the test passes; otherwise, it fails.Final Answer:
The test passes if the element with class 'title' inside the Shadow DOM has text 'Welcome'. -> Option CQuick Check:
Correct chaining + assertion = The test passes if the element with class 'title' inside the Shadow DOM has text 'Welcome'. [OK]
- Assuming .shadow() is invalid after .get()
- Confusing global elements with Shadow DOM elements
- Misunderstanding assertion target scope
cy.get('widget-component').find('button.submit').click()What is the most likely reason?
Solution
Step 1: Analyze the command chain
The code tries to find a button insidewidget-componentwithout entering Shadow DOM.Step 2: Identify missing Shadow DOM access
To access elements inside Shadow DOM,.shadow()must be used before.find(). Missing it causes failure to locate the button.Final Answer:
You forgot to use.shadow()before.find()to access Shadow DOM. -> Option AQuick Check:
Missing .shadow() causes element not found [OK]
- Skipping .shadow() when accessing Shadow DOM
- Assuming .find() works globally inside Shadow DOM host
- Blaming selector syntax without checking Shadow DOM usage
outer-component hosts a Shadow DOM containing inner-component, which itself hosts another Shadow DOM with a button #confirm. Which Cypress command chain correctly clicks the button?Solution
Step 1: Access the first Shadow DOM
Start withcy.get('outer-component')and enter its Shadow DOM using.shadow().Step 2: Access the nested Shadow DOM
Findinner-componentinside the first Shadow DOM, then enter its Shadow DOM again with.shadow().Step 3: Find and click the button
Inside the nested Shadow DOM, find the button with id#confirmand perform.click().Final Answer:
cy.get('outer-component').shadow().find('inner-component').shadow().find('#confirm').click() -> Option DQuick Check:
Nested Shadow DOM requires chaining .shadow() twice [OK]
- Trying to access nested Shadow DOM without multiple .shadow() calls
- Using .find() directly without entering Shadow DOM
- Confusing order of .shadow() and .find() calls
Ready to prove it?
Pass the test and this pattern moves unknown
