如何将参数传递给使用adb shell am Instrumentation命令启动的测试功能

ila*_*ana 25 instrumentation android

我正在开发Android,我正在使用仪器测试手机应用程序.Instrumentation是Android环境来测试应用程序.

为此,我使用带有测试用例名称的am命令.我运行adb,然后输入adb shell,然后在shell中写入am命令.

我希望与这个命令一起提供一个参数.我的意思是我希望将参数传递给am命令启动的测试.

可能吗 ???请帮忙 ?

Rya*_*rad 50

你可以将数据uri,mime类型甚至"extras"传递给am命令.

我[开始|仪器]

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

是仪器[-e <arg_name> <arg_value>] [-p <prof_file>] [-w] <component>

您可以将它们作为"额外"传递,然后获取传递给它的额外内容.

你会像这样传递它们:

am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT 
  -e foo bar -e bert ernie -n my.package.component.blah
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中:

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" );
}
Run Code Online (Sandbox Code Playgroud)

  • 嗨Ryan我的类扩展了InstrumentationTestCase而不是Activity类,所以我无法执行:this.getIntent().我理解我需要创建意图:Intent intent = new Intent(Intent.someAction); 你能帮助我找出我应该在这里使用什么动作吗?我还读到有次要属性:类别,类型,组件,附加功能.我知道我还需要相应地更新清单文件.很抱歉这么多q/a - 我不再使用Android和Java了.非常感谢您的协助.Ilana (3认同)

Ton*_*ter 14

将参数传递给:(例如,-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
Run Code Online (Sandbox Code Playgroud)

在测试类中:

{
    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");
    }
}
Run Code Online (Sandbox Code Playgroud)