能否在Android Instrumented Test中设置ConstraintLayout的可点击与启用状态?
Absolutely! Both Espresso and UiAutomator have you covered here—you can easily set those view properties before triggering the click in your waitressCallContainer_click_successResponse_buttonChangeState test method. Let’s walk through how to implement each approach:
Using Espresso
Espresso lets you interact directly with View instances via custom ViewActions, which is perfect for modifying properties like isClickable and isEnabled. Here’s how to do it:
// First, set waitressCallContainer to clickable onView(withId(R.id.waitressCallContainer)) .perform(object : ViewAction { override fun getConstraints(): Matcher<View> { // Ensure we're targeting a ConstraintLayout return isAssignableFrom(ConstraintLayout::class.java) } override fun getDescription(): String { return "Set ConstraintLayout to clickable state" } override fun perform(uiController: UiController?, view: View?) { (view as ConstraintLayout).isClickable = true } }) // Then, set waitressCallViewCircle to enabled onView(withId(R.id.waitressCallViewCircle)) .perform(object : ViewAction { override fun getConstraints(): Matcher<View> { // Ensure we're targeting an ImageView return isAssignableFrom(ImageView::class.java) } override fun getDescription(): String { return "Set ImageView to enabled state" } override fun perform(uiController: UiController?, view: View?) { (view as ImageView).isEnabled = true } }) // Now trigger the click as intended onView(withId(R.id.waitressCallContainer)).perform(click())
A quick note: If you run into issues with the view not being immediately available, you can add a check to ensure it’s displayed first, like .check(matches(isDisplayed())) before calling perform().
Using UiAutomator
UiAutomator works at a higher level, interacting with the device’s UI directly. You can use it to locate your views and modify their properties with just a few lines of code:
val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) // Locate the ConstraintLayout by resource ID (replace with your app's package name) val waitressCallContainer = device.findObject( UiSelector().resourceId("com.your.app.package:id/waitressCallContainer") ) // Set it to clickable waitressCallContainer.clickable = true // Locate the ImageView val waitressCallViewCircle = device.findObject( UiSelector().resourceId("com.your.app.package:id/waitressCallViewCircle") ) // Set it to enabled waitressCallViewCircle.enabled = true // Now perform the click waitressCallContainer.click()
If for some reason the direct property setters don’t work (rare, but possible), you can fall back to executing a JavaScript snippet to modify the view’s state:
waitressCallContainer.executeScript("this.isClickable = true;") waitressCallViewCircle.executeScript("this.isEnabled = true;")
Which Should You Choose?
- Espresso is ideal if your test is focused solely on your app—it integrates tightly with Android’s testing framework, has built-in synchronization, and is more concise for in-app interactions.
- UiAutomator is better if you need cross-app functionality or more low-level device interactions, though it requires a bit more setup (like specifying the correct package name).
内容的提问来源于stack exchange,提问作者Alexei

