thiesdiggity
thiesdiggity

Reputation: 1967

Android UIAutomation get the last child (UiObject) of dynamic View

I have a situation in which I need to get the "last child" of a dynamic View using Android's UIAutomation testing suite.

I have the following code but it's not working correctly:

UiSelector viewSelector = new UiSelector().resourceId("conversation_view").childSelector(new UiSelector().index(4));
UiObject conView = new UiObject(viewSelector);
//get the last child
UiObject lastChild = conView.getChild(new UiSelector().index(conView.getChildCount()-1));

Everything is working correctly except for the "lastChild" piece. The "getChildCount" is getting the correct number of children so I know the first part is retrieving the correct parent object.

It appears that its searching through "all" children but I only want it to search through the immediate children.

Anyone have any thoughts of how I can get the last "immediate" child from an UiObject?

Thanks!

Upvotes: 2

Views: 2628

Answers (1)

thiesdiggity
thiesdiggity

Reputation: 1967

For those of you whom are interested. The solution is to create a collection, which will give you a count of all the children. From there you can get the last child by using "getChildByInstance".

The following code appears to do the trick:

UiSelector viewSelector = new UiSelector().resourceId("conversation_container").childSelector(new UiSelector().scrollable(true));
//get the last child
UiCollection children = new UiCollection(viewSelector);
int childCount = children.getChildCount(new UiSelector().className("android.view.View"));
UiObject lastChild = children.getChildByInstance(new UiSelector().className("android.view.View"), childCount - 1);

Upvotes: 2

Related Questions