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.
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());
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With