Uiautomator 2.0 - UiObject2

1. InstrumentationRegistry类

1.1. 类说明:

一个暴露的注册实例,持有instrumentation运行的进程和参数,还提供了一种简便的方法调用instrumentation, application context和instrumentation参数。

1.2 相关API

 

返回类型API
static BundlegetArguments(): 返回一个instrumentation参数副本
static ContextgetContext():  返回instrumentation对应包的Context
static InstrumentationgetInstrumentation(): 返回当前运行的instrumentation
static ContextgetTargetContext(): 返回一个目标应用程序的Context
static voidregisterInstance(Instrumentation instrumentation, Bundle arguments):记录或暴露当前instrumentation运行和instrumentation参数包的副本,存储在注册中心

1.3 简单示例

 

1.3.1 

 

package com.test.auto.hellouiautomator;  
  
import android.app.Instrumentation;  
import android.content.Context;  
import android.content.Intent;  
import android.net.Uri;  
import android.os.Bundle;  
import android.support.test.InstrumentationRegistry;  
import android.support.test.runner.AndroidJUnit4;  
import android.support.test.uiautomator.UiDevice;  
  
import org.junit.Before;  
import org.junit.Test;  
import org.junit.runner.RunWith;  
  
import java.io.IOException;  
  
/** 
 * Created by auto on 16/3/3. 
 */  
  
@RunWith(AndroidJUnit4.class)  
public class TestClass01 {  
    public UiDevice mDevice;  
    public Instrumentation instrumentation;  
  
    @Before  
    public void setUp(){  
        instrumentation = InstrumentationRegistry.getInstrumentation();  
        mDevice = UiDevice.getInstance(instrumentation);  
    }  
  
    @Test  
    public void testCase01() throws IOException {  
        //获取运行时的参数  
        Bundle args = InstrumentationRegistry.getArguments();  
        //输出到运行报告中  
        instrumentation.sendStatus(100, args);  
  
        //获取测试包的Context  
        Context testContext = InstrumentationRegistry.getContext();  
        //获取被测应用的Context  
        Context testedContext = InstrumentationRegistry.getTargetContext();  
  
        //通过Context来启动一个Activity,e.g.浏览器  
        String url = "https://www.baidu.com";  
        Intent i1 = new Intent(Intent.ACTION_VIEW);  
        i1.setData(Uri.parse(url));  
        i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        testContext.startActivity(i1);  
          
        //通过目标Context来启动拨号功能  
        Intent i2 = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + 10086));  
        i2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        testedContext.startActivity(i2);  
          
        Bundle inputBundle = new Bundle();  
        inputBundle.putString("key", "value");  
        //注入一个Bundle  
        InstrumentationRegistry.registerInstance(instrumentation, inputBundle);  
        //获取运行参数  
        Bundle outBundle = InstrumentationRegistry.getArguments();  
        //输出到结果报告中  
        instrumentation.sendStatus(110,outBundle);  
  
  
    }  
  
}  

  

 

1.3.2 运行结果
am instrument -w -r -e test 123 com.test.auto.hellouiautomator

 

 

2. UiDevice新增API

2.1 API 介绍

 

返回类型API
voiddumpWindowHierarchy(OutPutStream out): 获取当前页面层级到输出流
StringexecuteShellCommand(String cmd): 执行一个shell命令。备注:此方法只支持api21以上,手机需要5.0系统以上
UiObject2findObject(BySelector selector): 返回第一个匹配条件的对象
UiObjectfindObject(UiSelector selector): 返回一个匹配条件的代表视图的UiObject对象
List<UiObject2>findObjects(BySelector selector): 返回所有匹配条件的对象
<R> Rwait(SearchCondition<R> condition, long timeout): 等待的条件得到满足

2.2 代码示例

 

package com.test.auto.hellouiautomator;  
  
import android.app.Instrumentation;  
import android.os.Bundle;  
import android.os.Environment;  
import android.support.test.InstrumentationRegistry;  
import android.support.test.runner.AndroidJUnit4;  
import android.support.test.uiautomator.By;  
import android.support.test.uiautomator.UiDevice;  
import android.support.test.uiautomator.UiObject;  
import android.support.test.uiautomator.UiObject2;  
import android.support.test.uiautomator.UiObjectNotFoundException;  
import android.support.test.uiautomator.UiSelector;  
import android.support.test.uiautomator.Until;  
import android.widget.TextView;  
  
import org.junit.Before;  
import org.junit.Test;  
import org.junit.runner.RunWith;  
  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.OutputStream;  
import java.util.List;  
  
/** 
 * Created by auto on 16/3/3. 
 */  
  
@RunWith(AndroidJUnit4.class)  
public class TestClass01 {  
    public UiDevice mDevice;  
    public Instrumentation instrumentation;  
  
    @Before  
    public void setUp(){  
        instrumentation = InstrumentationRegistry.getInstrumentation();  
        mDevice = UiDevice.getInstance(instrumentation);  
    }  
  
    @Test  
    public void testCase01() throws IOException, UiObjectNotFoundException {  
  
        //dumpWindowHierarchy(OutPutStream out)  
        File file = new File(Environment.getExternalStorageDirectory()+File.separator+"dump.xml");  
        if(file.exists()){  
            file.delete();  
        }  
        file.createNewFile();  
        OutputStream outputStream = new FileOutputStream(file);  
        mDevice.dumpWindowHierarchy(outputStream);  
  
        //executeShellCommand(String cmd)  
        mDevice.executeShellCommand("am start -n com.tencent.mobileqq/.activity.SplashActivity ");  
  
        //findObject(BySelector selector)  
        mDevice.wait(Until.findObject(By.text("联系人")),2000);  
        UiObject2 uiObject2 = mDevice.findObject(By.text("联系人"));  
        uiObject2.click();  
  
        //findObject(UiSelector selector)  
        UiObject uiObject = mDevice.findObject(new UiSelector().text("短信"));  
        uiObject.click();  
  
        //findObjects(BySelector selector)  
        List <UiObject2> uiObject21 = mDevice.findObjects(By.clazz(TextView.class));  
        Bundle bundle = new Bundle();  
        for (UiObject2 a:uiObject21) {  
            bundle.putString("TextView", a.getText());  
        }  
        instrumentation.sendStatus(123,bundle);  
  
  
    }  
  
}  

  

  2  UiAutomator2 - UiObject2 

 

   
 
3 UiObject2 API
 
   
返回API [ 基础动作 ]说明
voidclear()清除编辑框里的内容
voidclick()点击一个对象
<R> RclickAndWait(EventCondition<R> condition, long timeout)点击一个对象然后等待在超时时间内条件成立则通过,否则抛出异常
voiddrag(Point dest)拖拽这个对象到指定位置
voiddrag(Point dest, int speed)自定义速度拖拽这个对象到指定位置,速度:像素数/秒
voidlongClick()执行一个长时间点击这个对象动作
booleanscroll(Direction direction, float percent)对该对象执行一个滚动动作
booleanscroll(Direction direction, float percent, int speed)自定义滚动速度对该对象执行一个滚动动作
voidlegacySetText(java.lang.String text)设置文本内容通过发送keycode
voidsetText(java.lang.String text)设置文本内容如果这个对象是一个可编辑的字段
   
返回API [ 手势 ]说明
voidpinchClose(float percent)对该对象执行关闭手势
voidpinchClose(float percent, int speed)自定义速度对该对象执行关闭手势
voidpinchOpen(float percent)对该对象执行打开手势
voidpinchOpen(float percent, int speed)自定义速度对该对象执行打开手势
booleanfling(Direction direction)对该对象执行一个滑动的手势,Direction代表从哪个方向作为起点
booleanfling(Direction direction, int speed)自定义速度对对象执行一个滑动的手势
voidswipe(Direction direction, float percent)执行一个滑动手势
voidswipe(Direction direction, float percent, int speed)执行一个滑动手势,可定义滑动速度
voidsetGestureMargin(int margin)以像素为单位设置边缘用于手势
voidsetGestureMargins(int left, int top, int right, int bottom)以像素为单位设置边缘用于手势
   
返回API [ 获取属性 ]说明
String getApplicationPackage()返回应用程序的包名称
StringgetClassName()返回对象的类名称
StringgetContentDescription()返回该对象的内容描述
StringgetResourceName()返回这个对象的完全限定的资源名的id
StringgetText()返回此对象的文本值
RectgetVisibleBounds()返回该对象的可见范围在屏幕坐标
PointgetVisibleCenter()返回一个指向这个对象的可见范围的中心
   
返回API [ 属性判断 ]说明
booleanisCheckable()返回此对象是否具有checkable属性
booleanisChecked()返回此对象是否具有checked属性
booleanisClickable()返回此对象是否具有clickable属性
booleanisEnabled()返回此对象是否具有enabled属性
booleanisFocusable()返回此对象是否具有focusable属性
booleanisFocused()返回此对象是否具有focused属性
booleanisLongClickable()返回此对象是否具有longclickable属性
booleanisScrollable()返回此对象是否具有scrollable属性
booleanisSelected()返回此对象是否具有selected属性
   
返回API [ 获取子元素 ]说明
UiObject2findObject(BySelector selector)搜索所有的元素在这个对象层级之下,并返回第一个对象与搜索条件相匹配
List<UiObject2>findObjects(BySelector selector)搜索所有的元素在这个对象层级之下,并返回所有对象与搜索条件相匹配
List<UiObject2>getChildren()返回这个对象下的直接子元素的集合
UiObject2getParent()返回该对象的父类
intgetChildCount()返回这个对象直属子元素的数量
booleanequals(java.lang.Object object)比较两个对象是否相等
inthashCode()获取对象的hashCode
booleanhasObject(BySelector selector)返回该对象是否存在
voidrecycle()回收这个对象
<R> Rwait(SearchCondition<R> condition, long timeout)等待的条件得到满足
<R> Rwait(UiObject2Condition<R> condition, long timeout)等待的条件得到满足
   

转载于:https://www.cnblogs.com/zsr0401/p/6277180.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值