How do I detect a view that I have created programmatically in espresso

别说谁变了你拦得住时间么 提交于 2020-01-07 03:54:07

问题


I have this line of Espresso test code:

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

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.

So how do I detect views that have been inflated programmatically with Espresso?


回答1:


First of all, Espresso allows you to use Hamcrest matchers in tests.

Hamcrest 1.3 Quick Reference.

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

Secondly, for RecyclerView tests in Espresso use onData methods instead onView.

Espresso 2.1. Espresso Cheat Sheet Master

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

Finally, check these great Googles repository for get more examples

  1. GoogleSample
  2. GoogleCodeLabs


来源:https://stackoverflow.com/questions/34000017/how-do-i-detect-a-view-that-i-have-created-programmatically-in-espresso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!