diff --git a/packages/host/core/browser/harness.ts b/packages/host/core/browser/harness.ts index 8e93f6f8..fdb9668f 100644 --- a/packages/host/core/browser/harness.ts +++ b/packages/host/core/browser/harness.ts @@ -110,9 +110,19 @@ export async function modeGroup(scope: Scope): Promise { await menu.waitFor() return menu.getByRole('radiogroup', {name: 'Comparison mode'}) } -/** Chooses a comparison mode (in the menu, choosing closes it). */ +/** + * Chooses a comparison mode. In the menu, choosing closes it, and this returns once the menu has handed focus on: it + * does so only as it unmounts, after its exit animation, so a control the test focused before then would lose its + * focus, and the keys pressed next would miss it. + */ export async function chooseMode(scope: Scope, name: string) { - await (await modeGroup(scope)).getByRole('radio', {name, exact: true}).click() + const group = await modeGroup(scope) + const inMenu = (await group.locator('xpath=ancestor::*[@role="dialog"]').count()) > 0 + await group.getByRole('radio', {name, exact: true}).click() + if (!inMenu) return + await settled(scope) + // The popover's focus scope hands focus on in a timer it starts as it unmounts; one started now runs after it. + await pageOf(scope).evaluate(() => new Promise(resolve => setTimeout(resolve, 0))) } /** Asserts the checked mode, closing the menu again if it had to be opened. */ export async function expectMode(scope: Scope, name: string) {