UiCollection类介绍

一、UiCollection类说明

1)UiCollection类是UiObject类的子类,即UiObject类的所有方法都被UiCollection继承下来了,都可以使用

2)UiCollection代表元素条目的集合

二、UiCollection功能说明

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

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

三、UiCollection使用场景

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

2)一般用在需要找子类,且子类由于某些原因不好定位

3)获取某一类的数量,如获取联系人列表下当前试图下联系人的数量

四、相关API介绍:

1、从集合中查找对象:

1)相应API介绍:

返回值 API
UiObject getChildByText(UiSelector childPattern, String text)
UiObject getChildByDescription(UiSelector childPattern, String text)
UiObject getChildByInstance(UiSelector childPattern, int instance)

  在UiSelector选择器的查找条件中从子ui元素中搜索,递归搜索所有符合条件的子集。

  再次用文本/描述/实例条件从前面搜索子集定位到想要的元素。

2)参数说明

  childPattern   UiSelector从子元素的选择条件

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

3)返回值

  UiObject

4)抛出异常

  UiObjectNotFondException

5)API应用举例

package com.test.uicollection;

import android.view.KeyEvent;

import com.android.uiautomator.core.UiCollection;
import com.android.uiautomator.core.UiDevice;
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 Demo extends UiAutomatorTestCase { /**
* @param args
*/
public static void main(String[] args) {
String jarName, testClass, testName, androidID;
jarName="demo";
testClass="com.test.uicollection.Demo";
testName="testInstance";
androidID="1";
new UiAutomatorHelper(jarName, testClass, testName, androidID); } public void testText() throws UiObjectNotFoundException{
UiDevice.getInstance().pressHome();
sleep(2000);
UiObject apps=new UiObject(new UiSelector().description("Apps"));
apps.click();
sleep(2000);
UiObject fileManage=new UiObject(new UiSelector().text("File Manager"));
fileManage.click();
sleep(2000);
UiCollection collection=new UiCollection(new UiSelector().className("android.widget.ListView"));
UiSelector childPattern=new UiSelector().className("android.widget.TextView");
String text="Movies";
UiObject music=collection.getChildByText(childPattern, text);
music.click();
} public void testDesc() throws UiObjectNotFoundException{
UiDevice.getInstance().pressHome();
sleep(2000);
UiObject phone=new UiObject(new UiSelector().text("Phone"));
phone.click();
sleep(2000);
UiCollection collection=new UiCollection(new UiSelector().className("android.widget.TableLayout")); UiSelector childPattern=new UiSelector().className("android.widget.ImageButton");
String text="one"; UiObject button=collection.getChildByDescription(childPattern, text);
button.click();
sleep(500);
} public void testInstance() throws UiObjectNotFoundException{
UiDevice.getInstance().pressHome();
sleep(2000);
UiObject phone=new UiObject(new UiSelector().text("Phone"));
phone.click();
sleep(2000);
UiObject editText=new UiObject(new UiSelector().resourceId("com.android.dialer:id/digits"));
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_MOVE_END);
String text=editText.getText();
System.out.println("THE TEXT IS: "+text);
while(editText.getText()!=""){
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_DEL);
} UiCollection collection=new UiCollection(new UiSelector().className("android.widget.TableLayout"));
UiSelector childPattern=new UiSelector().className("android.widget.ImageButton");
UiObject one=collection.getChildByInstance(childPattern, 0);
UiObject zero=collection.getChildByInstance(childPattern, 10);
UiObject eight=collection.getChildByInstance(childPattern, 7);
UiObject six=collection.getChildByInstance(childPattern, 5); one.click();
sleep(500);
zero.click();
sleep(500);
zero.click();
sleep(500);
eight.click();
sleep(500);
six.click();
sleep(500); } }

Demo.java

2、获取某种搜索条件组件的数量
1)相应API介绍

public int getChildCount(UiSelector childPattern)

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

public int getChildCount()

仅直接查找符合条件的子类的数量(不涉及后代)

2)参数说明

childPattern  选择条件

3)返回值

int  符合条件的子子孙孙集合的数量

4)API应用举例

package com.test.uicollection;

import android.view.KeyEvent;

import com.android.uiautomator.core.UiCollection;
import com.android.uiautomator.core.UiDevice;
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 Demo extends UiAutomatorTestCase { /**
* @param args
*/
public static void main(String[] args) {
String jarName, testClass, testName, androidID;
jarName="demo";
testClass="com.test.uicollection.Demo";
testName="testCount";
androidID="1";
new UiAutomatorHelper(jarName, testClass, testName, androidID); } public void testCount() throws UiObjectNotFoundException{
UiDevice.getInstance().pressHome();
sleep(2000);
UiObject phone=new UiObject(new UiSelector().text("Phone"));
phone.click();
sleep(2000); //getChildCount(UiSelector childPattern) 递归查找后代中所有符合条件的元素的数量
UiCollection collection=new UiCollection(new UiSelector().className("android.widget.TableLayout"));
UiSelector childPattern=new UiSelector().className("android.widget.ImageButton");
int imageButtonCount=collection.getChildCount(childPattern);
System.out.println("ImageButtonCount="+imageButtonCount); //getChildCount() 仅查找子类中符合条件的元素数量
UiCollection tableCcollection=new UiCollection(new UiSelector().className("android.widget.TableLayout"));
int tableImageButtonCount=tableCcollection.getChildCount();
System.out.println("TableImageButtonCount="+tableImageButtonCount);
} }

Demo.java

Android无线测试之—UiAutomator UiCollection API介绍的更多相关文章

  1. Android无线测试之—UiAutomator UiScrollable API介绍三

    获取列表子元素 一.相关API介绍 返回值 API 描述 UiObject getChildByDescription(UiSelector childPattern, String text, bo ...

  2. Android无线测试之—UiAutomator UiObject API介绍六

    手势操作 1.手势相关操作 2.相关API介绍 返回值 API 描述 boolean performMultiPointerGesture(PointerCoords[]... touches) 执行 ...

  3. Android无线测试之—UiAutomator UiDevice API介绍八

    获取包名.开启通知栏.快速设置.获取布局文件的方法 一.包名.通知栏.快速设置.布局文件等相关知识: 1)包名:标示应用的符号,每个应用的名字 2)通知栏:从主界面的顶端向下拉,就可以打开通知栏 3) ...

  4. Android无线测试之—UiAutomator UiDevice API介绍四

    拖拽与滑动 一.概念介绍: 1)拖拽:将组建从一个坐标移动到另一个坐标 2)移动:从一二坐标点移动到另一个坐标点 3)步长:从一点滑动到另一点使用的时间 二.拖拽与滑动的相关API: 返回值 方法名 ...

  5. Android无线测试之—UiAutomator UiScrollable API介绍八

    设置滚动方向 一.设置滚动方向相关API 返回值 API 描述 UiScrollable setAsHorizontalList 设置滚动方向为水平滚动 UiScrollable setAsVerti ...

  6. Android无线测试之—UiAutomator UiScrollable API介绍七

    滑动到某个对象 一.滑动到某个对象相关API 返回值 API 描述 boolean scrollIntoView(UiSelector selector) 滑动到条件元素所在位置,并且尽量让其居于屏幕 ...

  7. Android无线测试之—UiAutomator UiScrollable API介绍六

    向前与向后滚动API 一.向前与向后滚动相关API 返回值 API 描述 boolean scrollBackward(int steps) 自动以步长向后滑动 boolean scrollBackw ...

  8. Android无线测试之—UiAutomator UiScrollable API介绍五

    滑动区域校准常量设置与获取 一.校准概念 校准常量指的是:滑动操作坐标时的偏移量,用来取偏移比例 二.相关API 返回值 API 描述 double getSwipeDeadZonePercentag ...

  9. Android无线测试之—UiAutomator UiScrollable API介绍四

    获取与设置最大滚动次数常量值 一.获取与设置最大滚动次数常量值相关API 返回值 API 描述 int getMaxSearchSwipes() 获取执行搜索滑动过程中的最大滑动次数,默认最大滚动次数 ...

随机推荐

  1. zabbix_get :command not found 解决办法

    zabbix_get 找不到命令是因为没有安装上zabbix_get ,解决办法: 1.yum list all |grep zabbix 返回一个列表,表中出现 zabbix-get.x86_84 ...

  2. MAC-Zsh安装与使用——终极Shell

    前言:Zsh可配置性强,用户可以自定义配置,个性化强.Zsh tab补全更强大,该功能可以让我们节约很多时间.Zsh 还有代码高亮功能,使得代码更好看了,显得逼格更高.Zsh 还有很多强大的功能,这里 ...

  3. 产品经理PM

    首先希望大家记住的就是,千万不要以为产品经理是什么高大上的光环,产品经理其实只是一种状态,一种心态而已. 大家可能看到BAT每年都会从校园里面招聘一些产品经理,尤其是我们腾讯,声称以产品为王,每年投产 ...

  4. Java成员变量与局部变量同名

    看到成员变量和局部变量同名这个知识点的时候一开始有点懵逼,想了一下其实特别简单. 先来看一个简单的代码. 首先我定义了一个Person类. public class Person { private ...

  5. 将XML格式的字符串封装成DOM对象

    在java端将字符串转化为xml对象可以使用DocumentHelper.parseText(xmlReturn).getRootElement(); 在js中同样有方法可以将字符串转化为xml对象, ...

  6. Android开发——跟随手指的小球实现

      今天要实现的是一个跟随手指的小球,说白了就是让小球按着手指滑动的轨迹运动,实现起来还是比较容易的. 用到的类是drawView,我们先自定义一个DrawView组件. DrawView.java: ...

  7. python-循环while

    while 只要…条件成立,就一直做…. for 循环会在可迭代的序列被穷尽的时候停止,while 则是在条件不成立的时候停止,因此 while 的作用概括成一句话就是:只要…条件成立,就一直做…. ...

  8. Springboot读取配置文件的两种方法

    第一种: application.yml配置中的参数: zip: Hello Springboot 方法读取: @RestController public class ControllerTest ...

  9. sql自动增长标识(转载)

    sql自动增长标识   对于一个设了自动增长标识的数据表来说,它的字段的值是由数据库自动设置的:这在导数据时很麻烦.   当我们导数据时,我们往往想想将标识字段的数据也导进来,怎么办呢?   方法有两 ...

  10. excel weekday

    weekday(日期值)=星期几 星期天是1 星期六是7