下载依赖库
- 进入https://github.com/,搜索android-testing
- 下载工程googlesamples/android-testing
- 工程中android-testing-master\ui\espresso\BasicSampleBundled\libs目录中就是需要的依赖库(不需要其中的android-support-v4.jar)
AndroidStudio配置
- 打开AndroidStudio,新建工程
- 切换到Project视图,新建libs文件夹,把刚才下载的依赖库拷贝到文件夹中,并添加到编译目录中
- 切换回Android视图,在测试包下新建测试类(例如:UiTestDemo)
- 编写测试类,代码如下:
package mqqzt.myuitest1;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import android.view.KeyEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Created by mqqzt-yang on 2015/11/21.
*/
@RunWith(JUnit4.class)
public class UiTestDemo {
@Test
public void testPress() throws RemoteException {
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
uiDevice.pressMenu();
uiDevice.pressKeyCode(KeyEvent.KEYCODE_VOLUME_DOWN);
uiDevice.pressRecentApps();
uiDevice.pressHome();
}
}
- 编辑运行器
- build.gradle文件defaultConfig代码块增加运行器
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"