码迷,mamicode.com
首页 > 移动开发 > 详细

Android无线测试之—UiAutomator UiCollection API介绍

时间:2015-06-15 18:26:42      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

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介绍

标签:

原文地址:http://www.cnblogs.com/fsw-blog/p/4563950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!