How to scroll to a particular element using UiAutomator classes

2,026 views
Skip to first unread message

Gaurav Dalal

unread,
Aug 28, 2018, 2:29:16 AM8/28/18
to Android Testing Support Library
Hi Team, 

I am using both UiAutomator support classes and espresso functions as my project is part react-native and rest android native. After a lot of struggle to automate the app on UI front, got it working using both the above support tools. But scrolling is still a problem!

My question is - How to scroll to a particular element on screen using UiAutomator and Espresso as well ?

In UIAutomator I'm using UiScrollable, UiObject etc but it deliberately scrolls to the bottom of the screen instead of an element.

Tried following ways: 

Using UiScrollable ----->>
try {
new UiScrollable(new UiSelector().scrollable(true))
.scrollIntoView(new UiSelector().resourceId("add_request_tv"));
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}

Using UiObject class ---->>
UiObject selectItemBTN = mdevice.findObject(new UiSelector()
.className("android.widget.ViewGroup")
.childSelector(new UiSelector()
.description("selectItem")
.index(0)
));

if (selectItemBTN.exists()) {
try {
selectItemBTN.click();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
}

Please suggest a solution for scrolling to a particular element on screen.

Thanks
Gaurav

Gaurav Dalal

unread,
Jan 23, 2019, 4:43:33 AM1/23/19
to Android Testing Support Library
back to the question, got the solution for this case... thought to post it here for others! Having no answer to you question hurts sometimes... so here it is!

There are different ways depending on usage:

1.  When you want to scroll to a particular index of an identifier. testData.getRoomTypeIndex() is just the index to which you . want to scroll, it could be 0, 1 etc...  
UiScrollable roomSelection = new UiScrollable(new UiSelector().scrollable(true).className("android.widget.ScrollView"));
UiSelector selectRoom = new UiSelector().childSelector(new UiSelector().description("selectRoom").instance(testData.getRoomTypeIndex()));

try {
roomSelection.scrollIntoView(selectRoom);
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}

2. When you just need to click on an element based on some text match, use this.
UiSelector pahFilter = new UiSelector().childSelector(new UiSelector().description("payMode_filter").childSelector(new UiSelector().textContains(payModeText)));

try {
mDevice.waitForIdle(3000);
UiObject filterPAH = mDevice.findObject(pahFilter);
filterPAH.click();
mDevice.waitForIdle(3000);
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}

There are multiple ways to find an element based on UI hierarchy. We could do it based on the requirement and based on where the view/element exists on the screen. Ui automator library is a powerful tool for android automation. 

This works perfectly on react-native screen, for android native the strings might need a tweak. 
Hope this helps when it comes to scrolling on the view!

Abhay Sood

unread,
Nov 10, 2022, 10:36:40 AM11/10/22
to Android Testing Support Library
How can we scroll to an element and click on it based on locater ?
Reply all
Reply to author
Forward
0 new messages