Test Overview
This test opens a webpage containing a shadow DOM element. It accesses a button inside the shadow DOM and clicks it. Then it verifies that clicking the button changes the text inside the shadow DOM as expected.
This test opens a webpage containing a shadow DOM element. It accesses a button inside the shadow DOM and clicks it. Then it verifies that clicking the button changes the text inside the shadow DOM as expected.
describe('Shadow DOM access test', () => { it('should click button inside shadow DOM and verify text change', () => { cy.visit('https://example.com/shadowdom') cy.get('my-shadow-host').shadow().find('button#shadow-btn').click() cy.get('my-shadow-host').shadow().find('span#status').should('have.text', 'Clicked!') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initialized, no browser opened yet | — | PASS |
| 2 | Browser opens and navigates to 'https://example.com/shadowdom' | Browser shows the page with a custom element 'my-shadow-host' containing shadow DOM | — | PASS |
| 3 | Find 'my-shadow-host' element and access its shadow DOM | 'my-shadow-host' element is found, shadow DOM is accessible | — | PASS |
| 4 | Find button with id 'shadow-btn' inside shadow DOM and click it | Button inside shadow DOM is found and clicked | — | PASS |
| 5 | Find span with id 'status' inside shadow DOM and check its text | Span inside shadow DOM is found | Verify span text is 'Clicked!' | PASS |
| 6 | Test ends | Test completed successfully | — | PASS |
shadow() command to enter the Shadow DOM context before finding elements inside it.cy.get('selector').shadow().find('inner-selector') to access elements inside Shadow DOM.cy.get('selector').shadow().find('inner-selector') -> Option AsubmitBtn inside a Shadow DOM hosted by my-component?my-component is the Shadow DOM host, so we start with cy.get('my-component')..shadow() to enter the Shadow DOM, then .find('#submitBtn') to locate the button inside.cy.get('custom-element').shadow().find('.title').should('have.text', 'Welcome')custom-element, enters its Shadow DOM with .shadow(), then finds the element with class title.cy.get('widget-component').find('button.submit').click()widget-component without entering Shadow DOM..shadow() must be used before .find(). Missing it causes failure to locate the button..shadow() before .find() to access Shadow DOM. -> Option Aouter-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?cy.get('outer-component') and enter its Shadow DOM using .shadow().inner-component inside the first Shadow DOM, then enter its Shadow DOM again with .shadow().#confirm and perform .click().Pass the test and this pattern moves unknown