uiautomator2.0的配置的两种方法

时间:2023-03-09 14:31:07
uiautomator2.0的配置的两种方法

方法一(使用在线下载的方式导入依赖):

1.首先创建项目工程,创建的项目的android_api版本要与测试的android_api版本一致(24就是24 ,不能26或者17去兼容)

2.然后就是将本地的SDK添加到AS中

2.1(添加SDK):

uiautomator2.0的配置的两种方法

2.2(添加SDK):

uiautomator2.0的配置的两种方法

3.在SDK下载内容

3.1:

uiautomator2.0的配置的两种方法

3.2:

uiautomator2.0的配置的两种方法

3.3:

uiautomator2.0的配置的两种方法

4.配置build.gradle

4.1.配置APP目录下的build

uiautomator2.0的配置的两种方法

4.2配置内容(添加配置标注的内容,其他内容不变动)

uiautomator2.0的配置的两种方法

4.3.配置文件

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
// testCompile 'junit:junit:4.12'
androidTestCompile('com.android.support.test:runner:0.5', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1' //这个需要依据android版本变化
compile 'com.android.support.constraint:constraint-layout:1.0.2'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'
}

5.同步依赖

uiautomator2.0的配置的两种方法

uiautomator2.0的配置的两种方法

方法二(手动导入依赖):

一、前期准备

 在此之前先将Android studio 环境安装搭建好,搭建好后接下来需要uiautomator2 jar包,这里就为大家提供了,
可以自行到这里下载 http://pan.baidu.com/s/1pJX6kiB 解压后里面文件有一下内容

uiautomator2.0的配置的两种方法

这些jar包是UiAutomator2的需要用到的jar包

二、开始建工程

打开Android Studio(一下统称AS),File——》new——》new project 如图

uiautomator2.0的配置的两种方法

出现以下界面

uiautomator2.0的配置的两种方法

根据自己需求修改参数 ,下一步

uiautomator2.0的配置的两种方法

下一步

uiautomator2.0的配置的两种方法

下一步

uiautomator2.0的配置的两种方法

完成

uiautomator2.0的配置的两种方法

好啦工程就弄好了 

可以下载到虚拟机看一下是否成功

第三步、配置UiAutomator2环境

首先导入包,将上面的lib.zip文件解压到工程目录下

uiautomator2.0的配置的两种方法

刷新工程就可以看到

uiautomator2.0的配置的两种方法

将jar包导入工程 ,选择所有jar包,点击右键,选择add as library

uiautomator2.0的配置的两种方法

出现小界面,选择App,点击OK

uiautomator2.0的配置的两种方法

现在就可以成功导入了 

我们可以去工程文件app目录下的build.gradle文件看到

uiautomator2.0的配置的两种方法

上面就是刚才加入的jar 

现在就可以进行uiautomator2 的测试程序编写了 

为了更好观察和编写,先进行切换显项

uiautomator2.0的配置的两种方法

uiautomator2.0的配置的两种方法

这样工程界面就简洁很多了 

我们可以看到上面红框,后缀android test,我们的测试工程就是在这个文件编写的

uiautomator2.0的配置的两种方法

点击那个路径,右键 new ——》java class

uiautomator2.0的配置的两种方法

出现创建新的类文件界面,写入名字后可以看到添加了一个

uiautomator2.0的配置的两种方法

点开uiaut文件,就可以在里面编写工程了 

下面是我写的一个工程

uiautomator2.0的配置的两种方法

代码如下

/**
* Created by LENOVO on 2016/1/21.
*/
@RunWith(AndroidJUnit4.class)
public class uiaut { UiDevice device;
Instrumentation instrumentation;
@Before
public void setUp(){ instrumentation = InstrumentationRegistry.getInstrumentation();
device = UiDevice.getInstance(instrumentation);
}
@Test
public void testSettext(){
UiObject2 sendMessage = device.findObject(By.res("com.android.mms:id/embedded_text_editor"));
sendMessage.setText("new set text");
sleep(2000);
sendMessage.clear();
sleep(2000);
sendMessage.setText("old set text");
} public void sleep(int mint){
try {
Thread.sleep(mint);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

这样工程就算完成了 

解析来还得要配置,更好的调试,这也是AS强大的地方 

Run——》Edit Configuration

uiautomator2.0的配置的两种方法

点击+, 选择Android Tests

uiautomator2.0的配置的两种方法

uiautomator2.0的配置的两种方法

差不多完成了,还需要配置一个地方

uiautomator2.0的配置的两种方法

需要在build.gradle文件defaultConfig下,加入这一句

testInstrumentationRunner ="android.support.test.runner.AndroidJUnitRunner"
  • 1

这样就可以啦

先打开虚拟器uiautomator2.0的配置的两种方法

启动虚拟器

uiautomator2.0的配置的两种方法

由于上面的测试工程是向短信输入框输入内容的,所以我先打开短信的发送界面 

一切准备好,那就运行了

uiautomator2.0的配置的两种方法

可以看设备有变化