diff --git a/.changeset/check-sr-only-label.md b/.changeset/check-sr-only-label.md new file mode 100644 index 0000000000..5779a88dbb --- /dev/null +++ b/.changeset/check-sr-only-label.md @@ -0,0 +1,5 @@ +--- +"@e2e-dev/web": patch +--- + +`check`, `uncheck`, and `tap` click a checkbox or radio that `sr-only` or `visually-hidden` clips, the usual toggle-switch markup, through its own label instead of failing with `intercepts pointer events`. `uncheck` still refuses a native radio without clicking it. diff --git a/docs/reference/screen.mdx b/docs/reference/screen.mdx index 1e9fd33264..2cc2201b29 100644 --- a/docs/reference/screen.mdx +++ b/docs/reference/screen.mdx @@ -791,8 +791,9 @@ await screen.getByRole('checkbox', 'Remember me').check(); ``` On the web engine, a checkbox or radio hidden under its own label (as in -Element Plus) is clicked through that label. Anything else on top of the -point the click lands on still fails `ACTION_FAILED`. +Element Plus) or clipped by `sr-only` or `visually-hidden` is clicked +through that label. Anything else on top of the point the click lands on +still fails `ACTION_FAILED`. #### `uncheck` diff --git a/packages/web/src/actions.ts b/packages/web/src/actions.ts index 1985317ef4..eca258ab15 100644 --- a/packages/web/src/actions.ts +++ b/packages/web/src/actions.ts @@ -98,15 +98,17 @@ async function setChecked(target: ActionTarget, checked: boolean, operation: Con const UNRENDERED = 'the control and its labels are not visible'; /** - * Runs in the page. A component library (Element Plus) hides a native - * checkbox or radio under its label's styled spans with `z-index: -1`, or - * gives it no box at all, so Playwright refuses the input itself (` - * intercepts pointer events`, `element is not visible`) though a user's click - * there reaches it through the label. Returns that label, or null to click the - * control itself. The choice reads the control and its labels only, past - * anything else on top (a loading mask, a modal): Playwright's checks on the - * label wait that out, or fail on it. Throws `UNRENDERED` while none of them - * is rendered (a dialog not open yet), since nothing tells the two apart then. + * Runs in the page. Returns the label to click in place of a checkbox or + * radio that a click cannot reach at its own centre, or null to click the + * control itself. Element Plus hides the native input under its label's + * styled spans with `z-index: -1` or gives it no box, and `sr-only` clips it, + * so Playwright refuses the input (` intercepts pointer events`, + * `element is not visible`) though a user's click reaches it through the + * label. A label with a link or other control at the point is never chosen. + * The choice reads the control and its labels only, past anything else on + * top (a loading mask, a modal), which Playwright's checks on the label then + * wait out or fail on. Throws `UNRENDERED` while no label can be hit yet (a + * dialog not open, or not taking pointer events), so the read is retried. */ function pickStandIn(node: Element, unrendered: string): HTMLLabelElement | null { if (!(node instanceof HTMLInputElement) || (node.type !== 'checkbox' && node.type !== 'radio') || node.matches(':disabled')) { @@ -122,37 +124,46 @@ function pickStandIn(node: Element, unrendered: string): HTMLLabelElement | null root.elementsFromPoint(x, y).find((hit) => hit === node || labels.some((label) => label.contains(hit))); const pressesInteractive = (label: HTMLLabelElement, hit: Element | undefined): boolean => label.contains(hit?.closest(INTERACTIVE) ?? null); - node.scrollIntoView({ block: 'nearest', inline: 'nearest' }); - const box = node.getBoundingClientRect(); - if (!hasBox(box)) { + const labelAtCentre = (requireHit: boolean): HTMLLabelElement | null => { const shown = labels.filter((label) => hasBox(label.getBoundingClientRect()) && label.checkVisibility({ visibilityProperty: true })); if (shown.length === 0) throw new Error(unrendered); - return ( - shown.find((label) => { - label.scrollIntoView({ block: 'nearest', inline: 'nearest' }); - return !pressesInteractive(label, topmost(...centre(label.getBoundingClientRect()))); - }) ?? null - ); - } + let unreached = false; + for (const label of shown) { + label.scrollIntoView({ block: 'nearest', inline: 'nearest' }); + const hit = topmost(...centre(label.getBoundingClientRect())); + if (hit === undefined && requireHit) unreached = true; + else if (!pressesInteractive(label, hit)) return label; + } + if (unreached) throw new Error(unrendered); + return null; + }; + node.scrollIntoView({ block: 'nearest', inline: 'nearest' }); + const box = node.getBoundingClientRect(); + if (!hasBox(box)) return labelAtCentre(false); const [x, y] = centre(box); - const hit = topmost(x, y); - if (hit === undefined) throw new Error(unrendered); - const label = labels.find((own) => own.contains(hit)); - if (label === undefined || pressesInteractive(label, hit)) return null; - const outer = label.getBoundingClientRect(); - return x >= outer.left && x <= outer.right && y >= outer.top && y <= outer.bottom ? label : null; + const hits = root.elementsFromPoint(x, y); + const hit = hits.find((each) => each === node || labels.some((label) => label.contains(each))); + const label = hit === undefined ? undefined : labels.find((own) => own.contains(hit)); + if (label !== undefined && !pressesInteractive(label, hit)) { + const outer = label.getBoundingClientRect(); + if (x >= outer.left && x <= outer.right && y >= outer.top && y <= outer.bottom) return label; + } + if (!hits.includes(node)) return labelAtCentre(true); + return null; } /** * Runs in the page: where a click on the label lands, the centre of its - * control relative to the label, or none for a control with no box, which - * takes the label's centre. + * control relative to the label, or none for a control with no box or one + * centred outside the label, which takes the label's centre. */ function controlPoint(label: HTMLLabelElement): { x: number; y: number } | null { const box = label.control?.getBoundingClientRect(); if (box === undefined || box.width === 0 || box.height === 0) return null; const outer = label.getBoundingClientRect(); - return { x: box.left + box.width / 2 - outer.left, y: box.top + box.height / 2 - outer.top }; + const x = box.left + box.width / 2 - outer.left; + const y = box.top + box.height / 2 - outer.top; + return x < 0 || x > outer.width || y < 0 || y > outer.height ? null : { x, y }; } /** diff --git a/packages/web/tests/integration/check-actions.test.ts b/packages/web/tests/integration/check-actions.test.ts index f792defbdc..6bac8a0695 100644 --- a/packages/web/tests/integration/check-actions.test.ts +++ b/packages/web/tests/integration/check-actions.test.ts @@ -254,6 +254,22 @@ const ELEMENT_PLUS_CASES = [ ['radio', '#large'], ] as const; +const VISUALLY_HIDDEN_PAGE = ` + + +`; + +const VISUALLY_HIDDEN_CASES = [ + ['Tailwind sr-only toggle switch', '#toggle'], + ['Tailwind sr-only checkbox in a text label', '#plain'], + ['Bootstrap visually-hidden radio', '#pro'], +] as const; + describe.each(KINDS)('a control its own label covers, through the %s target', (kind) => { it.each(ELEMENT_PLUS_CASES)('checks an Element Plus %s', async (_, selector) => { await page.setContent(ELEMENT_PLUS_PAGE); @@ -308,6 +324,77 @@ describe.each(KINDS)('a control its own label covers, through the %s target', (k } }); + it.each(VISUALLY_HIDDEN_CASES)('checks and taps a %s through its label', async (_, selector) => { + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + await page.setContent(VISUALLY_HIDDEN_PAGE); + expect(await perform(await targetOf(kind, selector), action)).toBeUndefined(); + expect(await page.isChecked(selector)).toBe(true); + } + }); + + it('unchecks a Tailwind sr-only toggle switch through its label', async () => { + await page.setContent(VISUALLY_HIDDEN_PAGE); + await page.evaluate(() => { + (document.getElementById('toggle') as HTMLInputElement).checked = true; + }); + expect(await perform(await targetOf(kind, '#toggle'), { kind: 'uncheck' })).toBeUndefined(); + expect(await page.isChecked('#toggle')).toBe(false); + }); + + it('still refuses a Tailwind sr-only checkbox whose label has a link at its centre, and follows no link', async () => { + await page.setContent(` + `); + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + expect(await perform(await targetOf(kind, '#c'), action, REFUSAL_TIMEOUT_MS)).toMatchObject({ code: 'NOT_ACTIONABLE' }); + expect(await page.isChecked('#c')).toBe(false); + expect(page.url()).not.toContain('#went'); + } + }); + + it('checks a Tailwind sr-only checkbox inside a padded label at its own point, past a link at the label centre', async () => { + await page.setContent(` + `); + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + await page.evaluate(() => { + (document.getElementById('c') as HTMLInputElement).checked = false; + }); + expect(await perform(await targetOf(kind, '#c'), action)).toBeUndefined(); + expect(await page.isChecked('#c')).toBe(true); + expect(page.url()).not.toContain('#went'); + } + }); + + it('still refuses a Tailwind sr-only checkbox whose label has a link at its centre once its dialog takes pointer events, and follows no link', async () => { + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + await page.setContent(` + + `); + expect(await perform(await targetOf(kind, '#c'), action, REFUSAL_TIMEOUT_MS * 2)).toMatchObject({ code: 'NOT_ACTIONABLE' }); + expect(await page.isChecked('#c')).toBe(false); + expect(page.url()).not.toContain('#went'); + } + }); + + it('checks a boxless checkbox through a label that wraps, whose box centre falls between its lines', async () => { + await page.setContent(`
aaaaaaaaaaaaaaaaa +
`); + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + await page.evaluate(() => { + (document.getElementById('c') as HTMLInputElement).checked = false; + }); + expect(await perform(await targetOf(kind, '#c'), action)).toBeUndefined(); + expect(await page.isChecked('#c')).toBe(true); + } + }); + + it('checks a visible one-pixel checkbox whose only label is hidden as itself', async () => { + await page.setContent(` + + `); + expect(await perform(await targetOf(kind, '#c'), { kind: 'check' })).toBeUndefined(); + expect(await page.isChecked('#c')).toBe(true); + }); + it('still refuses a radio whose label covers it from outside the label box', async () => { await page.setContent(`