views:

50

answers:

1

Hi All

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 ?

+1  A: 

you can pass a data uri, mime type and even "extras" to the am command.

am [start|instrument]

am start [-a <action>] [-d ]
[-t <mime_type>] [-c <category> [-c <category>] ...]
[-e <extra_key> <extra_value>
[-e <extra_key> <extra_value> ...]
[-n <component>] [-D] [<uri>]

am instrument [-e <arg_name> <arg_value>] [-p <prof_file>] [-w] <component>

You could pass them as "extras" and then get the extras that are passed to it.

You would pass them like this:

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" );
}
Ryan Conrad
Hi RyanI 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
All examples I have are not talking about pass params to test launched by am command ... I really performed a search
ilana
i updated my answer with an example that should work to get the extras set from the am start command
Ryan Conrad
Hi Ryan Thanks a lot for your help !!I am about to try thisCan 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
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
Hi RyanMy 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 hereI 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