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.
- 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.
- Monkey Runner : It captures screen image, comparing two MonkeyImage objects and writing an image to a file.
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:
- 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.
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.
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
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)
print"App is already installed."
print"App not installed, installing APKs..."
image = self.device.takeSnapshot()
image.writeToFile(self.imageDir + 'screenshot_' + packagename + str(self.count) + '.png','png')
#Simulate Device Events Home, Back and Menu buttons
log(self.logDir + 'test' + str(self.count) +'.log', self.device);
if __name__ == "__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.