Automating Android Native Apps Testing using MonkeyRunner

MonkeyRunner is a tool that comes with the Android SDK. It is basically a tool using python scripts to run automation for Android apps.

The Android SDK site has a simple example of a monkeyrunner script and how to run it. See “A Simple monkeyrunner program” and “Running monkeyrunner” – both can be found here. For a start, don’t run with the plug-in option.

One thing to note about monkeyrunner is that unlike UI Automation or any of the other automation tools, you don’t search for a UI element and click (or tap) on it. Instead, you use the keypad keys to navigate. I found using the up, down, left, right and center keys most useful. Also there doesn’t seem to be a way to look for  UI elements and extract the labels or even accessibility keys.

There are 3 main components for monkeyrunner: MonkeyRunner, MonkeyDevice and MonkeyImage classes.

The MonkeyRunner class contains static utility methods to do a run – the most important being the WaitForConnection() method which returns a MonkeyDevice class for actual testing.

The MonkeyDevice class is the most important as its methods control the device eg. install a package, perform button press, drag, start Activities.

The MonkeyImage is used for capturing screenshots.

Some Issues to take note of

There is also a typo in the script found in the SDK documentation:

# sets a variable with the package's internal name
package = ''

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

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

Will mean that runComponent=''
instead of which is correct.

Also for some reason, I could not get the script to run with'KEYCODE_MENU','DOWN_AND_UP')

as it seemed that the ‘press’ method required 3 params instead of 2. I just added a dummy string param at the end eg. ‘xx’.

Note that the script is written in Python – here is a decent for the Python language I found useful.


Leave a comment

Filed under Android, Native App, QA, Testing

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s