Find element with certain text inside another element using UISelector query

I have the following code snippet and the screenshot attached.

String query = "new UiScrollable(new UiSelector().className(\"androidx.recyclerview.widget.RecyclerView\"))" +
                ".scrollIntoView(new UiSelector().text(\"Test Group\"))";
driver.findElementByAndroidUIAutomator (query).click ();

What I want is to find an element with the text “Test Group” using UISelector, but inside the RecyclerView only (not searching the whole app source). What I get is the element inside search field instead (not in the RecyclerView).

Please advice. I know that I can get all searched elements using findElements(By.id("name")). But I want to use UI selector in this case.

what problem now? does it start scrolling?

I’ll try to explain in another way.
Please look at the screenshot. I have 2 elements with the “Test Group” text:

  1. The first is inside the search field (has id search_src_text). This is what I typed to find the required group.
  2. The second is the search result which I need to get (has id name). It is located within the RecyclerView.

With my UIAutomator query I’m getting the first element (inside the search field). What I want is to get the element inside the RecyclerView (not counting the element inside search field at all).

I know that I can do it with findElements(By.id("name")), but I want to understand how can I query with UIAutomator inside one particular element (in my case it is RecyclerView).

All clear. You provide example of code. You did not mention what happen when you run this code.

Aleksei, as I said twice, when I run the code I click the element with text “Test Group” from the search field (first element which query found in the app sources). But I need to get it from my search results (which are located inside the recycler view). I can even make a video for you if you don’t understand :slight_smile:
And my question is how can I search for the element with UIAutomator not in the whole app sources, but in one particular element (container), in my case this recycler view.

interesting. try to add:

new UiSelector().resourceIdMatches(\".*name\").text(\"Test Group\")

It works, thanks. But I have one question:


If I need to scroll the search results to find the required group, this query will not work (NoSuchElementException).


Can I use something with UIScrollable and childSelector to achieve my goal?


I tried this monster of a query, but it of course doesn’t work:

String query = "new UiScrollable(new UiSelector().className(\"androidx.recyclerview.widget.RecyclerView\")).scrollIntoView(new UiSelector().className(\"androidx.recyclerview.widget.RecyclerView\").childSelector(new UiSelector().text(\"Test Group\"))))";

try:

new UiScrollable(new UiSelector().resourceIdMatches(\".*recycler_view").scrollable(true))
.scrollIntoView(new UiSelector().resourceIdMatches(\".*name\").text(\"Test Group\"))";

or

new UiScrollable(new UiSelector().scrollable(true))
.scrollIntoView(new UiSelector().resourceIdMatches(\".*name\").text(\"Test Group\"))";
1 Like

Thanks a lot!
How did you get all this knowledge with UIAutomator queries? :slight_smile:

Работа такая.

2 Likes

Согласен) Продолжу на инглише для красоты)

I have one more question. Sometimes the required search result can have the ID number instead of name (when I search for the number instead of group).

Can I have logical ‘OR’ in my query which will check if the element with the ID name OR number has the the required text ?

you may try appium annotations BUT! such (i mean UiScrollable(). scrollIntoView…) commands may work not reliable when they together.

    @AndroidFindAll(value = {
            @AndroidBy(uiAutomator = query_1),
            @AndroidBy(uiAutomator = query_2)
    })
    private List<MobileElement> elements;
1 Like