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

对象搜索—类名与包名

一、类名属性定位对象

返回值 API 描述
UiSelector calssName(String className) 完整类名匹配
UiSelector calssNameMatches(String regex) 正则类名匹配

搜索条件的快速书写方式

1)组件名字.class.getName方式

2)完整类名方式:android.widget.LinearLayout

3)正则方式

4)常量方式

二、包名属性定位对象

返回值 API 描述
UiSelector packageName(String name) 包名完整匹配
UiSelector packageNameMatches(String name) 正则包名匹配

注意:使用包名对象一般用来获取的是点击的父类的第一个子元素

三、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="testClassAndPackage";
        androidId="1";
        new UiAutomatorHelper(jarName, testClass, testName, androidId);
    }

    public void testClassAndPackage() 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();
        
    }

}
Demo1.java
原文地址:https://www.cnblogs.com/fsw-blog/p/4556474.html