Espresso "NoMatchingViewException" in CI
Espresso searched the current view hierarchy and no view matched your ViewMatcher, so it throws NoMatchingViewException. On CI this is often a timing issue: the screen under test was not shown yet, or a different screen was on top.
What this error means
An instrumented test fails with "androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: ..." and dumps the current view hierarchy. It can pass locally but fail on the CI emulator.
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found
matching: with id: com.example:id/login_button
View Hierarchy:
+--->DecorView{...}Common causes
The target screen was not displayed yet
A transition or async load had not completed, so the matched view was not in the hierarchy when Espresso looked.
The wrong activity/screen is on top
A dialog, splash, or a different destination is showing, so the expected view id is absent from the current hierarchy.
How to fix it
Idle on async work with an IdlingResource
- Register an IdlingResource for the async work that gates the screen.
- Espresso then waits for idle before matching the view.
- Avoid
Thread.sleep; use idling instead.
IdlingRegistry.getInstance().register(myIdlingResource);
onView(withId(R.id.login_button)).check(matches(isDisplayed()));Assert the right screen first
Verify the expected activity/destination is displayed before matching a child view, so a wrong-screen state fails clearly.
How to prevent it
- Use IdlingResources instead of fixed sleeps for async screens.
- Assert screen/activity state before matching child views.
- Keep matchers on stable view ids, not text that varies.