Selector.shadowRoot Method

Returns the shadow root node. Chain to other methods to traverse the shadow DOM. Returns null if the shadow DOM is not open.

selector.shadowRoot() → Selector

Warning

You cannot perform actions with the shadowRoot() element, or use it in assertions. Use this element as the shadow DOM entry point.

The example below shows how to identify the root node and access the shadow tree:

import { Selector } from 'testcafe';

fixture`Selector.shadowRoot`
    .page`https://devexpress.github.io/testcafe/example/`;

test('Get text within shadow tree', async t => {
    const shadowRoot = Selector('div').withAttribute('id', 'shadow-host').shadowRoot();
    const paragraph  = shadowRoot.child('p');

    await t.expect(paragraph.textContent).eql('This paragraph is in the shadow tree');

    try {
        await t.click(shadowRoot);
        // causes an error
        // do not target the shadow root directly or use it in assertions
    }
    catch (error) {
        await t.expect(error.code).eql('E27');
    }
});