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

前端 未结 4 1766
渐次进展
渐次进展 2020-11-30 05:02

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 wi

相关标签:
4条回答
  • 2020-11-30 05:28

    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 ( );
    
    0 讨论(0)
  • 2020-11-30 05:33

    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

    0 讨论(0)
  • 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");
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:39

    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" );
    }
    
    0 讨论(0)
提交回复
热议问题