Loading...
Loading...
Use this skill to write Espresso 3.7.0 tests against Android Views — `onView(matcher).perform(action).check(matches(...))`, `onData(...)` for `AdapterView`, `RecyclerViewActions`, `Intents.intended/intending`, and `IdlingResource` registration. Covers the full `ViewMatchers` / `ViewActions` / `ViewAssertions` catalog, the `RootMatchers.DEFAULT` exclusion of dialogs and popups (the `inRoot(isDialog())` requirement), the `BottomNavigationViewActions` doesn't-exist gotcha in 3.7.0 contrib, and the `PickerActions.setDate(year, month, day)` 1-12 month convention. Use when the user reports `NoMatchingViewException`, `AmbiguousViewMatcherException`, `PerformException`, asks "how do I click in a dialog", "Espresso typeText doesn't fire", "intent stub for camera", or "Espresso vs UiAutomator".
npx skill4agent add skydoves/android-testing-skills writing-espresso-testscompose/../../../compose/interop/testing-with-espresso-interop/SKILL.mdonView(...).perform(...).check(...)RootMatchers.DEFAULTNoMatchingViewExceptionAmbiguousViewMatcherExceptionPerformExceptionAlertDialogRootMatchers.DEFAULTIntent.startActivityForResultIdlingResourceRecyclerViewBottomNavigationViewActionscompose/../../../compose/finders/finding-nodes-by-tag-text-content/SKILL.md../../../compose/interop/testing-with-espresso-interop/SKILL.mdInjectEventSecurityException../../uiautomator/cross-app-tests-with-uiautomator/SKILL.md../../runner/running-instrumented-tests-with-androidjunit4/SKILL.md../../scenarios/launching-activities-with-activityscenario/SKILL.md../../runner/running-instrumented-tests-with-androidjunit4/SKILL.mddocs/CORPUS.mddependencies {
androidTestImplementation("androidx.test.espresso:espresso-core:3.7.0")
androidTestImplementation("androidx.test.espresso:espresso-contrib:3.7.0") // RecyclerViewActions, PickerActions
androidTestImplementation("androidx.test.espresso:espresso-intents:3.7.0") // Intents.intended/intending
androidTestImplementation("androidx.test.espresso:espresso-accessibility:3.7.0")
androidTestImplementation("androidx.test.espresso:espresso-web:3.7.0") // WebView
implementation("androidx.test.espresso:espresso-idling-resource:3.7.0") // for production code that exposes IdlingResources
}
android.testOptions.animationsDisabled = true // hermeticActivityActivityScenarioActivityScenarioRuleFragmentScenarioonView(matcher)Matcher<View>withId(R.id.button)withTagKey(int)withResourceName(String)withTextwithContentDescriptionwithHintandroidx.test.espresso.matcher.ViewMatchersimport androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.*
import org.hamcrest.Matchers.allOf
onView(withId(R.id.submit_button))
onView(allOf(withText("Submit"), isEnabled()))
onView(withContentDescription("Open menu"))
onView(allOf(withId(R.id.subtitle), hasDescendant(withText("Hello"))))ViewMatchersisDisplayedisCompletelyDisplayedisDisplayingAtLeast(percent)isEnabledisNotEnabledisFocusedisCheckedisNotCheckedisSelectedwithEffectiveVisibility(Visibility.VISIBLE|INVISIBLE|GONE)withAlpha(float)withParenthasDescendantwithChildisRootwithClassNameisAssignableFrom(Class)hasSiblingisDescendantOfAViewActions.perform(action)androidx.test.espresso.action.ViewActionsimport androidx.test.espresso.action.ViewActions.*
onView(withId(R.id.email)).perform(typeText("test@example.com"), closeSoftKeyboard())
onView(withId(R.id.submit_button)).perform(click())
onView(withId(R.id.input)).perform(replaceText("new"), pressImeActionButton())
onView(withId(R.id.list)).perform(swipeUp())
onView(withId(R.id.scroller)).perform(scrollTo()) // requires single-direction ScrollViewclicklongClickdoubleClicktypeTextreplaceTextclearTextpressKeypressImeActionButtoncloseSoftKeyboardswipeUpswipeDownswipeLeftswipeRightscrollToandroidx.test.espresso.action.ViewActionsViewAssertions.check(...)androidx.test.espresso.assertion.ViewAssertionsimport androidx.test.espresso.assertion.ViewAssertions.*
import androidx.test.espresso.matcher.ViewMatchers.*
onView(withId(R.id.title)).check(matches(withText("Welcome")))
onView(withId(R.id.banner)).check(matches(isDisplayed()))
onView(withId(R.id.banner)).check(doesNotExist())
onView(withId(R.id.list)).check(selectedDescendantsMatch(withId(R.id.row), isCompletelyDisplayed()))matches(matcher)doesNotExist()matches(not(isDisplayed()))onView(...).perform(...).check(...)mainThreadExecutoruiController.loopMainThreadUntilIdle()MessageQueueIdlingResourceIdlingRegistryisIdleNow == trueLooperviewMatcherRootNoMatchingViewExceptionAmbiguousViewMatcherExceptionviewAction.getConstraints().matches(view)PerformExceptionviewAction.perform(uiController, view)viewAssertion.check(view, null)IdlingResourceExecutorServiceinRoot(...)RootMatchers.DEFAULTAlertDialogimport androidx.test.espresso.matcher.RootMatchers.*
onView(withText("Submit")).inRoot(isDialog()).perform(click())
onView(withText("Copy")).inRoot(isPlatformPopup()).perform(click()) // overflow menu, popup window
onView(withText("Saved")).inRoot(isPlatformPopup()).check(matches(isDisplayed())) // toastRootMatchers.DEFAULTallOf(hasWindowLayoutParams(), allOf(anyOf(allOf(isDialog(), withDecorView(hasWindowFocus())), isSubwindowOfCurrentActivity()), isFocusable()))RecyclerViewRecyclerViewActionsonView(...).perform(...)RecyclerViewimport androidx.test.espresso.contrib.RecyclerViewActions.*
onView(withId(R.id.list))
.perform(scrollToPosition<RecyclerView.ViewHolder>(20))
onView(withId(R.id.list))
.perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(3, click()))
onView(withId(R.id.list))
.perform(scrollTo<RecyclerView.ViewHolder>(hasDescendant(withText("Pikachu"))))
onView(withId(R.id.list))
.perform(actionOnItem<RecyclerView.ViewHolder>(hasDescendant(withText("Pikachu")), click()))RecyclerViewActionsisAssignableFrom(RecyclerView.class) AND isDisplayed()AdapterViewonData(...)onView(...)import androidx.test.espresso.Espresso.onData
import org.hamcrest.Matchers.allOf
onData(allOf(`is`(instanceOf(String::class.java)), `is`("Item 5")))
.inAdapterView(withId(R.id.list))
.perform(click())onDataIntents@Before@AfterIntentsRuleimport androidx.test.espresso.intent.rule.IntentsRule
import androidx.test.espresso.intent.Intents.*
import androidx.test.espresso.intent.matcher.IntentMatchers.*
@get:Rule val intentsRule = IntentsRule()
@Test
fun openCameraStub() {
intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE))
.respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, Intent()))
onView(withId(R.id.take_photo)).perform(click())
intended(allOf(
hasAction(MediaStore.ACTION_IMAGE_CAPTURE),
hasFlag(Intent.FLAG_GRANT_READ_URI_PERMISSION),
))
}IntentsTestRule@Deprecateddocs/CORPUS.mdIntentsRuleIntents.init()Intents.release()IdlingResourceAsyncTaskExecutorServiceclass CountingIdlingResource(private val name: String) : IdlingResource {
private val counter = AtomicInteger(0)
private var callback: IdlingResource.ResourceCallback? = null
override fun getName() = name
override fun isIdleNow() = counter.get() == 0
override fun registerIdleTransitionCallback(cb: IdlingResource.ResourceCallback) { callback = cb }
fun increment() { counter.incrementAndGet() }
fun decrement() { if (counter.decrementAndGet() == 0) callback?.onTransitionToIdle() }
}
@Before fun setUp() = IdlingRegistry.getInstance().register(myIdler)
@After fun tearDown() = IdlingRegistry.getInstance().unregister(myIdler)
// Master policy default is 60 seconds; setIdlingResourceTimeout default is 26 seconds.
// For debugger sessions, opt out of timeouts entirely with
// IdlingPolicies.setMasterPolicyTimeoutWhenDebuggerAttached(false)
// — there is no "-1 = infinite" magic value.
IdlingPolicies.setMasterPolicyTimeout(60, TimeUnit.SECONDS)
IdlingPolicies.setIdlingResourceTimeout(45, TimeUnit.SECONDS)increment()decrement():espresso-idling-resourceimplementationandroidTestImplementationNoMatchingViewException.inRoot(isDialog())AmbiguousViewMatcherExceptionallOf(...)PerformExceptionclick()isDisplayingAtLeast(90)InjectEventSecurityException// WRONG
onView(withText("Submit")).perform(click())
// WRONG because: RootMatchers.DEFAULT (line 51 of RootMatchers.java) excludes dialogs.
// The matcher does not see the dialog window. Result: NoMatchingViewException ("No views in
// hierarchy found matching: with text Submit"), even though the dialog is clearly on screen.// RIGHT
import androidx.test.espresso.matcher.RootMatchers.isDialog
onView(withText("Submit")).inRoot(isDialog()).perform(click())isPlatformPopup()BottomNavigationView// WRONG
import androidx.test.espresso.contrib.BottomNavigationViewActions
onView(withId(R.id.bottom_nav)).perform(BottomNavigationViewActions.selectItem(R.id.tab_profile))
// WRONG because: BottomNavigationViewActions does NOT exist in espresso-contrib 3.7.0.
// The class was historically documented but never shipped in 3.7.0's contrib jar (R3 lines 91-93).
// Compile fails with "unresolved reference".// RIGHT — click the underlying menu item view directly
import com.google.android.material.bottomnavigation.BottomNavigationView
import org.hamcrest.Matchers.allOf
onView(allOf(
withId(R.id.tab_profile),
isDescendantOfA(isAssignableFrom(BottomNavigationView::class.java)),
)).perform(click())PickerActions.setDate// WRONG
import androidx.test.espresso.contrib.PickerActions
onView(withClassName(equalTo(DatePicker::class.java.name)))
.perform(PickerActions.setDate(2026, 4, 15)) // intends "May 15, 2026"
// WRONG because: PickerActions.setDate uses 1-12 month convention (it subtracts 1 internally
// before calling DatePicker.updateDate which uses 0-11). Passing 4 sets April, not May.
// Source: R3 line 460, PickerActions.java line 49.// RIGHT — May = 5
onView(withClassName(equalTo(DatePicker::class.java.name)))
.perform(PickerActions.setDate(2026, 5, 15))Thread.sleep// WRONG
onView(withId(R.id.fab)).perform(click())
Thread.sleep(500) // wait for the FAB exit animation
onView(withId(R.id.fab)).check(doesNotExist())
// WRONG because: Thread.sleep is a smell. Espresso's idle pump already waits for animations
// driven through the main looper; if it doesn't, register an IdlingResource for the offending
// async path. See `docs/CORPUS.md` D.7 — "Thread.sleep is a smell".// RIGHT — disable animations OR register an IdlingResource
android.testOptions.animationsDisabled = true // for property animators driven via Choreographer
// And/or, for non-Looper waits:
IdlingPolicies.setMasterPolicyTimeout(45, TimeUnit.SECONDS)
IdlingRegistry.getInstance().register(myAnimationIdler)RecyclerViewimport androidx.test.espresso.contrib.RecyclerViewActions.*
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
onView(withId(R.id.list))
.perform(scrollTo<RecyclerView.ViewHolder>(hasDescendant(withText("Snorlax"))))
onView(withId(R.id.list))
.perform(actionOnItem<RecyclerView.ViewHolder>(hasDescendant(withText("Snorlax")), click()))scrollToactionOnItemRecyclerView.ViewHolderMatcher<View>.inRoot(isDialog())AlertDialog.inRoot(isPlatformPopup())RootMatchers.DEFAULTwithIdwithTagKeywithResourceNamewithTextThread.sleepIdlingResourceIdlingPolicies.setMasterPolicyTimeouttestOptions.animationsDisabled = trueandroidx.test.espresso.contrib.BottomNavigationViewActionsespresso-contrib:3.7.0isDescendantOfA(isAssignableFrom(BottomNavigationView::class.java))PickerActions.setDate(year, month, day)DatePicker.updateDateIntents.init()Intents.release()IntentsRuleintendedintending:espresso-idling-resource:3.7.0implementationandroidTestImplementationIntentsTestRule@DeprecatedIntentsRuleinitreleasetestOptions.animationsDisabled = trueSettings.GlobalNoMatchingViewExceptionRootgrep -r "Thread.sleep" src/androidTest/grep -r "BottomNavigationViewActions" src/androidTest/grep -r "IntentsTestRule" src/androidTest/IntentsRuleinRoot(isDialog())AlertDialog./gradlew :<module>:connectedDebugAndroidTestNoMatchingViewExceptionPerformExceptiontestOptions.animationsDisabled = trueandroidIdlingResourceIdlingRegistry.getInstance().unregister(...)@Afterandroidx/test/espresso/matcher/ViewMatchers.javawithIdwithTagKeywithResourceNamewithTextwithSubstringwithHintisDisplayedisEnabledisCheckedwithEffectiveVisibilitywithParenthasDescendantwithChildisRootisDescendantOfAandroidx/test/espresso/matcher/RootMatchers.javaDEFAULT = allOf(hasWindowLayoutParams(), allOf(anyOf(allOf(isDialog(), withDecorView(hasWindowFocus())), isSubwindowOfCurrentActivity()), isFocusable()))androidx/test/espresso/action/ViewActions.javaclicklongClickdoubleClicktypeTextreplaceTextclearTextpressKeyswipeUp/Down/Left/RightscrollTopressImeActionButtoncloseSoftKeyboardandroidx/test/espresso/assertion/ViewAssertions.javamatchesdoesNotExistselectedDescendantsMatchandroidx/test/espresso/contrib/RecyclerViewActions.javascrollToPositionscrollToactionOnItemAtPositionactionOnItemisAssignableFrom(RecyclerView) AND isDisplayedandroidx/test/espresso/contrib/PickerActions.javasetDate(year, monthOfYear, dayOfMonth)setTime(hours, minutes)androidx/test/espresso/IdlingPolicies.javasetMasterPolicyTimeoutsetIdlingResourceTimeoutsetMasterPolicyTimeoutWhenDebuggerAttachedandroidx/test/espresso/intent/Intents.javainitreleaseintendedintendingandroidx.test.espresso.intent.rule.IntentsRuletasks/research/R3-espresso.mddocs/CORPUS.mdRootMatchers.DEFAULT