espresso no views in hierarchy found: how to fix
Fixes Espresso 'no views in hierarchy' errors: idling resources and view matchers. Use when Espresso cannot find views. Not for Appium.
TL;DR
Espresso cannot see the view because it has not appeared yet or the matcher is wrong. Register idling resources for async work, fix the matcher with Layout Inspector, and avoid sleep().
Error
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id "submit"Steps
- Check the view hierarchy dump in the error; is the view there at all? Expected: present means timing, absent means matcher or navigation.
- For timing, register an IdlingResource for the async work (network, DB). Expected: Espresso waits properly.
- For matcher issues, verify with Layout Inspector: the exact id, text, and hierarchy. Expected: the correct matcher.
- Replace any
Thread.sleep()with idling resources orisDisplayed()waits. Expected: no fixed sleeps. - Re-run. Expected: green.
When to use
NoMatchingViewExceptionin Espresso.- Async-heavy screens.
When not to use
- Appium tests (different framework).
- Views that genuinely do not exist (navigation bug).
Tool compatibility
- Espresso 3.x; AndroidX Test.
Variant phrasings
Espresso view not found
The short form; idling resources first.
NoMatchingViewException fix
The exception name; matcher vs timing.
Why it happens
Espresso syncs on the UI thread but not on your app's background work. Without idling resources, it checks too early.
Edge cases
- Animations keep the UI thread busy; disable them in tests.
- WebViews need separate handling; Espresso-Web exists for that.
- Custom idling resources must be unregistered to avoid leaks.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_1AP1L0c5GyjBJbWAJ5gWuA
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.