Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI Automator / Ui Automator2 : Scroll and find child UI Element in Recycler View

I want to acheive something like this.
Open Gmail app, look for a particular mail (scroll also) and click on it.
In the method below, I am able to search for a particular mail and click on it.

public void openMailWithParticularTitle(){
        UiObject2 obj =  Utils.getDeviceInstance().findObject(By.res("com.google.android.gm:id/recycler_list_view"));
        List<UiObject2> mails = obj.findObjects(By.clazz("android.view.View"));
        for(int i =0; i<mails.size();i++){
            if(mails.get(i).getContentDescription()!=null && mails.get(i).getContentDescription().contains("My Mail Link")){
                mails.get(i).click();
                break;
            }
        }
    }

But it only looks for visible items, doesn't scroll to look for child elements.
So I looked around and tried this, but this also doesn't seem to work due to some reasons.

 public void scrollMailWithParticularTitle2() throws UiObjectNotFoundException {
        openApp(Const.package_gmail_app,true);
        UiScrollable settingsItem = new UiScrollable(new UiSelector()
                .className("android.support.v7.widget.RecyclerView"));
        UiObject about = settingsItem.getChildByText(new UiSelector()
                .className("android.view.View"), "My Mail Link");
        about.click();
    }


Any help/ suggestion would be appreciated. (I've limited knowledge of UI testing)

like image 646
AnswerDroid Avatar asked Oct 27 '25 03:10

AnswerDroid


1 Answers

Try with scrollIntoView function:

Perform a scroll forward action to move through the scrollable layout element until a visible item that matches the selector is found

I used it once to find my app in apps menu:

UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject allAppsButton = mDevice.findObject(new UiSelector().description("Apps"));
allAppsButton.clickAndWaitForNewWindow();

UiScrollable appView = new UiScrollable(new UiSelector().scrollable(true));
appView.scrollIntoView(new UiSelector().text(APP_TITLE));
mDevice.findObject(new UiSelector().text(APP_TITLE)).clickAndWaitForNewWindow();
like image 76
robi24 Avatar answered Oct 29 '25 18:10

robi24