Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android uiautomator to click ListView

I have an android app which uses the uiautomator to click the options in a listview. The ListView looks like this:

enter image description here

I am trying to click the Full Benchmark list item, but my code for it does not recognize the list item. This is what I have:

UiScrollable listView = new UiScrollable(new UiSelector().scrollable(
        true).className("android.widget.ListView"));

UiObject item1 = listView.getChildByText(new UiSelector()
.className(android.widget.TextView.class.getName()),
        "Full Benchmark");

item1.click();

I would appreciate any help!

like image 693
RagHaven Avatar asked Jun 07 '13 20:06

RagHaven


2 Answers

Here is what I use to find, and then click an item in a listview:

//Find and click a ListView item
public void clickListViewItem(String name) throws UiObjectNotFoundException {
    UiScrollable listView = new UiScrollable(new UiSelector());
    listView.setMaxSearchSwipes(100);
    listView.scrollTextIntoView(name);
    listView.waitForExists(5000);
    UiObject listViewItem = listView.getChildByText(new UiSelector()
            .className(android.widget.TextView.class.getName()), ""+name+"");
    listViewItem.click();
    System.out.println("\""+name+"\" ListView item was clicked.");
}

So in your case it would be

clickListViewItem("Full Benchmark")

Or:

UiScrollable listView = new UiScrollable(new UiSelector());
listView.setMaxSearchSwipes(100);
listView.scrollTextIntoView(name);
listView.waitForExists(5000);
UiObject listViewItem = listView.getChildByText(new UiSelector()
        .className(android.widget.TextView.class.getName()), "Full Benchmark");
listViewItem.click();
like image 54
Frank Charlton Avatar answered Oct 23 '22 05:10

Frank Charlton


There are two approaches for the above said thing.

Approach 1:

  1. Analyse UI and then get the instance of the list view say the list view containing text is the 3rd listview
  2. Create a Uicollection object like UiCollection dummyList = new UiCollection(new UiSelector().className("android.widget.ListView").index(3));
  3. Create a object for Benchmark UiObject dummyBenchmark = dummyList.getChildByText("Full Benchmark");

Approach 2:

new UiScrollable(
    new UiSelector().scrollable(true)
).scrollIntoView(
    new UiSelector().text("Full BenchMark")
);
new UiObject(
    new UiSelector().text("Full BenchMark")
).click();
like image 26
Sagar Kratin Avatar answered Oct 23 '22 05:10

Sagar Kratin