Mobile Development 5 min read

Getting Started with MonkeyRunner: A Step-by-Step Guide for Android Automation Testing

This tutorial explains what MonkeyRunner is, how to set up the environment, write Python scripts, connect to Android devices, install and launch apps, simulate user actions, capture screenshots, and clean up, providing a complete example script for automated mobile testing.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Getting Started with MonkeyRunner: A Step-by-Step Guide for Android Automation Testing

What is MonkeyRunner?

Although both Monkey and MonkeyRunner are used for Android application automation testing, they serve different purposes. Monkey generates pseudo‑random user events for stress testing, while MonkeyRunner provides an API that lets you write Python scripts to control a device or emulator for precise, complex test scenarios.

Basic Steps to Write a MonkeyRunner Script

Install and Set Up the Environment

Ensure JDK and Android SDK are installed, and add the /tools directory to the system PATH so the monkeyrunner command is globally accessible.

Write the Script

Create a new .py file with any text editor and import the required modules:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

Connect to the Device

device = MonkeyRunner.waitForConnection()
if not device:
print('无法连接到设备')
exit(1)

Install the Application

If the app under test is not installed, use the installPackage() method:

apk_path = 'path/to/your/app.apk'
device.installPackage(apk_path)

Launch the Application

package = 'com.example.myapp'
activity = '.MainActivity'
runComponent = package + '/' + activity
device.startActivity(component=runComponent)

Perform Test Actions

Simulate touch and drag gestures:

# Simulate a tap at (100, 200)
device.touch(100, 200, MonkeyDevice.DOWN_AND_UP)
# Simulate a drag
device.drag((100,200), (300,400), 0.5, 5)

Screenshot Verification

result = device.takeSnapshot()
result.writeToFile('screenshot.png','png')

End the Test

After testing, optionally uninstall the app:

device.removePackage(package)

Practical Example: A Simple Test Script

The following script demonstrates a full workflow for testing an app named com.example.myapp , including connection, installation, launch, interaction, screenshot, and cleanup:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# Connect to device
device = MonkeyRunner.waitForConnection()
if not device:
print('无法连接到设备')
exit(1)
# Install app
apk_path = 'path/to/com.example.myapp.apk'
device.installPackage(apk_path)
# Launch app
package = 'com.example.myapp'
activity = '.MainActivity'
runComponent = package + '/' + activity
device.startActivity(component=runComponent)
# Simulate tap
device.touch(100, 200, MonkeyDevice.DOWN_AND_UP)
# Wait for app response
MonkeyRunner.sleep(2)
# Capture screenshot
result = device.takeSnapshot()
result.writeToFile('screenshot.png','png')
# Uninstall app
device.removePackage(package)

Conclusion

By writing MonkeyRunner scripts, you can create more complex and precise test scenarios than the basic Monkey tool allows. This guide should help you start your own MonkeyRunner scripting journey, and future articles will explore designing test strategies for various requirements.

Mobile DevelopmentPythonandroidAutomation TestingMonkeyRunnerTesting Scripts
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.