DOM Inspection Tests Are Not Supported

The content and structure of HTML, CSS, and the DOM in Lightning Experience can change at any time and is not a supported API. UI tests that reach into component internals using tools, such as Selenium WebDriver, are hard to maintain. This is not a change in Spring ’19, as we’ve never supported HTML, CSS and the DOM as a public API. We’re calling out the fragility of these tests again due to changes in this release as Lightning Experience continues to evolve with modern web standards.

Where: This release note applies to Lightning Experience and Salesforce Classic.

Why: Parts of Lightning Experience use Lightning web components, which are based on the Web Components standard. This standard includes Shadow DOM, which hides a component's markup, style, and behavior from other components. This encapsulation poses challenges for UI tests, especially tests that rely on globally searching the DOM or reaching into the internals of custom elements.

How: In Spring ’19, we made significant changes to introduce a shadowRoot property that encapsulates an element's DOM subtree. This shadowRoot is represented as a DocumentFragment in the DOM and elements inside this DOM subtree are not available by traditional DOM querying methods. Elements that are rendered by Lightning web components contain this new shadowRoot property.

You can't globally query the DOM for an element inside a shadowRoot. For example, this Selenium WebDriver query throws an error if the <div> tag is inside a custom element with a shadowRoot property.

WebDriver.findElement(By.cssSelector('div'))

We recommend writing unit tests for Aura components and Lightning web components.

For Aura components, use Lightning Testing Service to write automated tests to ensure your components perform as expected. Lightning Testing Service supports testing with standard JavaScript test frameworks. It provides easy-to-use wrappers for using Jasmine and Mocha.

For Lightning web components, use Jest to write automated tests.