如何测试 espresso android 由 sdk 提供的原生组件,例如android:id/search_close_btn

How to test espresso android native components provied by sdk e.g. android:id/search_close_btn

测试我们在 AppBar 中使用 android 本机搜索组件但 Espresso 无法从资源访问这些组件。例如 android.R.id.search_close_btn 未找到。知道如何测试吗?

onView(withId(android.R.id.search_close_btn)).perform(click)); 

onView(withId(android.R.id.search_src_text)).perform(click));

使用onView(withText("Clear query")).perform(click));,这将搜索带有内容描述的视图。

有时视图没有任何文本或 ID,但它有内容描述,如 "Press back"。在这种情况下,您可以像示例中那样使用 withContentDescription() Espresso 匹配器:

onView(withContentDescription("Press back")).perform(click());