29

I am developing in Android, I am using instrumentation to test Phone application. Instrumentation is Android env to test applications.

For that I use am command with name of test case. I run adb, then I enter adb shell, then write in shell the am command.

I wish to deliver a parameter together with this am command. I mean that I wish to deliver parameters to the test launched by the am command.

Is it possible ??? Please help ?

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
ilana
  • 505
  • 2
  • 5
  • 12

5 Answers5

55

You can pass a data URI, mime type and even "extras" to the am command. You could pass them as "extras" and then get the extras that are passed to it.

The available options related to extras are:

  • -e | --es extra_key extra_string_value: Add string data as a key-value pair.
  • --ez extra_key extra_boolean_value: Add boolean data as a key-value pair.
  • --ei extra_key extra_int_value: Add integer data as a key-value pair.
  • --el extra_key extra_long_value: Add long data as a key-value pair.
  • --ef extra_key extra_float_value: Add float data as a key-value pair.
  • --eu extra_key extra_uri_value: Add URI data as a key-value pair.
  • --ecn extra_key extra_component_name_value: Add a component name, which is converted and passed as a ComponentName object.
  • --eia extra_key extra_int_value[,extra_int_value...]: Add an array of integers.
  • --ela extra_key extra_long_value[,extra_long_value...]: Add an array of longs.
  • --efa extra_key extra_float_value[,extra_float_value...]: Add an array of floats.

You would pass them like this:

adb shell am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT 
  -e foo bar -e bert ernie -n my.package.component.blah

Then in your code:

Bundle extras = this.getIntent().getExtras();

if (extras != null) {
    if (extras.containsKey("foo")) {
        Log.d("FOO", extras.getString("foo"));
    } else {
        Log.d("FOO", "no foo here");
    }

    if (extras.containsKey("bert")) {
        Log.d("BERT", extras.getString("bert"));
    } else {
        Log.d("BERT", "Bert is all alone");
    }
} else {
    this.setTitle("no extras found");
}
slhck
  • 36,575
  • 28
  • 148
  • 201
Ryan Conrad
  • 6,870
  • 2
  • 36
  • 36
  • Hi Ryan I spent few time for looking after good example of how can I deliver a parameter/s to my test - Sorry just couldn't find something useful. Can you please send some link or snippet of code in java of apk that should retrieve these parameters (extras) and an example of How I write adb shell am start command that delivers the extras to the test on target. Thanks a lot Ilana – ilana Jul 13 '10 at 08:48
  • All examples I have are not talking about pass params to test launched by am command ... I really performed a search – ilana Jul 13 '10 at 09:05
  • i updated my answer with an example that should work to get the extras set from the am start command – Ryan Conrad Jul 15 '10 at 03:27
  • Hi Ryan Thanks a lot for your help !! I am about to try this Can you tell me what is bar means And shouldn't we take parameters in inverted commas ? If I put phone number as parameter - need I put it in commas? – ilana Jul 19 '10 at 11:01
  • bar is the value set to the "foo" extra. foo is the name, bar is the value. same thing with "bert". bert is the name of the extra, ernie is the value. – Ryan Conrad Jul 19 '10 at 11:04
  • 3
    Hi Ryan My class extends InstrumentationTestCase and not Activity class, So I cannot perform: this.getIntent(). I anderstand that I need to create intent doing: Intent intent = new Intent(Intent.someAction); Can you help me to find out what action should I use here I also read that there are secondary attributes: category, type, component, extras. I know that I also need to update the manifest file accordingly. Sorry for so many q/a - I am quit new to Android and Java. Thanks a lot for your assist. Ilana – ilana Jul 20 '10 at 14:12
16

Pass the paramater in: (e.g., -e peerID SCH-I545)

adb -s 0915f98870e60701 shell am instrument -w -e class      /
com.example.android.testing.uiautomator.BasicSample.sendInvite /
-e peerID SCH-I545 /
com.example.android.testing.uiautomator.BasicSample.test/android.sup /
port.test.runner.AndroidJUnitRunner

In the test class:

{
    Bundle extras = InstrumentationRegistry.getArguments();
    String peerID = null;

    if ( extras != null ) {
        if ( extras.containsKey ( "peerID" ) ) {
            peerID = extras.getString("peerID");
            System.out.println("PeerID: " + peerID);
        } else {
            System.out.println("No PeerID in extras");
        }
    } else {
        System.out.println("No extras");
    }
}
Tony Nutter
  • 191
  • 1
  • 3
2

to send extra value you should add -n(Component) for sending extra value with -e

here is the sample to sent multiple key-value

adb shell am start -n com.example.jk.checkwifi/.MainActivity -e "imei" $myimei -e "ip" $IP

then to get data inside activity, get like this inside onCreate

ip = intent.getStringExtra("ip")
Jahangir Kabir
  • 1,783
  • 13
  • 17
0

exactly is:

 ./adb shell am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT -e user_id 1 -n com.shortcut.activity/com.shortcut.activity.SelectCardActivity

com.shortcut.activity/com.shortcut.activity.SelectCardActivity -> uir to your main class activity start app. will pass to your app param user_id = 1 and on class SelectCardActivity you get it as bellow :

  Bundle installparams = this.getIntent ( ).getExtras ( );
-2

Since you are already working on Android sdk, given you know the sdk location on your system - Go to sdk location on terminal(command prompt)-> type adb shell -> type am help

with example http://whenpridefucks.blogspot.in/2011/12/android-send-broadcast-intents-via-adb.html

user1901015
  • 95
  • 1
  • 6