Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

android - How can I deliver parameters to a test function, that launched using adb shell am Instrumentation command

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...