如何检测我在espresso中以编程方式创建的视图

时间:2015-11-30 13:41:01

标签: android android-testing android-espresso

我有这一系列的Espresso测试代码:

    onView(withId(R.id.rvWorkDaySchedule)).perform(swipeDown());

并且rvWorkDaySchedule在Android Studio的编辑器中以红色显示,因为布局中没有这样的XML视图ID - 我以编程方式创建此RecyclerView。

那么如何检测使用Espresso以编程方式充气的视图?

1 个答案:

答案 0 :(得分:2)

首先,Espresso允许您在测试中使用Hamcrest匹配器。

Hamcrest 1.3 Quick Reference

对于捕获以编程方式添加的视图最有用的是withChildwithParenthasSiblinghasDescendant

为了更清楚,我会从我的应用程序中给出一个简单的例子:

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()));

最后,检查这些优秀的Googles存储库以获取更多示例

  1. GoogleSample
  2. GoogleCodeLabs
相关问题