UIAutomator根据索引点击listview

7 android listview android-uiautomator

我正在尝试使用一般方法实现UIAutomator测试用例来执行ListView项目的单击(无论持有listitem的视图组的类型如何).

目前我有以下代码,但它一直点击第一项.

public void clickListViewItem(int index) throws UiObjectNotFoundException {
   UiObject listview = new UiObject(new UiSelector().className("android.widget.ListView"));
   if(index <= listview.getChildCount()){
      listview.getChild(new UiSelector().index(index)).click();
   }else{
       throw new UIObjectNotFoundException("Index is greater than listSize");
   }
}
Run Code Online (Sandbox Code Playgroud)

小智 11

我使用以下代码,它基于UISelector的可点击属性:

listview.getChild(new UiSelector().clickable(true).index(index)).click();
Run Code Online (Sandbox Code Playgroud)