1.打开androidStudio 创建Android项目 最后一步选择empty
2.在build.gradle(module:app)的dependencies{}里加上以下
androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
3.修改dependencies v7版本号 可以不改但可能会报错compile 'com.android.support:appcompat-v7:23.0.1'
4.在项目的java目录下 有两个目录 同名 其中一个有(androidTest)在这个Test目录下 建一个类
public class ApplicationTest {
public static String TAG="lisa";
public UiDevice device=null;
public Context mContext = null;
@BeforeClass
public static void beforeClass(){
Log.d(TAG, "BeforeClass");
}
@AfterClass
public static void afterClass(){
Log.d(TAG, "AfterClass");
}
@Before
public void before() throws RemoteException {
Log.d(TAG, "Before");
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mContext = InstrumentationRegistry.getContext();
device.wakeUp();
}
@After
public void after(){
Log.d(TAG, "After");
}
@Test
public void test001() {
Log.d(TAG, "test001");
assertEquals(1,1);
}
@Test
public void test002() {
Log.d(TAG, "test002");
}
}
5.在工程目录下terminal执行gradle cC
6.如果此时连接收集 就会看到手机屏幕点亮
device.wakeUp();被执行
7./Users/lisa/Codes/UiTestProj/app/build/reports/androidTests/connected/index.html 为执行详细信息
8.由于向下兼容 uiautomator1.0的代码也可以执行