xcuitest failed to get matching snapshot: how to fix
Fixes XCUITest 'failed to get matching snapshot' errors: query staleness and hierarchy. Use when XCUITest queries fail on snapshots. Not for Appium.
TL;DR
XCUITest snapshots the accessibility hierarchy; queries run against a stale snapshot after UI changes. Re-query after transitions, wait for existence, and keep queries simple.
Error
Failed to get matching snapshot: No matches found for "loginButton"Steps
- Add
waitForExistence(timeout:)before the query. Expected: the UI settles first. - Re-fetch the element after every navigation or sheet dismissal; never reuse queries across transitions. Expected: fresh snapshots.
- Simplify the query: fewer predicates, stable identifiers. Expected: less to go stale.
- Check the hierarchy with
debugDescriptionprintout. Expected: you see what actually exists. - Re-run. Expected: green.
When to use
failed to get matching snapshotin XCUITest.- After screen transitions.
When not to use
- Appium XCUITest driver issues (different layer).
- Launch failures.
Tool compatibility
- XCUITest; Xcode 14/15.
Variant phrasings
XCUITest no matches found
The short form; re-query after transitions.
XCUITest stale query
The mechanism; snapshots go stale.
Why it happens
XCUITest resolves queries against a captured hierarchy snapshot. UI changes invalidate it, and old query objects do not refresh.
Edge cases
firstMatchis faster but riskier; use it only for unique elements.- Animations delay snapshot updates; wait for them.
- System alerts interrupt the hierarchy; handle them explicitly.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_1FEwqoxQLeA26SdWQ1mNng
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.