1. InstrumentationRegistry类

1.1. 类说明:

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

1.2 相关API

返回类型 API
static Bundle getArguments(): 返回一个instrumentation参数副本
static Context getContext():  返回instrumentation对应包的Context
static Instrumentation getInstrumentation(): 返回当前运行的instrumentation
static Context getTargetContext(): 返回一个目标应用程序的Context
static void

registerInstance(Instrumentation instrumentation, Bundle arguments):

记录或暴露当前instrumentation运行和instrumentation参数包的副本,存储在注册中心

1.3 简单示例

1.3.1 测试代码

 package com.test.tommyxie.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 tommyxie 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 运行结果

2. UiDevice新增API

2.1 API 介绍

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

2.2 代码示例

 package com.test.tommyxie.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 tommyxie 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); } }

原创:http://blog.csdn.net/swordgirl2011/article/details/50941555

Uiautomator 2.0之UiDevice新增API学习小记的更多相关文章

  1. NSData所有API学习

      www.MyException.Cn  网友分享于:2015-04-24  浏览:0次   NSData全部API学习. 学习NSData,在网上找资料竟然都是拷贝的纯代码,没人去解释.在这种网上 ...

  2. 从零开始搭建.NET Core 2.0 API(学习笔记一)

    从零开始搭建.NET Core 2.0 API(学习笔记一) 一. VS 2017 新建一个项目 选择ASP.NET Core Web应用程序,再选择Web API,选择ASP.NET Core 2. ...

  3. Uiautomator - 6.0 以上权限受限问题

    问题:在android studio中使用UiAutomator 2.0 编写测试用例时,要实现截图(非命令方式),写入文件时出现权限被拒绝的提示.例如: java.io.FileNotFoundEx ...

  4. Openstack api 学习文档 & restclient使用文档

    Openstack api 学习文档 & restclient使用文档 转载请注明http://www.cnblogs.com/juandx/p/4943409.html 这篇文档总结一下我初 ...

  5. Openstack python api 学习文档 api创建虚拟机

    Openstack python api 学习文档 转载请注明http://www.cnblogs.com/juandx/p/4953191.html 因为需要学习使用api接口调用openstack ...

  6. Windows录音API学习笔记(转)

    源:Windows录音API学习笔记 Windows录音API学习笔记 结构体和函数信息  结构体 WAVEINCAPS 该结构描述了一个波形音频输入设备的能力. typedef struct { W ...

  7. 框架源码系列十一:事务管理(Spring事务管理的特点、事务概念学习、Spring事务使用学习、Spring事务管理API学习、Spring事务源码学习)

    一.Spring事务管理的特点 Spring框架为事务管理提供一套统一的抽象,带来的好处有:1. 跨不同事务API的统一的编程模型,无论你使用的是jdbc.jta.jpa.hibernate.2. 支 ...

  8. Windows录音API学习笔记

    Windows录音API学习笔记 结构体和函数信息  结构体 WAVEINCAPS 该结构描述了一个波形音频输入设备的能力. typedef struct { WORD      wMid; 用于波形 ...

  9. Windows录音API学习笔记--转

    Windows录音API学习笔记 结构体和函数信息  结构体 WAVEINCAPS 该结构描述了一个波形音频输入设备的能力. typedef struct { WORD      wMid; 用于波形 ...

随机推荐

  1. windows 常用命令整合--脚本工具

    到年终了,手里活不多了,平时就想着将平时一些常用的命令整合一下,于是下面的一个小小脚本就出来了... 好了,直接上菜:(http://files.cnblogs.com/files/hsuchan/c ...

  2. TAQSkinScrollBar 类美化滚动条再讨论

    再说:TAQSkinScrollBar 类美化滚动条,http://www.138soft.com/?p=156  里面有人提到不可以滚动 滚动的改善方法: unit AQSkinScrollBar; ...

  3. webpack 打包一个简单react组件

    安装Webpack,并加载一个简单的React组件 全局的npm模块安装: npm install -g webpack 安装jsx-loader npm install --save-dev jsx ...

  4. java 随机获取国内IP

    /* * 随机生成国内IP地址 */ public static String getRandomIp(){ //ip范围 int[][] range = {{607649792,608174079} ...

  5. Oldboy-Homework-Week1

    关于Python全栈开发第一周所讲的一些回忆(会陆续添加) 一.一些简单的命令.概念 1.print(""):输出 2.变量 3.input():输入 4.while循环.if.e ...

  6. 做 Web 开发少不了这些的

    抱歉,似乎有些标题党了.最近做服务器的热备,整理了些李纳斯工具的适用方法.看看还有不错的. 基本命令 sleep 500 暂停 ctrl + z 暂停 progress & 后台运行 jobs ...

  7. linux 字符设备驱动写法

    字符设备,块设备书 一.register_chrdev_region, register_chrdev, misc_register misc device(杂项设备) 在 Linux 内核的incl ...

  8. CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)

    CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)   在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾 ...

  9. MySQL中EXPLAIN命令详解

    explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了: 如: expla ...

  10. Linux Shell 通配符、元字符、转义符【转帖】

    作者:程默 说到shell通配符(wildcard),大家在使用时候会经常用到.下面是一个实例: 1   1 2 3 4 [chengmo@localhost ~/shell]$ ls a.txt  ...