Thursday, April 21, 2011

Android Monkeyrunner

I'm currently working on a project that requires automated testing of Android applications. Fortunately, Google has released a python API for manipulating Android devices, applications, intents, etc. called Monkeyrunner, but the help has been especially lacking. One reason for this is because the command that the Monkeyrunner project page says will generate the API documentation doesn't work because help.py does not appear to be provided in the Android SDK. This blog post will remedy that situation.

According to the Google project site, you should be able to run the following command to generate the API docs for Monkeyrunner:


monkeyrunner <format> help.py <outfile>


Unfortunately, the help.py file does not appear to exist. So, I've created a help.py file that will give you all the capabilities of the old help.py, if it ever existed. Copy and paste the following into a new file called help.py on your computer (or download the file from here):


help.py file to create on your computer

#!/usr/bin/env python

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from optparse import OptionParser

def help_callback (option, opt, value, parser):
parser.print_help ();

parser = OptionParser (add_help_option=False)
parser.add_option ("-o", "-f", "--outfile", dest="outfile", default="help.html",
help="file to output monkeyrunner help to",
metavar="OUTFILE")
parser.add_option ("-t", "--type", dest="type", default="html",
help="type of output to generate (html or text)",
metavar="TYPE")
parser.add_option ("-h", "--help", dest="help", default=None,
action="callback", callback=help_callback,
help="show usage information for this script")

(options, args) = parser.parse_args ()

if options.help is None:
text = MonkeyRunner.help(options.type)
f = open(options.outfile, 'w')
f.write(text)
f.close()

print "\nMonkeyrunner help written to " + options.outfile + " (type:" \
+ options.type + ")\n"




The file comes with its own help and usage information, which you can access by providing a -h or --h option like so:


monkeyrunner help.py -h


By default, the help.py sets the output file to help.html and sets the type of output to html. Feel free to use this to generate the Monkeyrunner built-in help for reference on your system.