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

获取列表子元素

一、相关API介绍

返回值 API 描述
UiObject getChildByDescription(UiSelector childPattern, String text, boolean allowScrollSearch) 是否允许滚动查看获取具备UiSelector条件元素集合后再以文本描述条件查找对象
UiObject getChildByDescription(UiSelector childPattern, String text) 默认滚动获取具备UiSelector条件元素集合后再以文本描述条件查找对象
UiObject getChildByInstance(UiSelector childPattern, int instance) 获取具备UiSelector条件的子集,再从子集中按照实例筛选想要的元素(不滚动)
UiObject getChildByText(UiSelector childPattern, String text, boolean allowScrollSearch) 是否允许滚动查看获取具备UiSelector条件元素集合后再以文本条件查找对象
UiObject getChildByText(UiSelector childPattern, String text) 默认滚动获取具备UiSelector条件元素集合后再以文本条件查找对象

 

 

 

 

 

 

 

 

二、API应用举例

package com.testuiselector;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
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="demo2";
        testClass="com.testuiselector.Demo";
        testName="testScrollGetChild";
        androidId="1";
        new UiAutomatorHelper(jarName, testClass, testName, androidId);

    }
    
    public void testScrollGetChild() throws UiObjectNotFoundException{
        UiDevice.getInstance().pressHome();
        sleep(1000);
        
        UiObject people=new UiObject(new UiSelector().text("People"));
        people.clickAndWaitForNewWindow();
        
        UiScrollable scroll=new UiScrollable(new UiSelector().className("android.widget.ListView"));
        
        //指定可以滚动查找子元素,可以成功找到在任何页中的电话信息
        UiObject H=scroll.getChildByText(new UiSelector().className("android.widget.TextView"), "H", true);
        H.click();
        
        //指定不可以滚动查找子元素,如果查找的电话信息不在第一页,会报错
        UiObject J=scroll.getChildByText(new UiSelector().className("android.widget.TextView"), "J", false);
        J.click();
        
        //getChildByInstance,不可以滚动,因此查找非第一页的电话信息会报错
        UiObject someone=scroll.getChildByInstance(new UiSelector().className("android.widget.TextView"), 20);
        someone.click();
    }

}
Demo.java

 

 

 

 

 

 

 

 

二、相关API应用举例

 

posted on 2015-06-23 18:01  发卡  阅读(656)  评论(0编辑  收藏  举报