Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(shadow dom): follow assigned slot when hit-testing (#37777)

Simon Knottccb908adac88
diff --git a/packages/injected/src/injectedScript.ts b/packages/injected/src/injectedScript.ts
index 76969f4ed6..3ed79366a9 100644
--- a/packages/injected/src/injectedScript.ts
+++ b/packages/injected/src/injectedScript.ts
@@ -989,7 +989,9 @@ export class InjectedScript {
const hitParents: Element[] = [];
while (hitElement && hitElement !== targetElement) {
hitParents.push(hitElement);
- hitElement = parentElementOrShadowHost(hitElement);
+ // Prefer the composed tree over the light-dom tree, as browser performs hit testing on the composed tree.
+ // Note that we will still eventually climb to the light-dom parent, as any element distributed to a slot is a direct child of the shadow host that contains the slot.
+ hitElement = hitElement.assignedSlot ?? parentElementOrShadowHost(hitElement);
}
if (hitElement === targetElement)
return 'done';
diff --git a/tests/page/page-click.spec.ts b/tests/page/page-click.spec.ts
index f048ea6c57..b9fa76c3d5 100644
--- a/tests/page/page-click.spec.ts
+++ b/tests/page/page-click.spec.ts
@@ -1261,3 +1261,29 @@ it('should set PointerEvent.pressure on pointermove', async ({ page, isLinux, he
[0, 50, 50],
]);
});
+
+it('should click into shadow root with slotted div', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/37768' } }, async ({ page }) => {
+ await page.setContent(`
+ <my-button>
+ <template shadowrootmode="open">
+ <button><slot></slot></button>
+ </template>
+ <div>Foo</div>
+ </my-button>
+ `);
+
+ await page.getByRole('button', { name: 'Foo' }).click();
+});
+
+it('should click shadow root button', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/37768' } }, async ({ page }) => {
+ await page.setContent(`
+ <my-button>
+ <template shadowrootmode="open">
+ <button><slot></slot></button>
+ </template>
+ <div>Foo</div>
+ </my-button>
+ `);
+
+ await page.locator('my-button').click();
+});
diff --git a/tests/page/selectors-css.spec.ts b/tests/page/selectors-css.spec.ts
index 3c34bd5c55..95980e1fdd 100644
--- a/tests/page/selectors-css.spec.ts
+++ b/tests/page/selectors-css.spec.ts
@@ -468,3 +468,16 @@ it('css on the handle should be relative', async ({ page }) => {
expect(await div.$eval(`.find-me`, e => e.id)).toBe('target2');
expect(await page.$eval(`div >> .find-me`, e => e.id)).toBe('target2');
});
+
+it('should use light DOM structure for child combinator with slotted content', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/37768' } }, async ({ page }) => {
+ await page.setContent(`
+ <my-button>
+ <template shadowrootmode="open">
+ <button><slot></slot></button>
+ </template>
+ <div class="content">Foo</div>
+ </my-button>
+ `);
+ expect(await page.$eval(`my-button > div`, e => e.className)).toBe('content');
+ expect(await page.$eval(`my-button > .content`, e => e.textContent)).toBe('Foo');
+});