七、UiCollection API 詳細介紹

一、UiCollection類介紹

  • UiCollection類兩大功能:從集合中查找對象;獲取某種搜索條件組件的數量
1.UiCollection類說明

1)UiCollection是UiObject的子類,可以使用UiObject中的所有API
2)UiCollection代表元素條目組合

2.UiCollection功能說明

1)先按照一定的條件枚舉出容器類界面所有符合條件的子元素
2)符合條件的元素中再次通過一定的條件最終定位需要的組件

3.UiCollection使用場景

1)一般使用容器類組件作爲父類
2)一般使用在需要找子類且子類由於某些因素不好定位的情況
3)獲取某一類的數量,如獲取聯繫人列表下當前視圖下聯繫人的數量

 

二、從集合中查找對象

 
1.相關API說明
複製代碼
public UiObject getChildByDescription(UiSelector childPattern,String text)

public UiObject getChildByText(UiSelector childPattern,String text)

public UiObject getChildByInstance(UiSelector childPattern,int instance)

在UiSelector選擇器的查找裏條件從子ui元素中搜索,遞歸尋找早所有符合條件的子集。

再次用 描述/文本/實例 條件從前面搜索子集定位到想要的元素
複製代碼
 
2.參數:

 

Tables

Are

childPattern UiSelector從子元素中的選擇條件
text、instance 從搜索出的元素中再次用 描述/文本/實例 條件搜索元素
返回值: UiObject
拋出異常: UiObjectNotFoundException

 

  • 例如:
複製代碼
public void testDemo1() throws UiObjectNotFoundException{

    //getChildByText()[界面停留在文件管理器界面]
    //1.通過類名獲取所有組件的集合
    UiCollection collection=new UiCollection(new UiSelector().className("android.widget.ListView"));
    //2.通過類名對步驟1中的集合進行刪選
    UiSelector childPattern=new UiSelector().className("android.widget.TextView");
    //3.通過text最終選中music文件
    String text="Music";
    UiObject music=collection.getChildByText(childPattern, text);
    //4.點擊music按鈕
    music.click();


    //getChildByDescription()[界面停留在撥號界面]
    //1.通過resourceId找出所有的組件集合
    UiCollection collection1=new UiCollection(new UiSelector().resourceId("com.android.dialer:id/dialpad"));
    //2.通過類名篩選步驟1集合中的組件
    UiSelector childPattern=new UiSelector().className("android.widget.ImageButton");
    //3.通過text最終定位到要選的組件
    String text="四";
    UiObject forth=collection1.getChildByDescription(childPattern, text);
    //4.點擊
    forth.click();


    //getChildByInstance()[界面停留在撥號界面]
    //1.通過resourceId找出所有的組件集合
    UiCollection collection1=new UiCollection(new UiSelector().resourceId("com.android.dialer:id/dialpad"));
    //2.通過類名篩選步驟1集合中的組件
    UiSelector childPattern=new UiSelector().className("android.widget.ImageButton");
    //3.通過instance最終定位需要使用的組件(以10086爲例子)
    UiObject child1=collection1.getChildByInstance(childPattern, 0);
    UiObject child0=collection1.getChildByInstance(childPattern, 10);
    UiObject child01=collection1.getChildByInstance(childPattern, 10);
    UiObject child8=collection1.getChildByInstance(childPattern, 7);
    UiObject child6=collection1.getChildByInstance(childPattern, 5);
    //4.點擊
    child1.click();
    child0.click();
    child01.click();
    child8.click();
    child6.click();
}
複製代碼

 

三、獲取某種搜索條件組件的數量

 

1.相關API說明

 

public int getChildCount(UiSelector childPattern)
按照UiSelector查找條件遞歸查找所有符合條件的子子孫孫集合的數量

 

參數

childPattern

選擇條件

返回值 int 符合條件的子子孫孫集合數量
  • 例如:
複製代碼
public void testDemo2() throws UiObjectNotFoundException{

        //例一:int getChildCount()[界面停留在撥號界面]
        //1.搜索UiAutomatorView界面上最頂端的那個組件(界面上第一個index爲0的就是)
        UiCollection collection=new UiCollection(new UiSelector().index(0));
        //2.假設要找所有ImageButtom數量
        int count=collection.getChildCount(new UiSelector().className("android.widget.ImageButton"));
        //3.將結果輸出
        System.out.println("ImageButton conut is : "+count);



        //例二:如果tebleCollection.getChildCount()括號內不添加參數只找他的直接子類個數
        //1.搜索類名爲android.widget.TableRow的組件集合
        UiCollection tebleCollection=new UiCollection(new UiSelector().className("android.widget.TableRow"));
        //2.返回直接子類個數(直接子類是指當前子類下面的組件不返回)
        int tebleChildCount=tebleCollection.getChildCount();
        //3.輸出直接子類個數
        System.out.println("Android.widget.TableRow : "+tebleChildCount);
    }

發佈了1 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章