uiautomator配置

添加依赖

在build.gradle中添加uiautomator依赖

1
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

编写用例

需要在androidTest中编写用例,而不是在main中。新建一个test类,下面是一个打开设置的一个测试。

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
package com.tt.demo;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class test {
static UiDevice device;

@Test
public void openSettings() throws InterruptedException {
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject2 settings = device.findObject(By.text("Settings"));
if(settings!=null){
settings.click();
Thread.sleep(2000);
}
}
}

测试类必须有@RunWith(AndroidJUnit4.class)运行器装饰,测试方法也必要有@Test装饰

安装APK

需要安装2个apk,一个是由main中编译出来的apk(傀儡应用),另一个则是由androidTest编译出来的apk。可以直接通过右侧的Gradle中的install直接安装

运行测试

打开命令提示符,输入以下指令即可开始进行测试

1
adb shell am instrument -w -e class com.tt.demo.test#openSettings com.tt.demo.test/androidx.test.runner.AndroidJUnitRunner

instrument参数参考