如何使用Android Intent以编程方式启动Instrumentation项目?

VIS*_*DIA 5 instrumentation android android-intent robotium android-activity

启动测试用例的一种方法是,

adb shell am instrument 
  -w com.google.vishal.test/android.test.InstrumentationTestRunner 
Run Code Online (Sandbox Code Playgroud)

我想用Android代码启动它(有意图)

例如,

adb shell am start -n com.google.android.contacts/.ContactsActivity
Run Code Online (Sandbox Code Playgroud)

我们可以通过以下方法使用Android intent运行: -

Intent intent = new Intent(com.google.android.contacts, ContactsActivity.class);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

但是,如何运行

adb shell am instrument 
  -w com.google.vishal.test/android.test.InstrumentationTestRunner 
Run Code Online (Sandbox Code Playgroud)

通过Android意图?

感谢您提前的帮助:-)

VIS*_*DIA 16

adb shell启动检测的命令: -

adb shell am instrument -w com.android.vishal.project.test/android.test.InstrumentationTestRunner   
Run Code Online (Sandbox Code Playgroud)

Android代码从Android Activity启动检测: -

 startInstrumentation(new ComponentName("com.android.vishal.project.test", "android.test.InstrumentationTestRunner"), null, null);
Run Code Online (Sandbox Code Playgroud)

注意 :

其他方法,

用于启动检测的Android代码(Android 2.3到Android 4.1.2)

String str_cmd = "adb shell am instrument -w com.android.vishal.project.test/android.test.InstrumentationTestRunner";
Runtime.getRuntime().exec(str_cmd);
Run Code Online (Sandbox Code Playgroud)

对于Android 4.2,它需要权限"android.permission.INJECT_EVENTS"并且只有系统应用程序允许.由于某些安全原因,用户应用程序无法使用此权限.

所以你不能使用Runtime.getRuntime().exec(str_cmd); 对于Android 4.2起......

所以现在的工作方法是:

 startInstrumentation(new ComponentName("com.android.vishal.project.test", "android.test.InstrumentationTestRunner"), null, null);
Run Code Online (Sandbox Code Playgroud)

从您的Activity执行此命令.

谢谢.