Skip to content

Commit 4481af6

Browse files
authored
Fix flaky autofocus stream test by waiting for the initial autofocus to settle (#1504)
Chrome's native autofocus processing for the page's [autofocus] button can run after `page.goto` resolves. If the test's stream rendering happens in between, the pending autofocus steals focus from the stream-appended input. Wait for the initial autofocus to complete before proceeding.
1 parent b67039e commit 4481af6

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/tests/functional/autofocus_tests.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ test("navigating a frame with a turbo-frame targeting the frame autofocuses [aut
4040
})
4141

4242
test("receiving a Turbo Stream message with an [autofocus] element when the activeElement is the document", async ({ page }) => {
43+
// Ensure the [autofocus] element has been processed before blurring
44+
await expect(page.locator("#first-autofocus-element")).toBeFocused()
45+
4346
await page.evaluate(() => {
4447
document.activeElement.blur()
4548
window.Turbo.renderStreamMessage(`
@@ -48,6 +51,7 @@ test("receiving a Turbo Stream message with an [autofocus] element when the acti
4851
</turbo-stream>
4952
`)
5053
})
54+
5155
await expect(page.locator("#autofocus-from-stream")).toBeFocused()
5256
})
5357

@@ -62,7 +66,6 @@ test("autofocus from a Turbo Stream message does not leak a placeholder [id]", a
6266
})
6367

6468
await expect(page.locator("#container-from-stream input")).toBeFocused()
65-
6669
})
6770

6871
test("receiving a Turbo Stream message with an [autofocus] element when an element within the document has focus", async ({ page }) => {

0 commit comments

Comments
 (0)