Shadow DOM access in Cypress - Build an Automation Script
describe('Shadow DOM Access Test', () => { beforeEach(() => { cy.visit('/shadow-dom-page') }) it('Clicks button inside Shadow DOM and verifies text update', () => { cy.get('#shadow-host') .shadow() .find('.shadow-button') .should('be.visible') .click() cy.get('#shadow-host') .shadow() .find('.shadow-text') .should('have.text', 'Button clicked!') }) })
The test starts by visiting the page containing the Shadow DOM element.
We locate the host element with #shadow-host, then use .shadow() to access its Shadow DOM.
Inside the Shadow DOM, we find the button with class .shadow-button, check it is visible, and click it.
After clicking, we again access the Shadow DOM and find the paragraph with class .shadow-text, asserting its text changed to 'Button clicked!'.
This approach uses Cypress's built-in Shadow DOM support and avoids hard waits by relying on .should() which retries until the condition is met or times out.
Now add data-driven testing with 3 different buttons inside Shadow DOM that update text differently
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
