配置build.gradle(app目录下):
加入引用库
androidTestCompile'com.android.support.test:runner:0.5'
androidTestCompile'com.android.support.test:rules:0.5'
androidTestCompile'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
// androidTestCompile 'com.android.support:support-annotations:25.3.1'
新建测试类
@RunWith(AndroidJUnit4.class)
public class HelloWorld {
private UiDevice device;
String PACKAGE_NAME = "com.android.calculator2";
@Before
public void set Up () throws Exception {
device= UiDevice.getInstance ( InstrumentationRegistry.getInstrumentation( ) );
// 获取上下文
Context context = InstrumentationRegistry.getContext();
// 通过将包名传给包管理器获取启动intent
finalIntent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
// 启动应用
context.startActivity(intent);
// 等待应用启动
device.wait(Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)),10000);
}
@Test