SoFunction
Updated on 2025-03-04

Share experience in Android automation testing In-depth UiScrollable

UiScrollable is a UiCollection and provides support for searching for items in a scrollable user interface (UI) elements. This class can be used with horizontally or vertically scrollable controls.

UiScrollable is a UiCollection (this thing is not understood yet), we can use it to find the control (item) we want on a swipable page (slide horizontally or up and down).

Example 1:The scrollClickObject method below is to use UiScrollable to automatically select the control we want to click on.

Copy the codeThe code is as follows:

public void scrollClickObject(String targetClassName,String targetName) throws UiObjectNotFoundException {
    UiScrollable collectionObject = new UiScrollable(new UiSelector().scrollable(true));
    if(()) {
        UiObject scrollableObject = (new UiSelector().className(targetClassName), targetName);
        ();
    } else {
        UiObject targetObject = new UiObject(new UiSelector().className(targetClassName).text(targetName));
        ();
    }
}

First, define a UiScrollable object. The only condition for identifying this object is that there are sliding controls on the screen. (There is a problem here, that is, if there are 2 sliding controls on the screen at the same time, an error will be reported)

Then we determine whether this sliding object exists

•Exist, use the getChildByText method to get the control we want to click on, and then click on it
•If it does not exist, it means that the page is not sliding, that is, all controls are displayed on the interface. Then we can use UiObject to get the control and operate it directly.