Traverse into the shadow DOM of an element.
.shadow(selector)
.shadow(selector, options)
Correct Usage
cy.get('.shadow-host').shadow()
Incorrect Usage
cy.shadow() // Errors, cannot be chained off 'cy'
cy.exec('npm start').shadow() // Errors, 'exec' does not yield DOM element
cy.get('.not-a-shadow-host').shadow() // Errors, subject must host a shadow root
.shadow() yields the new DOM element(s) it found. <div class="shadow-host">
#shadow-root
<button class="my-button">Click me</button>
</div>
// yields [#shadow-root (open)]
cy.get('.shadow-host').shadow().find('.my-button').click()
.shadow() requires being chained off a command that yields a DOM element that is a shadow host (i.e. has a shadow root directly attached to it). .shadow() will automatically retry until the element(s) exist in the DOM .shadow() will automatically retry until the element(s) host(s) a shadow root. .shadow() will automatically retry until all chained assertions have passed .shadow() can time out waiting for the element(s) to exist in the DOM . .shadow() can time out waiting for the element(s) to host a shadow root. .shadow() can time out waiting for assertions you've added to pass. When working with cy.click(), it sometimes won't click the right element in Chrome. It's happening because of the ambiguity in spec.
In this case, pass { position: 'top' } to cy.click() like below:
cy.get('#element').shadow().find('[data-test-id="my-button"]').click({ position: 'top' })
Traverse into the shadow DOM of an element
cy.get('.shadow-host').shadow()
The commands above will display in the Command Log as:
When clicking on the shadow command within the command log, the console outputs the following:
cy.get() with includeShadowDom optioncy.find() with includeShadowDom optioncy.contains() with includeShadowDom optionincludeShadowDom config option
© 2017 Cypress.io
Licensed under the MIT License.
https://docs.cypress.io/api/commands/shadow