0
0
Cypresstesting~10 mins

Shadow DOM access in Cypress - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - Cypress
Cypress
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!')
  })
})
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initialized, no browser opened yet-PASS
2Browser opens and navigates to 'https://example.com/shadowdom'Browser shows the page with a custom element 'my-shadow-host' containing shadow DOM-PASS
3Find 'my-shadow-host' element and access its shadow DOM'my-shadow-host' element is found, shadow DOM is accessible-PASS
4Find button with id 'shadow-btn' inside shadow DOM and click itButton inside shadow DOM is found and clicked-PASS
5Find span with id 'status' inside shadow DOM and check its textSpan inside shadow DOM is foundVerify span text is 'Clicked!'PASS
6Test endsTest completed successfully-PASS
Failure Scenario
Failing Condition: The button inside the shadow DOM is not found or not clickable
Execution Trace Quiz - 3 Questions
Test your understanding
Which Cypress command is used to access elements inside the shadow DOM?
A.shadow()
B.findShadow()
C.shadowRoot()
D.getShadow()
Key Result
When testing elements inside shadow DOM, use the '.shadow()' command in Cypress to access the shadow root before finding elements. This ensures reliable interaction with shadow DOM elements.