Mobile Development 5 min read

Using MonkeyRunner for Android Automated Testing: Scenarios and Sample Code

This article introduces MonkeyRunner, an Android automation tool, and demonstrates its use in basic operation testing, app installation and removal, UI interaction, and performance measurement with clear Python‑style code examples for each scenario.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using MonkeyRunner for Android Automated Testing: Scenarios and Sample Code

MonkeyRunner is an Android automation testing tool that enables functional and stress testing on devices or emulators. The following sections describe typical use cases and provide ready‑to‑run code snippets.

Basic Operation Testing

MonkeyRunner can simulate touch, drag, and key‑press events on the screen. The example connects to a device, taps a coordinate, performs a horizontal swipe, and sends a back‑key event.

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# Connect to device or emulator
device = MonkeyRunner.waitForConnection()
# Tap screen at (500, 500)
device.touch(500, 500, MonkeyDevice.DOWN_AND_UP)
# Drag from (200, 200) to (800, 200) over 0.5 seconds with 5 steps
device.drag((200, 200), (800, 200), 0.5, 5)
# Press the back key
device.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP)

Application Installation and Uninstallation

The tool can install an APK, launch its main activity, capture a screenshot, stop the app, and finally uninstall it.

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# Connect to device or emulator
device = MonkeyRunner.waitForConnection()
# Install APK
device.installPackage('/path/to/my_app.apk')
# Start the app (package name: com.example.myapp)
device.startActivity(component='com.example.myapp/.MainActivity')
# Wait for the app to load
MonkeyRunner.sleep(3000)
# Capture screenshot
snapshot = device.takeSnapshot()
snapshot.writeToFile('/path/to/screenshot.png', 'png')
# Stop the app
device.shell('am force-stop com.example.myapp')
# Uninstall the app
device.uninstallPackage('com.example.myapp')

UI Automation

MonkeyRunner can capture the screen, locate UI elements using image templates, and interact with the found elements.

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# Connect to device or emulator
device = MonkeyRunner.waitForConnection()
# Capture screen snapshot
snapshot = device.takeSnapshot()
# Load template image of the UI element
image_template = MonkeyImage.openFromFile('/path/to/ui_element_template.png')
# Find matching objects
elements = snapshot.findObjects(image_template)
if elements:
    # Operate on the first matched element
    element = elements[0]
    x, y = element.getCenter()
    device.touch(x, y, MonkeyDevice.DOWN_AND_UP)
else:
    print("UI element not found.")

Performance Testing

MonkeyRunner can measure the execution time of a series of actions and retrieve CPU and memory usage statistics from the device.

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# Connect to device or emulator
device = MonkeyRunner.waitForConnection()
# Start timing
start_time = MonkeyRunner.currentTimeMillis()
# Perform actions
device.touch(500, 500, MonkeyDevice.DOWN_AND_UP)
device.drag((200, 200), (800, 200), 0.5, 5)
device.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP)
# End timing
end_time = MonkeyRunner.currentTimeMillis()
execution_time = end_time - start_time
print(f"Operations took {execution_time} milliseconds.")
# Get CPU and memory usage
cpu_info = device.getCPUUsage()
memory_info = device.getMemoryInfo()
print(f"CPU usage: {cpu_info.user}%, {cpu_info.system}%")
print(f"Memory usage: {memory_info.free} free, {memory_info.total} total")

MonkeyRunner is well‑suited for functional and simple UI testing, but for complex interactions or deep logic verification you may need to combine it with frameworks such as Espresso or UI Automator. Note that MonkeyRunner uses a Java API, so when writing scripts in Python you must handle the appropriate syntax and type conversions.

Mobile DevelopmentPythonAndroidAutomated TestingUI testingMonkeyRunner
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.