Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(webkit): hit target check inside nested shadow roots (#43127)
Pavel Feldmanc0a167b77957
diff --git a/packages/injected/src/injectedScript.ts b/packages/injected/src/injectedScript.tsindex d020ee1c12..64043ea4f1 100644--- a/packages/injected/src/injectedScript.ts+++ b/packages/injected/src/injectedScript.ts@@ -1054,15 +1054,19 @@ export class InjectedScript {elements.unshift(singleElement);}}- if (elements[0] && elements[0].shadowRoot === root && elements[1] === singleElement) {- // Workaround webkit but where first two elements are swapped:- // <host>- // #shadow root- // <target>- // elementsFromPoint produces [<host>, <target>], while it should be [<target>, <host>]- // In this case, just ignore <host>.+ // Workaround webkit bug: elementsFromPoint() puts the outermost shadow host first (seen with <input>).+ //+ // DOM root.elementsFromPoint() in WebKit+ // <outer-host>+ // #shadow-root [<outer-host>, <inner-host>, ...]+ // <inner-host> ^^^^^^^^^^^^ bogus+ // #shadow-root [<outer-host>, <input>, <inner-host>, ...]+ // <input> ^^^^^^^^^^^^ bogus+ //+ // elementFromPoint() is correct. Drop elements[0] when elements[1] matches it and elements[0]+ // is the host of this or any enclosing root. Using `roots` makes it work for closed shadow roots.+ if (elements[0] && elements[1] === singleElement && roots.slice(index).some(r => (r as ShadowRoot).host === elements[0]))elements.shift();- }const innerElement = elements[0] as Element | undefined;if (!innerElement)break;diff --git a/tests/library/hit-target.spec.ts b/tests/library/hit-target.spec.tsindex 69fcd19251..9443051999 100644--- a/tests/library/hit-target.spec.ts+++ b/tests/library/hit-target.spec.ts@@ -464,3 +464,51 @@ it('should click in custom element', async ({ page }) => {await page.locator('input').click();expect(await page.evaluate('window.__clicked')).toBe(true);});++it('should click in nested custom elements', {+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43118' },+}, async ({ page }) => {+ await page.setContent(`+ <outer-host></outer-host>+ <script>+ customElements.define('inner-host', class extends HTMLElement {+ connectedCallback() {+ this.attachShadow({ mode: 'open' }).innerHTML = '<inner-most></inner-most>';+ }+ });+ customElements.define('inner-most', class extends HTMLElement {+ connectedCallback() {+ this.attachShadow({ mode: 'open' }).innerHTML = '<input>';+ this.shadowRoot.querySelector('input').addEventListener('click', () => window.__clicked = true);+ }+ });+ customElements.define('outer-host', class extends HTMLElement {+ connectedCallback() {+ this.attachShadow({ mode: 'open' }).innerHTML = '<inner-host></inner-host>';+ }+ });+ </script>+ `);+ await page.locator('input').click();+ expect(await page.evaluate('window.__clicked')).toBe(true);+});++it('should click an element inside nested closed shadow roots', {+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43118' },+}, async ({ page }) => {+ await page.setContent(`+ <div id=container></div>+ <script>+ const input = document.createElement('input');+ input.addEventListener('click', () => window.__clicked = true);+ const outer = document.getElementById('container').attachShadow({ mode: 'closed' });+ const inner = document.createElement('div');+ outer.appendChild(inner);+ inner.attachShadow({ mode: 'closed' }).appendChild(input);+ window.__target = input;+ </script>+ `);+ const handle = await page.evaluateHandle('window.__target');+ await (handle as any as ElementHandle).click();+ expect(await page.evaluate('window.__clicked')).toBe(true);+});