Skip to content

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

fix(fill): focus the editing host when filling its descendant (#43487)

Yury Semikhatskyb8c5f51db914
diff --git a/packages/injected/src/injectedScript.ts b/packages/injected/src/injectedScript.ts
index 86ebc166fe..46a7d13523 100644
--- a/packages/injected/src/injectedScript.ts
+++ b/packages/injected/src/injectedScript.ts
@@ -953,7 +953,12 @@ export class InjectedScript {
textarea.focus();
return 'done';
}
- (element as HTMLElement | SVGElement).focus();
+ // Focus the editing host, like a click would: its descendants are not focusable,
+ // and without focus the input goes to whichever frame has it.
+ let focusTarget = element;
+ while ((focusTarget as HTMLElement).isContentEditable && (focusTarget.parentElement as HTMLElement | null)?.isContentEditable)
+ focusTarget = focusTarget.parentElement!;
+ (focusTarget as HTMLElement | SVGElement).focus();
const range = element.ownerDocument.createRange();
range.selectNodeContents(element);
const selection = element.ownerDocument.defaultView!.getSelection();
diff --git a/tests/page/page-fill.spec.ts b/tests/page/page-fill.spec.ts
index 922b99edae..6ff7cfba6c 100644
--- a/tests/page/page-fill.spec.ts
+++ b/tests/page/page-fill.spec.ts
@@ -300,14 +300,19 @@ it('FI-024 should fill fixed position input', async ({ page }) => {
expect(await page.evaluate(() => document.querySelector('input').value)).toBe('some value');
});
-it('FI-025 should be able to fill when focus is in the wrong frame', async ({ page }) => {
+it('FI-025 should be able to fill when focus is in the wrong frame', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43483' },
+}, async ({ page }) => {
await page.setContent(`
<div contentEditable="true"></div>
- <iframe></iframe>
+ <iframe srcdoc="<body contenteditable><p>old</p></body>"></iframe>
`);
await page.focus('iframe');
await page.fill('div', 'some value');
expect(await page.$eval('div', d => d.textContent)).toBe('some value');
+ const paragraph = page.frameLocator('iframe').locator('p');
+ await paragraph.fill('new');
+ await expect(paragraph).toHaveText('new');
});
it('FI-026 should be able to fill the input[type=number]', async ({ page }) => {