English 中文(简体)
How to use MonkeyDevice.instrument?
原标题:

HI guys, I m trying to run one of my test instrumentation from my MonkeyRunner script. Unfortunately I can t get it to work. I ve tried calling MonkeyDevice.instrument with different variations of the parameters but had no luck.

I ve tried

device = MonkeyRunner.waitForConnection() device.instrument("android.test.InstrumentationTestRunner") device.instrument("com.myTestPackage.myTestClass") device.instrument("com.myTestPackage/.myTestClass") device.instrument("myTestClass")

None of these throw and error but they don t run the test either. I can run my instrumentation via Dev Tools or though Android Junit Test so I know it works.

So can someone tell me the correct to use this method? Thanks.

最佳回答

You are probably using wrong arguments. This script, which I named instrumentation.mr, helps you to use the right ones. Invoke it using you target package name.

#! /usr/bin/env monkeyrunner

import sys
from com.android.monkeyrunner import MonkeyRunner

PLI =  pm list instrumentation 

def usage():
    print >>sys.stderr, "usage: intrumentation.mr target-package"
    sys.exit(1)

def main():
    if len(sys.argv) != 2:
        usage()

    pkg = sys.argv[1]

    print "waiting for connection..."
    device = MonkeyRunner.waitForConnection()

    print "running istrumentation for %s" % pkg
    for (i, t) in map(lambda l: l.split(), device.shell(PLI).splitlines()):
        if t ==  (target=%s)  % pkg:
            print device.instrument(i.split( : )[1], {  wait :True })[ stream ]
            return

    print >>sys.stderr, "ERROR: instrumentation for %s not found" % pkg

if __name__ ==  __main__ :
    main()

For example:

$ instrumentation.mr com.example.aatg.tc

prints:

waiting for connection...
running istrumentation for com.example.aatg.tc

Test results for InstrumentationTestRunner=...............................
Time: 39.932

OK (31 tests)
问题回答

The MonkeyDevice.instrument(string class, dictionary args) maps onto the InstrumentationTestRunner commands. The Android documentation has some good info on the instrumentation command, here.

What the documentation does NOT tell you is how to specify args. I found that in the android source code. See AdbChipDevice line 483. Here s a copy and paste of the code:

   482     @Override
   483     public Map<String, Object> instrument(String packageName, Map<String, Object> args) {
   484         List<String> shellCmd = Lists.newArrayList("am", "instrument", "-w", "-r");
   485         for (Entry<String, Object> entry: args.entrySet()) {
   486             final String key = entry.getKey();
   487             final Object value = entry.getValue();
   488             if (key != null && value != null) {
   489                 shellCmd.add("-e");
   490                 shellCmd.add(key);
   491                 shellCmd.add(value.toString());
   492             }
   493         }
   494         shellCmd.add(packageName);
   495         String result = shell(shellCmd.toArray(ZERO_LENGTH_STRING_ARRAY));
   496         return convertInstrumentResult(result);
   497     }

So the following python monkey code:

params = dict()
params[ size ] =  small 
device = MonkeyRunner.waitForConnection()
device.instrument( com.mycompany.myapp/android.test.InstrumentationTestRunner , params)

Is equivalent to the following command:

adb shell am instrument -w -r -e size small com.mycompany.myapp/android.test.InstrumentationTestRunner




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签