0

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?

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • Do you programatically inflate it with `R.id.rvWorkDaySchedule` id? I'm not quite sure about that one... – Shark Nov 30 '15 at 14:39
  • No, there is no R.id.rvWorkDaySchedule. I have a variable private RecyclerView rvWorkDaySchedule = new RecyclerView(this). And then I add it to the view hierarchy of the layout using flMainContent.addView(rvWorkDaySchedule) – Kaloyan Roussev Nov 30 '15 at 14:54
  • `R.id.*` refer to the auto-generated values found in the R file... If you're adding the view programatically, find it's parent via ID and perform the swipe on it instead? – Shark Nov 30 '15 at 16:12
  • Can I get a parent's child then? Because there are one or two swipable elements present in the same parent – Kaloyan Roussev Nov 30 '15 at 16:15
  • Yes, you just need to avoid the "by ID" lookup due to adding your view programatically (thus, it won't be in the R file, ergo it will be highlighted red in Android Studio). `findViewById(...).getChildAt(0).` should work fine. – Shark Nov 30 '15 at 16:18
  • espresso does not provide neither findViewById() nor getChildAt() - onView(withParent(R.id.main_content)). when I click control + space here there are no useful methods – Kaloyan Roussev Nov 30 '15 at 16:22
  • I'm sorry for misspeaking; I was referring to the casual `findViewById(...)` Java/Android way of finding (and obtaining) views/view references, and the `getChildAt(int id)` method which is available for all subclasses of `View`. Perhaps this link (http://stackoverflow.com/questions/24748303/selecting-child-view-at-index-using-espresso) could be of some assistance as they're exposing the `getChildAt(int)` call to Espresso. – Shark Nov 30 '15 at 16:26
  • Thanks Ill take a look at it and let you know what happened – Kaloyan Roussev Nov 30 '15 at 16:32

1 Answers1

2

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
Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94