Skip to content
Latchkey

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.

Espresso
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

  1. Register an IdlingResource for the async work that gates the screen.
  2. Espresso then waits for idle before matching the view.
  3. Avoid Thread.sleep; use idling instead.
LoginTest.kt
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.

Frequently asked questions

What causes ""NoMatchingViewException""?
A transition or async load had not completed, so the matched view was not in the hierarchy when Espresso looked.
How do I fix "NoMatchingViewException"?
Idle on async work with an IdlingResource

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card