Introduction to the Monkey Runner Tool
This post won’t be a course but an introduction, because the aim is to discover by yourself how much this tool can be powerful. Monkey Runner is a tool which let the developer create programs which control the connected device/emulator.These programs will be developed in Python, and can install, launch and manipulate apps, and take screenshots. This is what we need to complete a Ant script which could build and sign the app for example. Indeed, if you have many applications, automate builds/tests, etc. becomes very interesting in order to gain time… and also because letting the computer to do all the job is pretty exciting!
Stop talking, and let’s go. The API is contained in three modules which are:
- MonkeyRunner : Class which provides methods for connecting a device/emulator.
- MonkeyDevice : Class which provides methods for installing/uninstalling packages, sending events to an application (gestures, keyboard events, etc.)
- MonkeyImage : Class which provides methods for capturing screenshots.
Let’s create our monkeyrunner file, call it mymonkey.py for example (.py because I need my syntax coloring on vim :p)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
To launch this script you only have to execute this command:
Note 1: The monkeyrunner command is situated in [android-sdk-path]/tools/
Note 2: If you are developing on windows, you may have to put the absolute paths (C:/etc.).
Note 3: You can of course run your Unit tests !
And voilà! You have now the most basic script in the world of the universe, which connect monkeyrunner to a device, install and launch an application, take and save a screenshot, and clic on the menu button, you can now imagine all the things you can do.
Please if you have any criticism/notes/suggestions, do not hesitate to write a comment, and if you liked this post, do not hesitate to share it!