DOM Inspection Tests Are Subject to Change

The content and structure of HTML, CSS, and the DOM in Lightning Experience can change at any time and can’t be considered a stable API. UI tests that reach into component internals using tools, such as Selenium WebDriver, require your ongoing maintenance. This is not a change in Spring ’19, as we’ve never guaranteed backward-compatible HTML, CSS and the DOM. 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. We understand the value that our customers gain from automated UI testing, and the maintenance burden this puts on them.

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, and these elements are hidden from normal DOM queries.

We recommend using unit-testing tools to test individual Aura components and Lightning web components, and only using UI testing tools for end-to-end testing.

For Aura components, use Lightning Testing Service to write unit 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 lwc-jest to write unit tests.

For end-to-end UI tests, you need to adapt your existing tests for Shadow DOM use. How to do this varies by tool, and is rapidly evolving. At this moment in time, this article is a good example for Selenium WebDriver.