Skip to content

fix(mobile): stop reading every element of an iOS web page as focused - #2333

Closed
karolkulesza wants to merge 12 commits into
e2e-inc:mainfrom
karolkulesza:fix/ios-webview-focused
Closed

karolkulesza wants to merge 12 commits into
e2e-inc:mainfrom
karolkulesza:fix/ios-webview-focused

Conversation

@karolkulesza

@karolkulesza karolkulesza commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What

  • On iOS, elements of a page in a WebView no longer all read as focused while no field on the page has focus. Once a field takes the keyboard, it is the only focused node, as before.

Why

  • XCTest reports focused on every element of the page, siblings included, while the page holds the focus. So not.toBeFocused() failed on a button nobody touched, the model saw every web element as focused, and the keyboard-cover check skipped them all.

How it works

  1. pageFocusEchoes (packages/mobile/src/nodes.ts:797) counts the focused elements under each outermost web view, leaving out the web view nodes (Android marks the host focused beside its field). More than one means the focus is the page's, and projectSnapshot drops the flag from all of them (nodes.ts:696).

Issue

Fixes #2331

Verified

Ran it locally: yes, iPhone 17 Pro simulator on iOS 26.5, apps/mobile-benchmark, with focus assertions added to the WebView test (tests/flows-lists.e2e.ts).

main this PR
ASSERTION_FAILED: expect.not.toBeFocused failed on getByRole("button", name: "Apply coupon"), observed: states: focused passes; after fill, the coupon field is the only focused node
  • The agentic WebView scenario still replays from its committed recording (CI=1, 1 replayed). The iOS golden tree loses only its spurious [focused] marks.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md).
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.

View guided diff

Summary by CodeRabbit

  • Bug Fixes

    • Improved focus reporting in iOS web views. When no page element is focused, coupon-form elements are no longer all shown as focused; a genuinely focused field continues to be identified correctly.
    • Focus checks now better reflect the screen’s reported state across iOS and Android web views and native elements.
  • Documentation

    • Clarified that focus checks correspond to the model’s view of which element is focused.

While the page in a WebView holds the focus and none of its elements does,
XCTest reports every element of the page as focused. A web view with more
than one focused node below it now has none.

Fixes e2e-inc#2331
@github-actions github-actions Bot added the size:M Up to 99 changed lines, not counting tests, fixtures, translated docs, or the lockfile label Nov 26, 2026

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Automated review (AI triage pass), not yet read by a maintainer.

Reviewed 5d25b4724. The bug is real and the change is small and well tested, but it picks the real focus by counting flags rather than reading a signal that says which element has keyboard focus.

Must fix

  1. packages/mobile/src/nodes.ts:815 - "more than one focused node under a web view = echo" is a count heuristic, and it misses in both directions:

    • A page with a single accessible element (or one element left after a re-render) has an echo count of 1, so that element still reads [focused] and not.toBeFocused() still fails, which is the bug from #2331.
    • If two nodes ever report real focus at once (a focused field plus a focused ancestor, say), both lose it.

    The flag comes from agent-device's runner, which ORs two different XCTest attributes: focused = hasKeyboardFocus || hasFocus (RunnerAXSnapshotBridge.m:794 and RunnerTests+SnapshotAcquisition.swift:564 in agent-device 0.21.43). hasFocus is focus-engine focus and hasKeyboardFocus is text-input focus. Please dump both attributes for the WebView scenario before and after fill. If the echo is only hasFocus, fix it in agent-device instead: report keyboard focus on its own, or drop hasFocus outside tvOS. Then e2e reads one exact flag and no count is needed. If both attributes echo, say so in the PR body with the dump, and the maintainer can decide whether a heuristic stopgap is acceptable.

Nits

  • nodes.ts:806-808 - skipping nested web views doesn't change the output, only how many times the loop runs. Removing the check leaves all 125 nodes and captured-snapshot tests green, because an inner web view's subset adds nothing to the outer union. Either drop it or add a comment saying it exists to avoid repeated walks.

What checks out

  • Interplay with the keyboard-cover check from #1686 is right (nodes.ts:697): the exemption now uses the filtered flag, so echoed web elements under the keyboard can be hidden, while a field with real focus stays exempt.
  • Android is handled: web-view nodes are left out of the count, so a focused host plus its field still yields one real focus. Mutation-checked: counting the host fails the Android test.
  • Tests can fail: reverting the filter fails 2 new unit tests plus the webview-accessibility golden tree. iOS CI ran the new benchmark assertions green at e8a0577.
  • The golden tree update only drops the spurious [focused] marks. The patch changeset fits.

CI: check fails in apps/testbed web locator-frame.e2e.ts > should wait for frame to go (timeout on frameLocator("iframe") >> locator("button")). That is the web driver and has nothing to do with this diff, which only touches packages/mobile. The same job passed at e8a0577 with an identical PR diff, so this is a flake, not caused by this PR. Re-run.

@karolkulesza

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Pushed d8926a0.

  • Nit: dropped the nested web view skip, as it changed nothing.
  • Must fix: I haven't dumped hasFocus and hasKeyboardFocus separately yet, so I can't say which one echoes. agent-device's runner requests only hasFocus from the AX server (RunnerAXSnapshotBridge.m:364-376) and ORs it with hasKeyboardFocus into focused (line 794), and the merged flag is all the snapshot JSON keeps. I'll dump both for the WebView scenario before and after fill and put the result here. If only hasFocus echoes, the fix belongs in agent-device and this count goes away; if both echo, the dump goes in the PR body for the maintainer's call. Until then the count heuristic stays as a stopgap.
  • CI: the locator-frame failure looks like the flake you describe. The push starts a fresh run, since I can't rerun the old one.

@cubic-dev-ai cubic-dev-ai Bot left a comment

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.

0 issues found across 1 file (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Automated review (AI triage pass), not yet read by a maintainer.

Reviewed d8926a0ba. The nit is fixed: the nested web view skip is gone. The must-fix from the last pass is still open. The author says so and plans to dump the focus attributes first.

Must fix

  1. packages/mobile/src/nodes.ts:812 - this is still the count heuristic focused.length > 1, which guesses at the root cause. It fails both ways:

    • A page whose only accessible element (or only one left after a re-render) carries the echoed flag still reads [focused], which is the #2331 bug.
    • If two nodes ever hold real focus at once, both lose it.

    In agent-device 0.21.43 the runner asks the AX server only for hasFocus (RunnerAXSnapshotBridge.m:364-376). It then ORs that with hasKeyboardFocus into the one focused flag (RunnerAXSnapshotBridge.m:792-794, RunnerTests+SnapshotAcquisition.swift:564). As requested last round:

    • Dump both attributes for the WebView scenario before and after fill.
    • If only hasFocus echoes, fix it in agent-device (report keyboard focus separately, or drop hasFocus outside tvOS) and read that flag here.
    • If both echo, put the dump in the PR body so the maintainer can decide whether a stopgap is acceptable.

Nits

  • nodes.ts:788-795 - "Real focus is on one node" is the heuristic's assumption, stated as a fact. If a stopgap lands, the comment should say it is one and link the upstream issue.

Checked

  • nodes.test.ts + captured-snapshots.test.ts: 125/125 locally at d8926a0.
  • Mutation-checked: disabling the echo filter fails 3 tests (2 unit tests and the iOS webview-accessibility golden tree).

CI
The red checks are cancellations, not failures. In spec run 45899347703, every job (install (11)/(12), check, bun, all test jobs) was cancelled about 1s after it started, with no steps run and no logs. spec gate then failed because it requires every job to pass. The cause is the workflow's concurrency: ${{ github.workflow }}-spec-${{ github.ref }} with cancel-in-progress on pull_request. Attempt 2 of the previous head's run (45897411342, the requested re-run for the locator-frame flake) started at 21:36:52, one second before this push. The two runs shared the concurrency group, and this head's run was the one cancelled. There is no lockfile or build problem, and the PR is not at fault.

The spec suite has not run on this head. The diff from 5d25b47 (spec green on attempt 2) only removes the ancestor loop. Re-run spec run 45899347703 or push again.

@karolkulesza

Copy link
Copy Markdown
Contributor Author

Pushed 032f221.

  • Nit: the comment on pageFocusEchoes now says it is a stopgap, names the single-echoed-element gap, and says the proper fix is reporting keyboard focus separately in agent-device.
  • Must fix: I still don't have the hasFocus / hasKeyboardFocus dump. I tried patching the runner's RunnerAXSnapshotBridge.m to print both, but snapshots go through the separate compiled snapshot-bridge helper under ~/.agent-device/snapshot-source and a long-lived daemon that other simulators on this machine were using, so the patch never ran. I reverted it rather than rebuild the shared daemon. I'll take the dump on a clean machine state and post it here. Until then the count heuristic stays as the stopgap.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

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.

All reported issues were addressed across 1 file (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

Comment thread packages/mobile/src/nodes.ts Resolved
@karolkulesza

Copy link
Copy Markdown
Contributor Author

@okwasniewski the requested changes are in, ready for another look when you have time.

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Automated review (AI triage pass), not yet read by a maintainer.

Reviewed bebc836f3. The pushes since 032f22145 (18:23, 18:34, 18:49) are three merges of main and nothing else: git diff 032f22145 bebc836f3 on the five PR files is empty. The fix is still the count heuristic, so the must-fix from the last two passes stays open.

Must fix

  1. packages/mobile/src/nodes.ts:816 - focused.length > 1 still decides which nodes are echoes. The docblock now says it is a stopgap (nodes.ts:794-800), which fixes the earlier nit. The two misses are unchanged: a page with a single echoed element still reads [focused] (the #2331 bug), and two nodes with real focus would both lose it. The 17:13 reply says the live check shows only the coupon field flagged after fill, so that case works. That covers one page on one iOS version. It says nothing about which attribute echoes. What we asked for is still missing: a dump of hasFocus and hasKeyboardFocus for the WebView scenario, taken before and after fill. You can get it without touching the shared daemon by setting your own AGENT_DEVICE_STATE_DIR, which gives you a separate runner and snapshot helper, then rebuilding with the print patch. Then:
    • Only hasFocus echoes: fix it in agent-device (report keyboard focus on its own, or drop hasFocus outside tvOS), and e2e reads that one flag with no count.
    • Both echo: put the dump in the PR body and the maintainer decides whether a stopgap is acceptable.

What checks out

  • The stopgap comment is honest. It names the single-element gap and the upstream fix.
  • Interplay with the keyboard-cover check and the Android host-plus-field case is the same as in the last two passes, where both were mutation-checked.

CI: electron-windows fails in packages/electron launch.test.ts > dialogs > dismisses a dialog its handler leaves open... with "the app opened no window that loaded a page within 19750ms". This PR only touches packages/mobile, so the failure is not from this diff. spec gate is red only because that job failed. Re-run.

@karolkulesza

karolkulesza commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Still no hasFocus / hasKeyboardFocus dump, and I won't guess at one. What I can say from agent-device 0.21.37's runner source (RunnerAXSnapshotBridge.m):

  • The runner asks the AX server for hasFocus only (snapshotAttributes, line 374). hasKeyboardFocus is left out of the request on purpose, with a comment that the snapshot answers it anyway.
  • dictionaryForSnapshot (lines 757-759) reads both and writes focused = hasKeyboardFocus || hasFocus. The merged flag is all that reaches e2e.

So the one attribute the runner requests is hasFocus, which fits it being the page-level echo, but whether hasKeyboardFocus echoes too is only knowable from a dump. Taking that dump means rebuilding the runner and the compiled snapshot-bridge helper under a separate AGENT_DEVICE_STATE_DIR. I can't do that safely on this machine today, since the booted simulators are in use by other work.

Proposal, so this isn't blocked on me: keep the count as the documented stopgap, and I open an agent-device issue asking for keyboard focus as its own field. If you'd rather hold the PR until the dump exists, say so and I'll take it on a free machine.

Also merged main in; the electron-windows failure is in packages/electron, outside this diff. I can't rerun it (no admin rights); the branch update starts a fresh run.

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Automated review (AI triage pass), not yet read by a maintainer.

Reviewed 8b841f6cb. The only push since bebc836f3 is a merge of main (git diff bebc836f3 8b841f6cb on the five PR files is empty). The fix is still the count heuristic, so the must-fix from the last three passes is still open.

Must fix

  1. packages/mobile/src/nodes.ts:816 - focused.length > 1 still decides which nodes are echoes. It misses in both directions: a page whose only element is echoed still reads [focused] (the #2331 bug), and two nodes with real focus would both lose it. The 21:48 reply confirms the runner asks the AX server only for hasFocus and ORs it with hasKeyboardFocus. Nobody has dumped the two attributes separately yet. That dump is what decides where the fix goes:
    • Only hasFocus echoes: fix it in agent-device (report keyboard focus on its own, or drop hasFocus outside tvOS) and read that one flag here, with no count.
    • Both echo: put the dump in the PR body, and the maintainer decides on a stopgap.

CI: test (26, runner) fails in packages/e2e tests/integration/test-step.test.ts > test.step around a test timeout > cuts the call running in a step... (expected 'failed' to be 'timed-out'). That is the known main flake, and packages/e2e is outside this diff. spec gate is red only because of that job. Re-run.

For the maintainer

  • The author proposes landing the count as a documented stopgap and opening an agent-device issue for a separate keyboard-focus field, because they can't take the dump on their machine. Policy says root cause, not a heuristic, so this stays CHANGES unless you accept the stopgap. Another way to unblock it: someone with a free simulator takes the dump (own AGENT_DEVICE_STATE_DIR, runner + snapshot-bridge rebuilt with a print patch).

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: WebView focus projection

Layer / File(s) Summary
Detect and suppress page-level focus echoes
packages/mobile/src/nodes.ts
projectSnapshot identifies multiple focused descendants within a web view as page-focus echoes. It uses the adjusted focus value for projected state and keyboard coverage checks.
Verify platform focus behavior
packages/mobile/tests/unit/nodes.test.ts, packages/mobile/tests/fixtures/snapshots/ios/webview-accessibility.tree.txt, apps/mobile-benchmark/tests/flows-lists.e2e.ts, .changeset/ios-webview-focus.md
Tests cover page-wide and unique focus on iOS, native focus outside a web view, and Android web-view focus. The iOS snapshot removes focused markers from coupon-form descendants, and the changeset documents the patch.

Suggested reviewers: kdy1, okwasniewski, krzysztofmoch

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to bbc48

This change stops iOS WebView elements from all reading as focused when no field has focus, while a focused field stays focused. Unit and end-to-end tests cover the behavior. No merge-blocking risk was found. The heuristic is a documented stopgap, so a lone echoed element can still read as focused.

Security architecture risk: Minimal · up to bbc48

The change corrects how mobile WebView focus is reported and can make an existing key action tap the selected field first. The reviewed change does not grant new access or change who can act on the device.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • observed — The changed decision starts with focus flags in the captured mobile accessibility tree. Its direct scope is WebView subtrees with multiple flagged non-WebView descendants and the mobile consumers of their projected focus or visibility. Desktop and Electron implementations are unchanged in the full PR comparison.

Trust Boundaries and Controls

  • observed — The focus flag determines field preparation, not authorization: the optional tap uses the existing node binding, and the key is sent through the same device client. Direct input to the currently focused field does not consult the revised node flag.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 845f2832-1b94-42b1-aa2e-878f00657231
Commits

Reviewing files that changed from the base of the PR and between 7c1b2b2 and bbc48bc.

Files selected for processing: 5

  • .changeset/ios-webview-focus.md
  • apps/mobile-benchmark/tests/flows-lists.e2e.ts
  • packages/mobile/src/nodes.ts
  • packages/mobile/tests/fixtures/snapshots/ios/webview-accessibility.tree.txt
  • packages/mobile/tests/unit/nodes.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@karolkulesza

Copy link
Copy Markdown
Contributor Author

@okwasniewski the requested changes are in, ready for another look when you have time.

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Automated review (AI triage pass), not yet read by a maintainer.

Reviewed bbc48bcc5. The only push since the last review (8b841f6cb) is a merge of main. The PR's own patch is byte-identical at both heads, and on the five PR files the diff is empty apart from locate.test.ts, which came in from main. The 19:03 "requested changes are in" comment does not match the code, so the must-fix from the last four passes is still open.

Must fix

  1. packages/mobile/src/nodes.ts:816 - focused.length > 1 is still the count heuristic. A page whose only element carries the echoed flag still reads [focused] (the #2331 bug), and two nodes with real focus would both lose it. The hasFocus / hasKeyboardFocus dump for the WebView scenario (before and after fill) is still missing, and so is any mention of it in the PR body. Either:
    • post the dump and, if only hasFocus echoes, fix it in agent-device and read the one flag here, or
    • wait for the maintainer's answer to your 21:48 stopgap proposal (below). Until then, please don't mark this addressed.

CI: android deterministic fails 67 tests. Every one of them fails at the home-list lookup (getByLabel("Control Inventory. "): no node). Before the tests ran, the job warned that the app repack had failed and fell back to a native build, so this looks like the app/environment, not this diff. mobile gate is red only because of that job. Recent mobile.yml runs on other branches are green. Re-run.

For the maintainer

  • The author's 21:48 proposal is still waiting on you. They want to keep the count as a documented stopgap (nodes.ts:794-800) and open an agent-device issue asking for keyboard focus as its own field. The alternative is to hold the PR until the dump exists. Under the root-cause policy the default is to hold.

@karolkulesza

Copy link
Copy Markdown
Contributor Author

@okwasniewski the requested changes are in, ready for another look when you have time.

@kdy1

kdy1 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Note

This text was drafted with AI and posted after human approval.

  • Harness: T3 Code
  • AI agent: Claude Opus 5.5 (claude-opus-5-5)

We will not take the focused-count heuristic, so I am closing this PR.

The heuristic guesses which focus flag is real from the number of focused nodes. A page whose only element has the echoed flag still reads as focused, which is the bug in #2331. Two nodes that have real focus both lose it. The root cause is in agent-device, so a fix must start there.

When e2e reads the device wrong because of agent-device, we fix agent-device first and then bump the pin here. For example, I sent these fixes to agent-device:

I apply the same rule to my own PRs here. I moved these to draft, and they were closed, because they worked around agent-device in the engine instead of fixing the root cause:

Thank you for the investigation and the work on this PR.

@kdy1 kdy1 closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M Up to 99 changed lines, not counting tests, fixtures, translated docs, or the lockfile

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mobile (iOS): every element of a page in a WebView reads as focused while no field has focus

3 participants