如何在 android 最新版本中以编程方式执行 UiAutomator 测试用例
How to execute UiAutomator test cases programatically in android latest versions
当我使用 adb shell 命令执行时,下面的代码工作正常。但是,如果我尝试使用 apk,它就无法正常工作。有帮助吗?
MainActivity.class
public void onStart() {
super.onStart();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString("Test", "value");
try {
startInstrumentation(new ComponentName("com.example.mypro.uiauto.test", "android.support.test.runner.AndroidJUnitRunner"),null, bundle);
} catch (Exception e) {
}
}
});
}
ExampleInstrumentedTest.class
@Test
public void pressHome() {
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
uiDevice.pressHome();
}
去年八月我遇到了同样的情况。到目前为止,您无法从 APK 触发插桩测试。这是 Android guide 的 link。因为在那种情况下,您也可以访问其他应用程序的 UI。这将是一个整体 android OS 中的安全问题,其中一个应用程序正在打开另一个应用程序并执行随机操作,这是一种威胁。
您可以通过命令 prompt/terminal 或仅通过 Android studio 执行插桩测试。
我分享的link,有一个标题为"Run UI Automator tests on a device or emulator"的专栏。
当我使用 adb shell 命令执行时,下面的代码工作正常。但是,如果我尝试使用 apk,它就无法正常工作。有帮助吗?
MainActivity.class
public void onStart() {
super.onStart();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString("Test", "value");
try {
startInstrumentation(new ComponentName("com.example.mypro.uiauto.test", "android.support.test.runner.AndroidJUnitRunner"),null, bundle);
} catch (Exception e) {
}
}
});
}
ExampleInstrumentedTest.class
@Test
public void pressHome() {
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
uiDevice.pressHome();
}
去年八月我遇到了同样的情况。到目前为止,您无法从 APK 触发插桩测试。这是 Android guide 的 link。因为在那种情况下,您也可以访问其他应用程序的 UI。这将是一个整体 android OS 中的安全问题,其中一个应用程序正在打开另一个应用程序并执行随机操作,这是一种威胁。
您可以通过命令 prompt/terminal 或仅通过 Android studio 执行插桩测试。 我分享的link,有一个标题为"Run UI Automator tests on a device or emulator"的专栏。