Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start Instrumentation project programmatically using Android Intent?

one way to start testcase is,

adb shell am instrument 
  -w com.google.vishal.test/android.test.InstrumentationTestRunner 

i want to start this using Android code (with intent)

for example,

adb shell am start -n com.google.android.contacts/.ContactsActivity

we can run using Android intent by following method :-

Intent intent = new Intent(com.google.android.contacts, ContactsActivity.class);
startActivity(intent);

But, how to run

adb shell am instrument 
  -w com.google.vishal.test/android.test.InstrumentationTestRunner 

by Android intent ?

Thanks for your help in advance :-)

like image 264
VISHAL VIRADIA Avatar asked Jan 15 '13 13:01

VISHAL VIRADIA


1 Answers

Command to start instrumentation from adb shell :-

adb shell am instrument -w com.android.vishal.project.test/android.test.InstrumentationTestRunner   

Android Code to start instrumentation from Android Activity :-

 startInstrumentation(new ComponentName("com.android.vishal.project.test", "android.test.InstrumentationTestRunner"), null, null);

Note :

Other Method,

Android Code for start instrumentation (Android 2.3 to 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);

for Android 4.2 it requires permission "android.permission.INJECT_EVENTS" & which is only allowed by System application. User application can not use this permission because of some security reasons.

so you can not use Runtime.getRuntime().exec(str_cmd); for Android 4.2 onwards ...

so now working method is :

 startInstrumentation(new ComponentName("com.android.vishal.project.test", "android.test.InstrumentationTestRunner"), null, null);

execute this command from your Activity.

Thanks.

like image 139
VISHAL VIRADIA Avatar answered Oct 04 '22 17:10

VISHAL VIRADIA