Saturday, June 18, 2011

monkeyrunner - Android Automation

Today, we're going to show you how simple it is to automate android events via the Android SDK with a tool known as, monkeyrunner.

The monkeyrunner tool provides an API for writing programs that control an Android device or emulator from outside of Android code (for more information, click here).  The interesting thing about monkeyrunner from our own experience is that if you run:

$ <android_sdk_path>/tools/monkeyrunner

with no parameters, you'll be presented with a wonderful Jython prompt...this means that possibilities are endless and limited only by one's imagination. :)



Now that the cogs are spinnin' and you can say to yourself, "this totally has potential" - let's dive right into automating some Android stuff!

Here's a python script that automates opening an android application, running various keypad events, and taking two screenshots for upload to the Android Developers Market.  Create a file called, monkeyrunner_job1.py and paste the following into the file (of course replacing the proper values of variable objects marked with the comment, "# INFO_NEEDED_ON_LINE_BELOW":

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

# Connects to the current device, returning a MonkeyDevice object
# INFO_NEEDED_ON_LINE_BELOW
device = MonkeyRunner.waitForConnection(600,'<adb device value taken from sdk command, "android_skd_path/adb devices">') # Wait 10 Minutes Until Things Start Exploding...

# Installs the Android package. Notice that this method returns a boolean, so you can test to see if the installation worked.
# INFO_NEEDED_ON_LINE_BELOW
install_status = device.installPackage('<apk_path>/app.apk')
print install_status
if install_status != True:
  print "Something went wrong with the installation!"
  sys.exit(1)

## sets a variable with the package's internal name
# INFO_NEEDED_ON_LINE_BELOW
package = 'com.yourmobileapp.prod.YourOwnAndroidApplication'

## sets a variable with the name of an Activity in the package
# INFO_NEEDED_ON_LINE_BELOW
activity = '.Yourmobileapp'

# sets the name of the component to start
runComponent = package + '/' + activity

# Runs the component
device.startActivity(component=runComponent)

time.sleep(60)
device.press('KEYCODE_DPAD_DOWN') # Press Down

# Takes a screenshot
result1 = None
while result1 == None:
  result1 = device.takeSnapshot()

print "Value of result1 Screenshot object"
print result1



# INFO_NEEDED_ON_LINE_BELOW
result1.writeToFile('./SB/screens/screen1.png','png')

# Press the Down Arrow and Long Press to Obtain Context Menu
time.sleep(3)
device.drag((150,300), (150,300), 10, 1) # Long Press
time.sleep(3)
device.press('KEYCODE_DPAD_DOWN') # Down
time.sleep(3)

# Take another Screenshot
result2 = None
while result2 == None:
  result2 = device.takeSnapshot()

print "Value of result2 Screenshot object"
print result2
result2.writeToFile('./SB/screens/screen2.png','png')


Save monkeyrunner_job1.py - now, to actually run it and see each item run line by line (great for debugging) execute the following command at a shell prompt:

$ <android_sdk_path>/tools/monkeyrunner <  monkeyrunner_job1.py

I hope you find this useful!

If you enjoyed this post, send us = kudos = 
(Bitcoin Addr: 19n6q3GZfoM64oqv5HsDnhzqvcEvJUvmdx)



For more information about writing Android applications in Java, check out this book on Amazon:



Cheers!

No comments:

Post a Comment