Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find view within hierarchy with Android Espresso

I have a dashboard with custom views.

I want to test that once the data is available, it properly gets displayed in the various text views.

My problem is: how do I select the various TextViews given they belong to a hierarchy.

Example: how to get current_month > sales > value view?

I know only how to do with a single hierarchy level but that does not help here:

onView(
  allOf(withId(R.id.value), 
  isDescendantOfA(withId(R.id.current_month))))

The hierarchy looks like:

+- RelativeLayout
|
|___+ CustomCardView (id = current_month)
|   |
|   |___+ CustomValueView (id = sales)
|   |   |
|   |   |___+ TextView (id = value)
|   |   |
|   |   |___+ TextView (id = unit)
|   |
|   |___+ CustomValueView (id = earnings)
|       |
|       |___+ TextView (id = value)
|       |
|       |___+ TextView (id = unit)
|
|___+ CustomCardView (id = last_month)
|   |
|   |___+ CustomValueView (id = sales)
|   |   |
|   |   |___+ TextView (id = value)
|   |   |
|   |   |___+ TextView (id = unit)
|   |
|   |___+ CustomValueView (id = earnings)
|       |
|       |___+ TextView (id = value)
|       |
|       |___+ TextView (id = unit)
|
|___+ CustomCardView (id = all_time)
    |
    |___+ CustomValueView (id = sales)
    |   |
    |   |___+ TextView (id = value)
    |   |
    |   |___+ TextView (id = unit)
    |
    |___+ CustomValueView (id = earnings)
        |
        |___+ TextView (id = value)
        |
        |___+ TextView (id = unit)
like image 955
Vincent Mimoun-Prat Avatar asked Feb 07 '17 16:02

Vincent Mimoun-Prat


2 Answers

Ok. Looks rather simple.

    onView(allOf(withId(R.id.value),
            isDescendantOfA(allOf(withId(R.id.sales),
                    isDescendantOfA(withId(R.id.current_month))))))

Is that the best way?

like image 93
Vincent Mimoun-Prat Avatar answered Nov 08 '22 23:11

Vincent Mimoun-Prat


Use withParent(Matcher) Hierarchy View Matcher:

onView(allOf(withId(R.id.value),
     withParent(allOf(withId(R.id.sales),
          withParent(withId(R.id.current_month)))),
  isDisplayed()));

Hope this helps.

like image 38
Akshay Mahajan Avatar answered Nov 09 '22 00:11

Akshay Mahajan