Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/check-sr-only-label.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 3 additions & 2 deletions docs/reference/screen.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
67 changes: 39 additions & 28 deletions packages/web/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (`<span>
* 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 (`<span> 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')) {
Expand All @@ -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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The pixel fallback accepts a label whose center lands on an ARIA checkbox, radio, or switch because INTERACTIVE omits those roles. Extend the interactive-control check so this path refuses those targets rather than clicking them.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/web/src/actions.ts, line 134:

<comment>The pixel fallback accepts a label whose center lands on an ARIA checkbox, radio, or switch because `INTERACTIVE` omits those roles. Extend the interactive-control check so this path refuses those targets rather than clicking them.</comment>

<file context>
@@ -122,37 +123,48 @@ function pickStandIn(node: Element, unrendered: string): HTMLLabelElement | null
+      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);
</file context>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

INTERACTIVE is the selector every label stand-in path on main already uses (the boxless control and the hit at the control point), not something this PR adds. Adding the ARIA checkbox, radio, and switch roles would change those paths for Element Plus and boxless controls too, so I have left it out of this fix. Happy to send it as a follow-up if you want it.

}
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);
Comment thread
abhinavapasok marked this conversation as resolved.
Resolved
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 };
}

/**
Expand Down
118 changes: 118 additions & 0 deletions packages/web/tests/integration/check-actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,22 @@ const ELEMENT_PLUS_CASES = [
['radio', '#large'],
] as const;

const VISUALLY_HIDDEN_PAGE = `<style>
.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border-width:0}
.visually-hidden{position:absolute!important;width:1px!important;height:1px!important;padding:0!important;margin:-1px!important;overflow:hidden!important;clip:rect(0,0,0,0)!important;white-space:nowrap!important;border:0!important}
label{display:inline-flex;align-items:center;gap:8px;cursor:pointer}
.track{width:44px;height:24px;border-radius:12px;background:#ccc}
</style>
<label><input type="checkbox" class="sr-only" id="toggle"><div class="track"></div><span>Email notifications</span></label>
<label><input type="checkbox" class="sr-only" id="plain"><span>Plain label</span></label>
<label><input type="radio" name="plan" class="visually-hidden" id="pro"><span>Pro</span></label>`;

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);
Expand Down Expand Up @@ -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(`<style>.sr-only{position:absolute;width:1px;height:1px;margin:-1px;overflow:hidden;clip:rect(0,0,0,0)}</style>
<label style="display:inline-block;width:120px;text-align:center"><input type="checkbox" class="sr-only" id="c"><a href="#went">Terms</a></label>`);
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(`<style>.sr-only{position:absolute;width:1px;height:1px;margin:-1px;overflow:hidden;clip:rect(0,0,0,0)}</style>
<label style="display:inline-block;padding:6px"><input type="checkbox" class="sr-only" id="c">I agree to the <a href="#went">Terms of Service and the Privacy Policy</a></label>`);
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(`<style>.sr-only{position:absolute;width:1px;height:1px;margin:-1px;overflow:hidden;clip:rect(0,0,0,0)}</style>
<div id="dialog" style="pointer-events:none"><label style="display:inline-block;width:140px;text-align:center"><input type="checkbox" class="sr-only" id="c"><a href="#went">Terms</a></label></div>
<script>setTimeout(() => document.getElementById('dialog').style.pointerEvents = '', 300)</script>`);
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(`<div style="width:200px;font:16px monospace">aaaaaaaaaaaaaaaaa
<input type="checkbox" style="display:none" id="c"><label for="c">ab cdefghijk</label></div>`);
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(`
<label for="c" style="display:none">C</label>
<input type="checkbox" id="c" style="width:1px;height:1px;margin:0">`);
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(`
<label style="display:inline-block;width:60px;height:20px">Pick<input type="radio" id="r"
Expand All @@ -322,6 +409,37 @@ describe.each(KINDS)('a control its own label covers, through the %s target', (k
}
});

it.each([
['in a page zoomed to 2', 'zoom:2', 'width:1px;height:1px'],
['in a page scaled by 2', 'transform:scale(2);transform-origin:0 0', 'width:1px;height:1px'],
])('checks and taps a visually hidden checkbox %s through its label', async (_, page_, size) => {
for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) {
await page.setContent(`<div style="${page_}"><label style="display:inline-flex;gap:8px"><input type="checkbox" id="c"
style="position:absolute;${size};margin:-1px;overflow:hidden;clip:rect(0,0,0,0)"><span>Agree</span></label></div>`);
expect(await perform(await targetOf(kind, '#c'), action)).toBeUndefined();
expect(await page.isChecked('#c')).toBe(true);
}
});

it('checks a Tailwind sr-only checkbox through a label set apart from it, at the label centre', async () => {
for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) {
await page.setContent(`<style>.sr-only{position:absolute;width:1px;height:1px;margin:-1px;overflow:hidden;clip:rect(0,0,0,0)}</style>
<input type="checkbox" class="sr-only" id="c"><span style="display:inline-block;width:80px"></span><label for="c">Agree</label>`);
expect(await perform(await targetOf(kind, '#c'), action)).toBeUndefined();
expect(await page.isChecked('#c')).toBe(true);
}
});

it('waits for the label of a Tailwind sr-only checkbox to take pointer events, then checks through it', async () => {
for (const action of [{ kind: 'check' }, { kind: 'tap' }] as const) {
await page.setContent(`<style>.sr-only{position:absolute;width:1px;height:1px;margin:-1px;overflow:hidden;clip:rect(0,0,0,0)}</style>
<div id="dialog" style="pointer-events:none"><label style="display:inline-flex;gap:8px"><input type="checkbox" class="sr-only" id="c"><span>Agree</span></label></div>
<script>setTimeout(() => document.getElementById('dialog').style.pointerEvents = '', 300)</script>`);
expect(await perform(await targetOf(kind, '#c'), action)).toBeUndefined();
expect(await page.isChecked('#c')).toBe(true);
}
});

it('checks a boxless checkbox through its first visible label, past a hidden one', async () => {
await page.setContent(`
<input type="checkbox" id="c" style="display:none">
Expand Down