diff --git a/.changeset/check-through-label.md b/.changeset/check-through-label.md new file mode 100644 index 0000000000..13b098e244 --- /dev/null +++ b/.changeset/check-through-label.md @@ -0,0 +1,5 @@ +--- +"@e2e-dev/web": patch +--- + +`check`, `uncheck`, and `tap` click a checkbox or radio hidden under its own label (as in Element Plus) through that label. `uncheck` still refuses a native radio without clicking it. Anything else on top of the point the click lands on still fails `ACTION_FAILED`. diff --git a/.changeset/list-label-drawn-checkbox.md b/.changeset/list-label-drawn-checkbox.md new file mode 100644 index 0000000000..35d12d128c --- /dev/null +++ b/.changeset/list-label-drawn-checkbox.md @@ -0,0 +1,5 @@ +--- +"@e2e-dev/web": patch +--- + +The agent now sees a checkbox or radio that a component library shrinks to no size under its own label (as in Element Plus), with its checked state. Before, it saw only the label text, and a tap that ticked the box reported no change. diff --git a/docs/reference/screen.mdx b/docs/reference/screen.mdx index eace89d5b5..b9a2046e38 100644 --- a/docs/reference/screen.mdx +++ b/docs/reference/screen.mdx @@ -760,6 +760,10 @@ check(options?: ActionOptions): Promise; 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`. + #### `uncheck` ```ts diff --git a/docs/web.mdx b/docs/web.mdx index 7e4a8bb16b..32db8ddc50 100644 --- a/docs/web.mdx +++ b/docs/web.mdx @@ -255,6 +255,13 @@ link is also listed as a control. The agent reads the first 512 characters of each block of text. In a longer block, e2e also lists its links and emphasized text on their own. +The agent also sees a checkbox or radio that a component library shrinks +to no size under its own label, as Element Plus does, with its checked +state. A tap on it clicks the label. When that label is or holds a link +or another control, the agent sees the label's text and that control but +not the checkbox, since a click on the label could land on the link. Locators still call that control hidden, as +Playwright does, so check it with `toBeChecked()`, not `toBeVisible()`. + ### Scoping a test to one size `platforms` and `requires` can't select a viewport, because every browser diff --git a/packages/web/src/actions.ts b/packages/web/src/actions.ts index 6a2f22410f..1985317ef4 100644 --- a/packages/web/src/actions.ts +++ b/packages/web/src/actions.ts @@ -62,7 +62,7 @@ async function setChecked(target: ActionTarget, checked: boolean, operation: Con retryable: false, }); } - await element.click({ timeout: remaining() }); + await clickControl({ kind: 'element', element }, verb, { timeout: remaining(), signal: operation.signal }); const hint = radio === 'aria' ? '; a radio usually clears only when another radio in its group is selected' : ''; const unchanged = (detail = ''): EngineError => new EngineError('NOT_ACTIONABLE', `${verb} clicked the control but its checked state did not change${detail}${hint}`, { @@ -91,6 +91,127 @@ async function setChecked(target: ActionTarget, checked: boolean, operation: Con } } +/** + * What the page throws while a checkbox or radio and every one of its labels + * are unrendered, so a click could reach none of them yet. + */ +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. + */ +function pickStandIn(node: Element, unrendered: string): HTMLLabelElement | null { + if (!(node instanceof HTMLInputElement) || (node.type !== 'checkbox' && node.type !== 'radio') || node.matches(':disabled')) { + return null; + } + const labels = [...(node.labels ?? [])]; + const root = node.getRootNode(); + if (labels.length === 0 || (!(root instanceof Document) && !(root instanceof ShadowRoot))) return null; + const INTERACTIVE = 'a,button,input,select,textarea,summary,[contenteditable],[role="button"],[role="link"]'; + const hasBox = ({ width, height }: DOMRect): boolean => width > 0 && height > 0; + const centre = ({ left, top, width, height }: DOMRect): [number, number] => [left + width / 2, top + height / 2]; + const topmost = (x: number, y: number): Element | undefined => + 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 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 + ); + } + 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; +} + +/** + * 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. + */ +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 }; +} + +/** + * Clicks a control with Playwright's actionability checks, through its own + * label when `pickStandIn` names one. The checks then run on the label, so + * anything else on top, such as a modal, still fails NOT_ACTIONABLE. The + * choice is read again until the control or a label renders, within the + * deadline. + */ +async function clickControl( + target: ActionTarget, + verb: 'tap' | 'check' | 'uncheck', + options: { timeout: number; signal: AbortSignal; modifiers?: KeyModifier[] }, +): Promise { + const { signal, ...click } = options; + const deadline = new Deadline(options.timeout); + const remaining = (): number => Math.max(1, deadline.remaining()); + let label: ElementHandle | undefined; + let firstRead = true; + await pollCondition({ + deadline, + signal, + negated: false, + evaluate: async () => { + const first = firstRead; + firstRead = false; + try { + const handle = + target.kind === 'locator' + ? await target.locator.evaluateHandle(pickStandIn, UNRENDERED, { timeout: remaining() }) + : await target.element.evaluateHandle(pickStandIn, UNRENDERED); + label = handle.asElement() ?? undefined; + if (label === undefined) void handle.dispose().catch(() => undefined); + return true; + } catch (cause) { + if (message(cause).includes(UNRENDERED)) return undefined; + // A read after one that found nothing rendered times out only at the deadline, which ends the poll. + if (!first && isPwTimeout(cause)) return undefined; + throw cause; + } + }, + onTimeout: () => + new EngineError('NOT_ACTIONABLE', `${verb} did not become actionable in time: ${UNRENDERED}`, { retryable: false }), + }); + // The operation abandons this call when cancelled; a read still in flight then must not lead to a click. + if (label === undefined) { + signal.throwIfAborted(); + await asActionable(target).click({ ...click, timeout: remaining() }); + return; + } + try { + const position = await label.evaluate(controlPoint); + signal.throwIfAborted(); + await label.click({ ...click, timeout: remaining(), ...(position === null ? {} : { position }) }); + } finally { + void label.dispose().catch(() => undefined); + } +} + /** * Which kind of radio a checkable element is, if any. A native radio can be * selected but never cleared by a click. An ARIA radio is clicked like any @@ -165,7 +286,7 @@ export async function dispatchLocatorAction( const locator = asActionable(target); switch (action.kind) { case 'tap': - await locator.click({ timeout, ...heldKeys(action) }); + await clickControl(target, 'tap', { timeout, signal: operation.signal, ...heldKeys(action) }); return; case 'doubleTap': await locator.dblclick({ timeout, ...heldKeys(action) }); diff --git a/packages/web/src/in-page/read-semantics.ts b/packages/web/src/in-page/read-semantics.ts index 13ad9515fa..2b9223a2e8 100644 --- a/packages/web/src/in-page/read-semantics.ts +++ b/packages/web/src/in-page/read-semantics.ts @@ -1104,6 +1104,28 @@ export const readSemanticsFunction = ( return !(rect.width > 0 && rect.height > 0); }; + /** + * True for a checkbox or radio hidden only by having no box, under a shown + * label of its own that draws it, as in Element Plus. Chrome's tree keeps + * such an input, and without it the model reads the label text with no + * checked state. A tap clicks the label in its place (`pickStandIn` in + * `actions.ts`), which needs a box (so not `display: contents`) and refuses + * when the label's centre lands on a link or other control. This rule is + * stricter: a label that is one, or holds one anywhere, does not count. + */ + const isDrawnByLabel = (el: Element, style: CSSStyleDeclaration | undefined): boolean => { + if (!(el instanceof HTMLInputElement) || (el.type !== 'checkbox' && el.type !== 'radio')) return false; + if (style?.visibility !== 'visible' || isSkippedContent(el)) return false; + const INTERACTIVE = 'a,button,input,select,textarea,summary,[contenteditable],[role="button"],[role="link"]'; + return associatedLabels(el).some( + (label) => + styleOf(label)?.display !== 'contents' && + !isHidden(label) && + !label.matches(INTERACTIVE) && + !Array.from(label.querySelectorAll(INTERACTIVE)).some((inner) => inner !== el), + ); + }; + /** * What takes the element and everything under it out of the tree walk: an * `aria-hidden` or inert subtree, which the accessibility tree drops though @@ -1574,14 +1596,17 @@ export const readSemanticsFunction = ( } let nextParent = parent; + const drawnByLabel = hidden && isDrawnByLabel(el, style); // An empty painted rectangle carries no semantics to be "interesting" by and // is still something a person sees and aims at; `roleOf` names it `box`. - if (!hidden && (isInteresting(el) || isVisibleEmptyBox(el))) { + if ((!hidden || drawnByLabel) && (isInteresting(el) || isVisibleEmptyBox(el))) { if (nodes.length >= maxNodes) { truncated = true; return; } nextParent = include(el, parent, style); + // Listed on purpose, so not "hidden", as a select's options are below. + if (drawnByLabel) nodes[nextParent]!.states.hidden = false; } // A closed select paints none of its options, so the hidden test would // drop every one of them — and a model shown `combobox "Category"` alone diff --git a/packages/web/tests/crosscheck/expected.txt b/packages/web/tests/crosscheck/expected.txt index 3b335501f3..be79ca7f38 100644 --- a/packages/web/tests/crosscheck/expected.txt +++ b/packages/web/tests/crosscheck/expected.txt @@ -2,6 +2,10 @@ # page | oracle field | role "name" | ours | theirs | reason # Generated by E2E_CROSSCHECK_UPDATE=1; edit only the reasons. See tests/crosscheck/expectations.ts. benchmark: file-round-trip | chrome name | button "" | ours | theirs Choose File | An unlabeled file input shows the browser's localized button text, which the page cannot read; Playwright reports it too, the tree leaves the input unnamed rather than guess the locale +fixture: controls drawn by their label | chrome missing | checkbox "" | ours no node | theirs checkbox | A checkbox with no box whose only label is not rendered shows a person nothing to aim at; the tree lists one only while a label of its own draws it +fixture: controls drawn by their label | chrome missing | checkbox "Accept the terms" | ours no node | theirs checkbox | A tap on a checkbox with no box clicks its label, which it refuses when the label's centre lands on a link, so the click follows no link; the tree leaves the checkbox out when its label holds a link anywhere, a stricter rule, rather than list one a tap may not reach +fixture: controls drawn by their label | chrome missing | checkbox "Contents" | ours no node | theirs checkbox | A `display: contents` label has no box, so a tap cannot click it in place of a checkbox with no box; the tree leaves the checkbox out and the model taps the label text, which sets it +fixture: controls drawn by their label | chrome missing | checkbox "Unlabelled" | ours no node | theirs checkbox | An aria-label draws nothing: a checkbox with no box and no label element shows a person nothing to aim at, so the tree leaves it out fixture: disabled states | chrome disabled | group "Shipping" | ours true | theirs false | `:disabled` matches a disabled fieldset itself; the tree keeps that so the model reads why the controls inside are disabled. Chrome and Playwright mark only the controls fixture: disabled states | chrome selected | option "Poland" | ours true | theirs false | Chrome drops the selected state of an option in a disabled select; Playwright agrees with the tree that it is the value fixture: generated content | chrome name | link "Sign out" | ours Sign out | theirs | Chrome names a link whose only content is an icon-font glyph by the glyph, which is nothing once dropped; the tree falls back to its title so the model reads the word a person hovering it sees diff --git a/packages/web/tests/crosscheck/fixtures.ts b/packages/web/tests/crosscheck/fixtures.ts index a494489968..ded135e02d 100644 --- a/packages/web/tests/crosscheck/fixtures.ts +++ b/packages/web/tests/crosscheck/fixtures.ts @@ -128,6 +128,21 @@ export const FIXTURE_PAGES: readonly FixturePage[] = [ `, }, + { + name: 'controls drawn by their label', + html: ` + + + + + + + + + + + `, + }, { name: 'disabled states', html: ` diff --git a/packages/web/tests/integration/check-actions.test.ts b/packages/web/tests/integration/check-actions.test.ts index 5cc98146cc..385bd4884c 100644 --- a/packages/web/tests/integration/check-actions.test.ts +++ b/packages/web/tests/integration/check-actions.test.ts @@ -11,9 +11,12 @@ import { createServer, type Server } from 'node:http'; import type { AddressInfo } from 'node:net'; import { chromium, type Browser, type Page } from 'playwright-core'; import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'; -import type { LocatorAction } from 'e2e/engine'; +import type { LocatorAction, SemanticNode } from 'e2e/engine'; import { classifyActionError, dispatchLocatorAction } from '../../src/actions.ts'; +import { ElementList } from '../../src/element-list.ts'; +import { captureDocument } from '../../src/observation.ts'; import type { ActionTarget } from '../../src/support.ts'; +import { Published } from '../helpers/published.ts'; let browser: Browser; let page: Page; @@ -46,19 +49,24 @@ beforeEach(async () => { const KINDS = ['locator', 'element'] as const; -/** The control as the engine would hold it: a locator, or an element handle pinned by an observation. */ +/** + * The control as the engine would hold it: a locator, or the element handle + * an observed ref resolves to, made on first use from the walk's in-page array. + */ async function targetOf(kind: (typeof KINDS)[number], selector: string): Promise { const locator = page.locator(selector); if (kind === 'locator') return { kind, locator }; - const element = await locator.elementHandle(); - if (element === null) throw new Error(`no element for ${selector}`); - return { kind, element }; + const walk = new ElementList(await locator.evaluateHandle((node) => [node])); + return { kind, element: await walk.at(0) }; } +/** A refusal waits out the whole deadline, so the cases that expect one take a short one. */ +const REFUSAL_TIMEOUT_MS = 500; + /** Performs one action and returns the contract error it failed with, classified as the engine reports it. */ -async function perform(target: ActionTarget, action: LocatorAction): Promise { +async function perform(target: ActionTarget, action: LocatorAction, timeoutMs = 2000): Promise { try { - await dispatchLocatorAction(target, action, { timeoutMs: 2000, signal: new AbortController().signal }, () => { + await dispatchLocatorAction(target, action, { timeoutMs, signal: new AbortController().signal }, () => { throw new Error('no second target'); }); return undefined; @@ -187,3 +195,301 @@ describe('a control gone before the click', () => { expect(await perform(target, { kind: 'check' })).toMatchObject({ code: 'NODE_STALE', retryable: true }); }); }); + +describe('a locator that matches another control after the state read', () => { + it('clicks the control it read, not the one the locator matches now', async () => { + await page.setContent(` + + `); + const locator = page.locator('[data-pick]'); + const moved = new Proxy(locator, { + get(real, property) { + if (property === 'elementHandle') { + return async (options?: { timeout?: number }) => { + const element = await real.elementHandle(options); + await page.evaluate(() => { + document.getElementById('first')!.removeAttribute('data-pick'); + document.getElementById('second')!.setAttribute('data-pick', ''); + }); + return element; + }; + } + const value: unknown = Reflect.get(real, property); + return typeof value === 'function' ? value.bind(real) : value; + }, + }); + expect(await perform({ kind: 'locator', locator: moved }, { kind: 'check' })).toBeUndefined(); + expect(await page.isChecked('#first')).toBe(true); + expect(await page.isChecked('#second')).toBe(false); + }); +}); + +const ELEMENT_PLUS_CSS = ` +.el-radio-button{outline:none;display:inline-block;position:relative} +.el-radio-button__original-radio{opacity:0;z-index:-1;outline:none;position:absolute} +.el-radio-button__inner{white-space:nowrap;vertical-align:middle;background:#fff;border:1px solid #dcdfe6;line-height:1;cursor:pointer;display:inline-block;position:relative;padding:8px 15px} +.el-checkbox{cursor:pointer;display:inline-flex;align-items:center;position:relative} +.el-checkbox__input{white-space:nowrap;cursor:pointer;outline:none;display:inline-flex;position:relative} +.el-checkbox__original{opacity:0;z-index:-1;outline:none;width:0;height:0;margin:0;position:absolute} +.el-checkbox__inner{display:inline-block;position:relative;border:1px solid #dcdfe6;width:14px;height:14px;background:#fff;z-index:1} +.el-radio{cursor:pointer;display:inline-flex;align-items:center;position:relative} +.el-radio__input{white-space:nowrap;cursor:pointer;outline:none;display:inline-flex;position:relative} +.el-radio__original{opacity:0;z-index:-1;outline:none;margin:0;position:absolute;top:0;bottom:0;left:0;right:0} +.el-radio__inner{border:1px solid #dcdfe6;border-radius:100%;width:14px;height:14px;background:#fff;position:relative;display:inline-block} +.modal{position:fixed;inset:0;background:rgba(0,0,0,.4);z-index:10} +`; + +const ELEMENT_PLUS_CONTROLS = ` + + + + +`; + +const ELEMENT_PLUS_PAGE = `${ELEMENT_PLUS_CONTROLS}`; + +const ELEMENT_PLUS_CASES = [ + ['radio-button', '#install'], + ['checkbox', '#agree'], + ['radio', '#large'], +] 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); + expect(await perform(await targetOf(kind, selector), { kind: 'check' })).toBeUndefined(); + expect(await page.isChecked(selector)).toBe(true); + }); + + it.each(ELEMENT_PLUS_CASES)('taps an Element Plus %s', async (_, selector) => { + await page.setContent(ELEMENT_PLUS_PAGE); + expect(await perform(await targetOf(kind, selector), { kind: 'tap' })).toBeUndefined(); + expect(await page.isChecked(selector)).toBe(true); + }); + + it('unchecks an Element Plus checkbox, and refuses an Element Plus radio without clicking it', async () => { + await page.setContent(`${COUNT_CLICKS}${ELEMENT_PLUS_PAGE}`); + await page.evaluate(() => { + for (const id of ['agree', 'large']) (document.getElementById(id) as HTMLInputElement).checked = true; + }); + expect(await perform(await targetOf(kind, '#agree'), { kind: 'uncheck' })).toBeUndefined(); + expect(await page.isChecked('#agree')).toBe(false); + const before = await clicks(); + expect(await perform(await targetOf(kind, '#large'), { kind: 'uncheck' })).toMatchObject({ code: 'NOT_ACTIONABLE' }); + expect(await page.isChecked('#large')).toBe(true); + expect(await clicks()).toBe(before); + }); + + it.each(ELEMENT_PLUS_CASES)('still refuses an Element Plus %s under a modal', async (_, selector) => { + await page.setContent(`${ELEMENT_PLUS_PAGE}`); + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + expect(await perform(await targetOf(kind, selector), action, REFUSAL_TIMEOUT_MS)).toMatchObject({ code: 'NOT_ACTIONABLE' }); + expect(await page.isChecked(selector)).toBe(false); + } + }); + + it.each(ELEMENT_PLUS_CASES)('waits out a loading mask over an Element Plus %s', async (_, selector) => { + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + await page.setContent(`${ELEMENT_PLUS_PAGE} + `); + expect(await perform(await targetOf(kind, selector), action)).toBeUndefined(); + expect(await page.isChecked(selector)).toBe(true); + } + }); + + it.each(ELEMENT_PLUS_CASES)('waits for an Element Plus %s in a dialog that opens later', async (_, selector) => { + for (const display of ['display:none', 'visibility:hidden']) { + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + await page.setContent(`
${ELEMENT_PLUS_CONTROLS}
+ `); + expect(await perform(await targetOf(kind, selector), action)).toBeUndefined(); + expect(await page.isChecked(selector)).toBe(true); + } + } + }); + + it('still refuses a radio whose label covers it from outside the label box', async () => { + await page.setContent(` + `); + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + expect(await perform(await targetOf(kind, '#r'), action, REFUSAL_TIMEOUT_MS)).toMatchObject({ + code: 'NOT_ACTIONABLE', + message: expect.stringContaining('intercepts pointer events'), + }); + expect(await page.isChecked('#r')).toBe(false); + } + }); + + it('checks a boxless checkbox through its first visible label, past a hidden one', async () => { + await page.setContent(` + + + `); + expect(await perform(await targetOf(kind, '#c'), { kind: 'check' })).toBeUndefined(); + expect(await page.isChecked('#c')).toBe(true); + }); + + it('checks a boxless checkbox past a label hidden by visibility, which keeps its box', 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 under a link in its own label, and follows no link', async () => { + await page.setContent(` + `); + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + expect(await perform(await targetOf(kind, '#install'), action, REFUSAL_TIMEOUT_MS)).toMatchObject({ + code: 'NOT_ACTIONABLE', + message: expect.stringContaining('intercepts pointer events'), + }); + expect(await page.isChecked('#install')).toBe(false); + expect(page.url()).not.toContain('#went'); + } + }); + + it('still refuses a boxless checkbox whose visible label has a link at its centre', 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', + message: expect.stringContaining('not visible'), + }); + expect(await page.isChecked('#c')).toBe(false); + expect(page.url()).not.toContain('#went'); + } + }); + + it('still refuses an Element Plus radio-button a disabled fieldset holds', async () => { + await page.setContent(`
${ELEMENT_PLUS_CONTROLS}
`); + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + expect(await perform(await targetOf(kind, '#install'), action, REFUSAL_TIMEOUT_MS)).toMatchObject({ + code: 'NOT_ACTIONABLE', + message: expect.stringContaining('not enabled'), + }); + expect(await page.isChecked('#install')).toBe(false); + } + }); + + it('still refuses a boxless checkbox whose every label is hidden', 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', + message: `${action.kind} did not become actionable in time: the control and its labels are not visible`, + }); + expect(await page.isChecked('#c')).toBe(false); + } + }); + + it('still refuses a disabled Element Plus radio-button', async () => { + await page.setContent(ELEMENT_PLUS_PAGE.replace('id="install"', 'id="install" disabled')); + for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) { + expect(await perform(await targetOf(kind, '#install'), action, REFUSAL_TIMEOUT_MS)).toMatchObject({ code: 'NOT_ACTIONABLE' }); + expect(await page.isChecked('#install')).toBe(false); + } + }); + + it('checks an Element Plus radio-button inside a shadow root', async () => { + await page.setContent(`
`); + expect(await perform(await targetOf(kind, '#install'), { kind: 'check' })).toBeUndefined(); + expect(await page.isChecked('#install')).toBe(true); + }); +}); + +describe('a checkbox or radio an observation lists over no box', () => { + it('takes a tap on its listed node, so the walk lists none a tap cannot reach', async () => { + await page.setContent(` + + + + `); + let nextId = 1; + const published = new Published(); + try { + const { tree } = await captureDocument( + { + testIdAttribute: 'data-testid', + site: undefined, + reserveIds: (count) => { const first = nextId; nextId += count; return first; }, + commit: published.commit, + }, + page, + { framePath: [], budget: 100, deadline: Date.now() + 10_000, signal: new AbortController().signal }, + ); + const nodes: SemanticNode[] = []; + const walk = (node: SemanticNode): void => { + nodes.push(node); + for (const child of node.children ?? []) walk(child); + }; + walk(tree); + const listed = nodes.filter((node) => node.role === 'checkbox' || node.role === 'radio'); + expect(listed.map((node) => node.name)).toEqual(['Agree', 'Small']); + for (const node of listed) { + const element = await published.element(node.ref.id); + expect(await element.evaluate((input) => input.getBoundingClientRect().width), node.name ?? '').toBe(0); + expect(await perform({ kind: 'element', element }, { kind: 'tap' }), node.name ?? '').toBeUndefined(); + expect(await element.evaluate((input) => (input as HTMLInputElement).checked), node.name ?? '').toBe(true); + } + // The one a `display: contents` label holds is left out; a tap on the label text sets it. + const text = nodes.find((node) => node.text === 'Contents'); + expect(text).toBeDefined(); + expect(await perform({ kind: 'element', element: await published.element(text!.ref.id) }, { kind: 'tap' })).toBeUndefined(); + expect(await page.isChecked('#contents')).toBe(true); + } finally { + published.dispose(); + } + }); +}); + +describe('a tap on a locator that never appears', () => { + it('fails NOT_ACTIONABLE within one deadline, as a plain click does', async () => { + await page.setContent('

Nothing to tap

'); + const started = Date.now(); + const error = await perform({ kind: 'locator', locator: page.locator('#missing') }, { kind: 'tap' }); + expect(Date.now() - started).toBeLessThan(3500); + expect(error).toMatchObject({ code: 'NOT_ACTIONABLE', retryable: false }); + expect(error?.message).toMatch(/^tap did not become actionable in time: .*Timeout 2000ms exceeded/); + expect(error?.message).toContain("waiting for locator('#missing')"); + }); +}); + +describe('a control its own label covers, inside a frame', () => { + it('checks an Element Plus radio-button', async () => { + await page.setContent(`

Outer

`); + const locator = page.frameLocator('iframe').locator('#install'); + expect(await perform({ kind: 'locator', locator }, { kind: 'check' })).toBeUndefined(); + expect(await locator.isChecked()).toBe(true); + }); +}); + +describe('an action cancelled while its stand-in read is in flight', () => { + it.each(KINDS)('clicks nothing through the %s target', async (kind) => { + for (const [selector, action] of [['#install', { kind: 'tap' }], ['#install', { kind: 'check' }], ['#plain', { kind: 'tap' }]] as const) { + await page.setContent(`${ELEMENT_PLUS_PAGE}`); + const target = await targetOf(kind, selector); + // Holds the page's main thread, so the read waits behind it until after the abort. + await page.evaluate(() => setTimeout(() => { const until = Date.now() + 400; while (Date.now() < until); }, 0)); + const controller = new AbortController(); + setTimeout(() => controller.abort(new Error('cancelled')), 100); + await expect( + dispatchLocatorAction(target, action, { timeoutMs: 2000, signal: controller.signal }, () => { + throw new Error('no second target'); + }), + ).rejects.toThrow('cancelled'); + expect(await page.isChecked(selector)).toBe(false); + } + }); +}); diff --git a/packages/web/tests/integration/read-node-checked.test.ts b/packages/web/tests/integration/read-node-checked.test.ts index e131fb4c53..cc279b73cc 100644 --- a/packages/web/tests/integration/read-node-checked.test.ts +++ b/packages/web/tests/integration/read-node-checked.test.ts @@ -70,6 +70,46 @@ describe('checked and selected', () => { expect(await page.getByRole('option').allTextContents()).toEqual(['Visible', 'Disabled', 'No visibility', 'Restored']); }); + it('lists a checkbox or radio its own label draws over no box as shown, with its checked state', async () => { + await page.setContent(` + + + + + `); + const nodes = await captureByTestId(); + expect(nodes.get('agree')).toMatchObject({ role: 'checkbox', name: 'Agree', states: { checked: true } }); + expect(nodes.get('large')).toMatchObject({ role: 'radio', name: 'Large', states: { checked: false } }); + for (const id of ['agree', 'large']) expect(nodes.get(id)?.states?.hidden).not.toBe(true); + expect(nodes.has('hidden-label')).toBe(false); + }); + + it('leaves out a box-less control no label of its own draws in a way a tap can click', async () => { + await page.setContent(` + + + + + + + + + + + + `); + const nodes = await captureByTestId(); + for (const id of ['invisible', 'text', 'skipped', 'contents', 'link', 'button-label', 'link-label', 'editable-label']) { + expect(nodes.has(id), id).toBe(false); + } + }); + it('applies the option limit to offered choices after hidden options are excluded', async () => { await page.setContent(``); const nodes = await captureByTestId(); diff --git a/packages/web/tests/integration/visibility.test.ts b/packages/web/tests/integration/visibility.test.ts index 39edf27acf..bef716364b 100644 --- a/packages/web/tests/integration/visibility.test.ts +++ b/packages/web/tests/integration/visibility.test.ts @@ -59,6 +59,7 @@ const PAGE = `
+