问题描述
我有这行Espresso测试代码:
I have this line of Espresso test code:
onView(withId(R.id.rvWorkDaySchedule)).perform(swipeDown());
并且rvWorkDaySchedule
在Android Studio的编辑器中以红色显示,因为布局中没有这样的XML视图ID-我以编程方式创建了此RecyclerView.
And rvWorkDaySchedule
is shown in red in the editor of Android Studio because there is no such XML view id in the layouts - I create this RecyclerView programmatically.
那么我如何检测被Espresso编程放大的视图?
So how do I detect views that have been inflated programmatically with Espresso?
推荐答案
首先,Espresso允许您在测试中使用Hamcrest匹配器.
First of all, Espresso allows you to use Hamcrest matchers in tests.
捕获以编程方式添加的视图最有用的是withChild
,withParent
,hasSibling
和hasDescendant
.
The most useful for catching the programmatically added views are withChild
, withParent
, hasSibling
, and hasDescendant
.
为了更清楚一点,我将在我的应用程序中举一个简单的示例:
To make it more clear, I would give a simple example from my app:
onView(withId(R.id.action_bar_details))
.check(matches(withChild(withChild(withText("Details")))));
第二,对于Espresso中的RecyclerView
测试,请使用onData
方法代替onView
.
Secondly, for RecyclerView
tests in Espresso use onData
methods instead onView
.
我的应用程序中的另一个示例-使用onData
方法
Another example from my app - using onData
method
onData(anything()).inAdapterView(withId(R.id.listView)).atPosition(getRandomPosition()).
onChildView(withId(R.id.item)).check(matches(isDisplayed()));
最后,检查这些出色的Google存储库以获取更多示例
Finally, check these great Googles repository for get more examples
- GoogleSample
- GoogleCodeLabs
这篇关于如何检测我在espresso中以编程方式创建的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!