interface EnhancedHTMLElement extends HTMLElement {connectedCallback?(): void;disconnectedCallback?(): void;}interface CustomElementConstructor {new (...params: unknown[]): EnhancedHTMLElement;}export default function customElementWrapper () {const origFn = customElements.define.bind(customElements)customElements.define = function(name: string, Constructor: CustomElementConstructor, options?: ElementDefinitionOptions) {const origConnectedCallback = Constructor.prototype.connectedCallbackConstructor.prototype.connectedCallback = function(this: HTMLElement) {let parentNode: ParentNode | null = thiswhile (parentNode.parentNode) {parentNode = parentNode.parentNode}console.debug('[WDIO]', 'newShadowRoot', this, parentNode, parentNode === document, document.documentElement)return origConnectedCallback?.call(this)}const origDisconnectedCallback = Constructor.prototype.disconnectedCallbackConstructor.prototype.disconnectedCallback = function(this: HTMLElement) {console.debug('[WDIO]', 'removeShadowRoot', this)return origDisconnectedCallback?.call(this)}return origFn(name, Constructor, options)}const origAttachShadow = Element.prototype.attachShadowElement.prototype.attachShadow = function (this: HTMLElement, init: ShadowRootInit) {const shadowRoot = origAttachShadow.call(this, init)let parentNode: ParentNode | null = thiswhile (parentNode.parentNode) {parentNode = parentNode.parentNode}console.debug('[WDIO]', 'newShadowRoot', this, parentNode, parentNode === document, document.documentElement)return shadowRoot}}