配置build.gradle(app目錄下):
加入引用庫(kù)
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'
新建測(cè)試類
@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();
//? ? ? ? 通過(guò)將包名傳給包管理器獲取啟動(dòng)intent
finalIntent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//? ? ? ? 啟動(dòng)應(yīng)用
context.startActivity(intent);
//? ? ? ? 等待應(yīng)用啟動(dòng)
device.wait(Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)),10000);
}
@Test