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.
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, MonkeyDeviceConnect to the Device
device = MonkeyRunner.waitForConnection()</code><code>if not device:</code><code> print('无法连接到设备')</code><code> exit(1)Install the Application
If the app under test is not installed, use the installPackage() method:
apk_path = 'path/to/your/app.apk'</code><code>device.installPackage(apk_path)Launch the Application
package = 'com.example.myapp'</code><code>activity = '.MainActivity'</code><code>runComponent = package + '/' + activity</code><code>device.startActivity(component=runComponent)Perform Test Actions
Simulate touch and drag gestures:
# Simulate a tap at (100, 200)</code><code>device.touch(100, 200, MonkeyDevice.DOWN_AND_UP)</code><code># Simulate a drag</code><code>device.drag((100,200), (300,400), 0.5, 5)Screenshot Verification
result = device.takeSnapshot()</code><code>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</code><code># Connect to device</code><code>device = MonkeyRunner.waitForConnection()</code><code>if not device:</code><code> print('无法连接到设备')</code><code> exit(1)</code><code># Install app</code><code>apk_path = 'path/to/com.example.myapp.apk'</code><code>device.installPackage(apk_path)</code><code># Launch app</code><code>package = 'com.example.myapp'</code><code>activity = '.MainActivity'</code><code>runComponent = package + '/' + activity</code><code>device.startActivity(component=runComponent)</code><code># Simulate tap</code><code>device.touch(100, 200, MonkeyDevice.DOWN_AND_UP)</code><code># Wait for app response</code><code>MonkeyRunner.sleep(2)</code><code># Capture screenshot</code><code>result = device.takeSnapshot()</code><code>result.writeToFile('screenshot.png','png')</code><code># Uninstall app</code><code>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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
