Android无线测试之—UiAutomator UiSelector API介绍之七

对象搜索—索引与实例

一、索引与实例说明:

1)index:在同一级中的编号,在兄弟类中组件的编号,index从0开始

2)instance:同一个布局中同一类组件的编号,instance从0开始

二、索引与实例属性定位对象

 
返回值 API 描述
UiSelector index(int index) 索引
UiSelector instance(int instance) 实例

 

 

 

 

 

 

三、API应用举例

package com.testuiselector;

import android.view.View;

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 Demo1 extends UiAutomatorTestCase {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String jarName, testClass, testName, androidId;
        jarName="demo1";
        testClass="com.testuiselector.Demo1";
        testName="testPackage";
        androidId="1";
        new UiAutomatorHelper(jarName, testClass, testName, androidId);
    }
    
    public void testClass() throws UiObjectNotFoundException{
        UiDevice.getInstance().pressHome();
        sleep(1000);
        
        //完全匹配
        UiSelector l1=new UiSelector().className("android.view.View").instance(3);
        UiObject clock1=new UiObject(l1);
        clock1.click();
        sleep(1000);
        
        UiDevice.getInstance().pressBack();
        sleep(1000);
        
        //正则匹配
        UiSelector l2=new UiSelector().classNameMatches(".*View").instance(4);
        UiObject clock2=new UiObject(l2);
        clock2.click();
        sleep(1000);
        
        UiDevice.getInstance().pressBack();
        sleep(1000);
        
        //class.getName()
        UiSelector l3=new UiSelector().className(View.class.getName()).instance(3);
        UiObject clock3=new UiObject(l3);
        clock3.click();
        sleep(1000);
        
        UiDevice.getInstance().pressBack();
        
    }
    
    public void testPackage() throws UiObjectNotFoundException{
    
        UiDevice.getInstance().pressHome();
        sleep(1000);
        
        UiSelector dl1=new UiSelector().packageName("com.android.deskclock");
        UiObject deskclock1=new UiObject(dl1);
        deskclock1.click();
        sleep(1000);
        
        UiDevice.getInstance().pressBack();
        sleep(1000);
        
        UiSelector dl2=new UiSelector().packageNameMatches(".*deskclock");
        UiObject deskclock2=new UiObject(dl2);
        deskclock2.click();
        
        
    }

}
Demo1.java

 

posted on 2015-06-06 13:07  发卡  阅读(508)  评论(0编辑  收藏  举报