如何检测我在 espresso 中以编程方式创建的视图
How do I detect a view that I have created programmatically in espresso
我有这行 Espresso 测试代码:
onView(withId(R.id.rvWorkDaySchedule)).perform(swipeDown());
并且 rvWorkDaySchedule
在 Android Studio 的编辑器中显示为红色,因为布局中没有这样的 XML 视图 ID - 我以编程方式创建此 RecyclerView。
那么如何检测使用 Espresso 以编程方式膨胀的视图?
首先,Espresso 允许您在测试中使用 Hamcrest 匹配器。
对于捕获以编程方式添加的视图最有用的是 withChild
、withParent
、hasSibling
和 hasDescendant
。
为了更清楚,我想举一个来自我的应用程序的简单示例:
onView(withId(R.id.action_bar_details))
.check(matches(withChild(withChild(withText("Details")))));
其次,对于 Espresso 中的 RecyclerView
测试,使用 onData
方法代替 onView
。
Espresso 2.1. Espresso Cheat Sheet Master
我的应用中的另一个示例 - 使用 onData
方法
onData(anything()).inAdapterView(withId(R.id.listView)).atPosition(getRandomPosition()).
onChildView(withId(R.id.item)).check(matches(isDisplayed()));
最后,查看这些很棒的 Google 存储库以获取更多示例
我有这行 Espresso 测试代码:
onView(withId(R.id.rvWorkDaySchedule)).perform(swipeDown());
并且 rvWorkDaySchedule
在 Android Studio 的编辑器中显示为红色,因为布局中没有这样的 XML 视图 ID - 我以编程方式创建此 RecyclerView。
那么如何检测使用 Espresso 以编程方式膨胀的视图?
首先,Espresso 允许您在测试中使用 Hamcrest 匹配器。
对于捕获以编程方式添加的视图最有用的是 withChild
、withParent
、hasSibling
和 hasDescendant
。
为了更清楚,我想举一个来自我的应用程序的简单示例:
onView(withId(R.id.action_bar_details))
.check(matches(withChild(withChild(withText("Details")))));
其次,对于 Espresso 中的 RecyclerView
测试,使用 onData
方法代替 onView
。
Espresso 2.1. Espresso Cheat Sheet Master
我的应用中的另一个示例 - 使用 onData
方法
onData(anything()).inAdapterView(withId(R.id.listView)).atPosition(getRandomPosition()).
onChildView(withId(R.id.item)).check(matches(isDisplayed()));
最后,查看这些很棒的 Google 存储库以获取更多示例