How to start Instrumentation project programmatically using Android Intent?
Asked Answered
P

2

6

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 :-)

Pre answered 15/1, 2013 at 13:50 Comment(3)
do you want to run your instrumentation test from a different activity in the same application or from another application ?Zeiger
from different project activity(1st project) , i want to start testing project 'com.google.vishal.test" (2nd test project) which automate some activity (3rd project).Pre
look here for a similar question.Zeiger
P
19

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.

Pre answered 17/1, 2013 at 5:47 Comment(8)
Hello @juned : you can run "adb push/pull" from HOST machine using this link #15464611, but you cannot run "adb push/pull" from android code, because adb is part of host side android sdk tool. Hope this will help you to solve your problem ...Pre
Thanks for response, is there any other solution to transfer files using USB ?Silvia
@juned: please see this video "MTP over USB" : youtube.com/watch?v=84O8KlPxEyUPre
just seen that video, but how to do that programmatically ?Silvia
From Host side, write one C or java program to write one file in C:\ Drive. same way you will get Drive name for your phone like for example F:\ is your phone directory. then if you want to push/pull some files to/from phone sdcard then just copy c:\file.txt to F:\file.txt. write program for that. thanks.Pre
Thanks, and how that file will be executed from android application, how communication will take place between android application and windows system ?Silvia
write file from PC using java program to sdcard. Then Read same file from sdcard using android app. Communication Done (message pass done). or using socket programming using WiFi or Bluetooth or WiFi direct whatever you like. example Link "#10388750"Pre
I am not able to run pull command with java... trying Runtime.exec but nothing is actually happening. When I try the same command in terminal, it worksToluol
M
2

This is actually not possible to do, the reason being is that to run instrumentation you need to it via ADB, adb has certain special privileges because of security and therefore can not be run on the phone (As with anything open source, it is of course possible but you would have to rewrite some android and then it would only work on phones you installed that on!).

May I ask your reason for doing this? If you really need to automate across applications your better choice might be to either us the new android ui test framework or to test only on the emulator and use something that runs on top of the view hierarchy because trying what you are currently is a dead end.

Medical answered 16/1, 2013 at 7:20 Comment(3)
i want to start different testing projects one after another, so can not start from desktop command prompt every time. so i need some method which execute by android code only. thanks for your excellent help @"Paul Harris", i found a way to run, and write bellow as my answer.Pre
@PaulHarris What do you mean it is not possible? Look at the startInstrumentation() method in Android's Context class: developer.android.com/reference/android/content/…, java.lang.String, android.os.Bundle)Unspoiled
this is useful for I want to use UiAutomator to start some app and interact automatically to do something for me, which relieve me from some stupid work.Hap

© 2022 - 2024 — McMap. All rights reserved.