UiAutomator(4)--UiCollection API

来源:互联网 发布:经营网络棋牌室赚钱吗 编辑:程序博客网 时间:2024/06/05 05:15

UiAutomator(4)--UiCollection API

一、UiCollection介绍

       UiCollection是UiObject的子类,代表元素条目集合。

        UiCollection功能说明:

        1.先按照一定的条件枚举出容器类界面所有符合条件的子元素

        2.再从符合条件的元素再次通过一定的条件最终定位需要的组件

        UiCollection使用场景

        1.一般使用容器类组件作为父类

        2.一般使用在需要找子类且子类由于某些因素不好定位

        3.获取某一类的数量,如获取联系人列表下当视图下联系人的数量


二、API使用

        1.使用文本与描述条件从集合中查找对象API

            (1)public UiObject getChildByDescription (UiSeletor childPattern,String text)

            (2)public UiObject getChildByText (UiSeletor childPattern,String text)

            (3)public UiObject getChildByInstance (UiSelector childpattern,int instance)

               在UiSelector选择器的查找条件中从子ui元素中搜索,递归寻找所有符合条件的子集。再次用 描述/文本/实例 条件从前面搜索子集定位到想要的元素。

               childPattern:UiSelector 从子元素中的选择条件

               text、instance从搜索元素总再次用 描述/文本/实例条件搜索元素

         

         2.获取某种搜索条件组件的数量API 

            (1)public int getChildCount (UiSelector childpattern)

                按照UiSeletor查找条件递归查找所有符合条件的子子孙孙集合的数量

                参数:childPattern,选择条件

                返回值:int,符合条件的子子孙孙集合数量


三、实例

        

package cn.microinvestment.weitou;


import java.io.IOException;


import com.android.uiautomator.core.UiCollection;

import com.android.uiautomator.core.UiObject;

import com.android.uiautomator.core.UiObjectNotFoundException;

import com.android.uiautomator.core.UiSelector;

import com.android.uiautomator.testrunner.UiAutomatorTestCase;


public class UiCollectionDemo extends UiAutomatorTestCase {


public static void main(String[] args) throws IOException {

Runtime.getRuntime().exec("adb shell am start -n "

+ "cn.microinvestment.weitou/cn.microinvestment.weitou.activity.WTLoginControllerActivity");

new UiAutomatorHelper("uicollection", "cn.microinvestment.weitou.UiCollectionDemo", "test", "4");


}

public static void test() throws UiObjectNotFoundException{

//选择一个内容为abc的观点点击

UiCollection collection=new UiCollection(new UiSelector().className("android.widget.RelativeLayout"));

UiSelector childPattern=new UiSelector().className("android.widget.TextView");

String text="abc";

UiObject abc=collection.getChildByText(childPattern, text);  

abc.clickAndWaitForNewWindow();

}


}

0 0
原创粉丝点击