Monkeyrunner is a tool from Android SDK. It provides an API using commands to execute tests in Android devices or Android emulators. It is possible to write a Python program that installs an Android application and perform it actions.
The API has the following classes of commands:
- Monkey Device: it provides connection to a device or emulator.
- http://developer.android.com/tools/help/MonkeyDevice.html
- Monkey Image: It can run packages, provides methods for installing and uninstalling packages, start an Activity, and send keyboard or touch events to an application.
- http://developer.android.com/tools/help/MonkeyImage.html
- Monkey Runner : It captures screen image, comparing two MonkeyImage objects and writing an image to a file.
- http://developer.android.com/tools/help/MonkeyRunner.html
MonkeyRunner Example
To use the monkeyrunner, it is necessary to include in the environment variable PATH the android tools directory.
... \ sdk \ build-tools \ android-4.4;
... \ sdk \ tools
- Create a folder called monkey in the c drive of the computer and put the scripts monkeyrunner_recorder.py monkeyrunner_playback.py available here:
- https://github.com/miracle2k/android-platform_sdk/tree/master/monkeyrunner/scripts
- Create subfolders images, apks-script, log and apks_to_test
- Open the emulator android or conect your Android device in USB (usb_debug must eon)
- Open a command prompt and type:
- monkeyrunner c: \ monkey \ monkeyrunner_recorder.py
The interface monkeyrunner recorder will appear.
With the mouse, try to perform actions on the emulator screen, like opening an application.
The actions will appear on the right of the recorder interface.
MonkeyRunner Recorder |
In the recorder interface , save the scripts clicking on the menu Export Actions
- Save the script as a file extension .mr (test.mr example)
- Close the recorder interface and reopen the command prompt
- Type: monkeyrunner c:\monkey\monkeyrunner_playback.py c: \monkey\test.mr
- Press enter and note that the emulator will play back your recorded actions on Emulator or in the Android device.
If you prefer, it is possible to create scripts directly in python and run. in monkeyrunner.
Follow the python code bellow, it tests installation of an application, it simulates the buttons' actions Back, Home and Menu and then it generates a log file and saves screen image.
class DroidTest:
rootfolder = 'c:\\monkey\\'
apkDir = rootdir + 'apks_to_test\\'
imageDir = rootdir + 'images\\'
logDir = rootdir + 'log\\'
def __init__(self, device, count):
self.device = device
self.count = count
def run(self):
eraseLog(self.device)
for apk in glob.glob(self.apkDir + '/*.apk'):
print "Testing apk %s" % apk
packagename,activity = get_package_activity_name(apk)
componentname = packagename + "/." + activity
apk_path = self.device.shell('pm path ' + packagename)
if apk_path.startswith('package:'):
print"App is already installed."
else:
print"App not installed, installing APKs..."
self.device.installPackage(apk)
print componentname
self.device.startActivity(component=componentname)
MonkeyRunner.sleep(5)
image = self.device.takeSnapshot()
image.writeToFile(self.imageDir + 'screenshot_' + packagename + str(self.count) + '.png','png')
#Simulate Device Events Home, Back and Menu buttons
self.device.press('KEYCODE_HOME', 'DOWN_AND_UP')
MonkeyRunner.sleep(5)
self.device.startActivity(component=componentname)
MonkeyRunner.sleep(5)
self.device.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(5)
self.device.startActivity(component=componentname)
MonkeyRunner.sleep(5)
self.device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)
log(self.logDir + 'test' + str(self.count) +'.log', self.device);
self.device.removePackage(apk)
if __name__ == "__main__":
main()
- Name this code as testDroid.py.
- Put an apk in the folder \apks_to_test
- Start the emulator or Android device and then type the following command in prompt: monkeyrunner c:\mokey\testDroid.py
The python program will run and it will execute the buttons tests.