Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropDown value selection using espresso android with dynamic element id's

PopupWindow$PopupViewContainer(@xxxxxxxx)
--ListPopupWindow$DropDownListView(@yyyyyyyy)
  --RelativeLayout(@zzzzzzz)
    ImageView
    TextView
  --RelativeLayout(@aaaaaaaa)
    ImageView
    TextView
  --RelativeLayout(@aaaaaaaa)
    ImageView
    TextView

I want to know how to access the the TextView in RelativeLayout 2 using espresso android automation, as @id is not present and values are assigned dynamically.

The above is the dropdown list and I want to click second choice.

e.g like when we search item in any search box we get list populated. And I want to click the second one in the list populated. All the element id's are dynamic.

like image 766
StackTrace Avatar asked Apr 03 '15 19:04

StackTrace


2 Answers

I was struggling with this issue and I found that you need to use a combination of withText to select the view and an option called RootMatchers.isPlatformPopup() which will try and find the matching text within views such as the autocomplete view and it is actually designed for this purpose.

It should look something like;

onView(withText("matching text"))
.inRoot(RootMatchers.isPlatformPopup())
.perform(click());
like image 121
arj Avatar answered Sep 21 '22 14:09

arj


You maybe be able to just do

onData(anything())
    .atPosition(1)
    .perform(click());

However, that assumes only one adapter view. If you have others, you'll need to somehow pick out that ListPopupWindow$DropDownListView.

I know you said all IDs are dynamic, but is there some ancestor view which you could pick out by ID? If so, you could do something like

onData(anything())
    .inAdapterView(isDescendantOfA(withId(someAncestorId)))
    .atPosition(1)
    .perform(click());

As a last resort, we could match on class name, but it would be a bit fragile:

onData(anything())
    .inAdapterView(withClassName(equalTo(
        "android.widget.ListPopupWindow$DropDownListView")))
    .atPosition(1)
    .perform(click());
like image 41
Daniel Lubarov Avatar answered Sep 18 '22 14:09

Daniel Lubarov