Android基于uiautomator测试(六) --UiCollection使用

  • UiCollection说明:

    • UiCollection是UiObject的子类
    • UiCollection代表元素的条目集合
  • Uicollection功能说明:

    • 先按照一定的条件枚举出容器类界面所有符合条件的子元素
    • 在从符合条件的元素再次通过一定的条件最终定位需要的组件(先找到一个合集,然后再找我们需要的元素)
  • 两大功能:

    • 从集合中获取对象
    • 获取某种搜索条件组建的数量
  • 使用场景:

    • 一般使用容器类组件作为父类
    • 一般使用在需要找子类且子类由于某种原因不好定位的场合
    • 获取某一类的数量,如获取联系人列表下当前视图下联系人的数量
  • 从集合中查找对象:

  • 第一个搜索:

  • 第一个条件为从子集中搜索的条件一,第二个条件为特定的描述,两次筛选

  • getChildByText():

public void testText() throws UiObjectNotFoundException {    
    UiDevice uiDevice = 
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    UiCollection uiCollection = new UiCollection(new 
UiSelector().className("android.widget.ListView"));    
    //首先找到TextView的集合    
    UiSelector childPattern = new 
UiSelector().className("android.widget.TextView");    
    //在集合中找到相应的TextView    
    String nameD = "Android";    
    UiObject Android = 
    uiCollection.getChildByText(childPattern, nameD);    
    Android.click();
 }
  • getChildByDescription():
    在这里插入图片描述
public void testDEscription() throws UiObjectNotFoundException {    
    UiDevice uiDevice = 
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    UiCollection uiCollection = new UiCollection(new 
UiSelector().resourceId("com.android.contacts:id/dialpad_layout"));    
    UiSelector uiSelector = new 
UiSelector().className("android.widget.RelativeLayout");    
    String text = "8按钮";    
    UiObject object = uiCollection.getChildByDescription(uiSelector, text);    
    object.click();
 }
  • getInstance():
public void testInstance() throws UiObjectNotFoundException {    
    UiDevice uiDevice = 
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    UiCollection uiCollection = new UiCollection(new 
UiSelector().resourceId("com.android.contacts:id/dialpad_layout"));    
    UiSelector uiSelector = new 
UiSelector().className("android.widget.RelativeLayout");    
    UiObject object1 = uiCollection.getChildByInstance(uiSelector, 0);    
    UiObject object4 = uiCollection.getChildByInstance(uiSelector, 10);    
    UiObject object44 = uiCollection.getChildByInstance(uiSelector, 10);    
    UiObject object3 = uiCollection.getChildByInstance(uiSelector, 7);   
    UiObject object5 = uiCollection.getChildByInstance(uiSelector, 11);   
    object1.click();    
    object0.click();    
    object00.click();    
    object8.click();    
    object6.click();
 }
  • 获取某种搜索条件组建的数量
public void testGetChildCount() throws UiObjectNotFoundException {    
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
     //整个界面的最顶级    
    UiCollection uiCollection = new UiCollection(new UiSelector().index(0));    
    //获取顶级界面下的TextView包含后代所有的    
    int textCount = uiCollection.getChildCount(new       
    UiSelector().className("android.widght.TextView"));    
    System.out.println(textCount);    
    UiCollection uiCollection1 = new UiCollection(new  
    UiSelector().resourceId("com.android.contacts:id/dialpad_layout"));    
    //只包含最上级的子类,不会获取后代子类    
    int tableChildCount = uiCollection1.getChildCount();    
    System.out.println("Wjx" + tableChildCount);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wjxbless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值