Because of the project needs, began to study the Android automated testing methods, some of the tools, methods and frameworks to do some simple collation, including Monkey, Monkeyrunner, Athrun, Appium, is still learning the Android test framework, CTS and so on. This article is mainly for the previous period of time study summary, because just contact soon, so this article will inevitably be some superficial, the great god please walk around ~
Directory
1.Monkeyrunner Introduction
2.Monkeyrunner Tool Features
The difference between 3.Monkeyrunner tools and monkey Tools
4. Running Monkeyrunner
5. Example
6. Study Notes
1, Monkeyrunner Introduction
Monkeyrunner is one of the tools that comes with the Android SDK, which provides an API to press Android devices or emulators.
Monkeyrunner provides an API that programs written using this API can control Android devices and emulators outside of Android code. With Monkeyrunner, you can write a Python program to install an Android application or test package, run it, send it an analog keystroke, intercept its user interface picture, and store it on your workstation.
The main purpose of the Monkeyrunner tool is to test applications and devices at the functional/framework level, or to run unit test suites, but you can of course use them for other purposes.
2. Monkeyrunner Tool Features
Multi-Device control: The Monkeyrunner API can implement test suites across multiple devices or emulators. You can pick up all the devices at the same time or start all the simulators at once (or all together), connect to each one by program, and then run one or more tests. You can also use a program to start a configured emulator, run one or more tests, and then close the emulator.
Functional testing: Monkeyrunner can be tested for an application automation function. gives you input values for keystrokes or touch events, and then observes the screenshot of the output result.
Regression test: Monkeyrunner can test the stability of an application by running an app and comparing its result screenshot to a given known good result screen.
Scalable Automation: Because Monkeyrunner is an API toolkit, you can develop a complete set of systems based on Python modules and programs to control your Android device. In addition to using the Monkeyrunner API, you can use standard Python OS and subprocess modules to invoke Android tools such as ADB.
Please see the official original description: http://developer.android.com/tools/help/monkeyrunner_concepts.html
3. The difference between Monkeyrunner tools and monkey Tools
Monkey:
The Monkey tool runs directly in the ADB shell of the device or emulator, generating a pseudo-random stream of events for the user or system.
Monkeyrunner:
The Monkeyrunner tool is a specific command and event control device or emulator that is defined on the workstation through the API.
4, run Monkeyrunnermonkeyrunner API mainly includes three modules 1, Monkeyrunner: This class provides methods for connecting Monkeyrunner and devices or emulators, and it provides a way to create a user interface display. 2, Monkeydevice: Represents a device or simulator. This class provides a way to install and unload packages, turn on activity, send keystrokes and touch events, run test packages, and more. 3, Monkeyimage: This class provides a way to capture the screen. This class provides methods for converting bitmaps into various formats, comparing two Monkeyimage objects, saving an image to a file, and so on.
#引用导入API
From Com.android.monkeyrunner import <module>
Run Monkeyrunner
The command syntax is:
Monkeyrunner-plugin <plugin_jar> <program_filename> <program_options>
Mode one: Run Monkeyrunner directly in the CMD command window
Method Two: Write test code file using Python, execute Monkeyrunner findyou.py run in cmd
Either way, you need to invoke the Monkeyrunner command under the tools subdirectory of the SDK directory.
Note: You must run the appropriate emulator or connect to the real machine before running Monkeyrunner, otherwise monkeyrunner cannot connect to the device running the emulator there are two methods: 1, execute simulator 2 through Eclipse, command call simulator in CMD here to introduce through the command , the method of executing the emulator in CMD EMULATOR-AVD test is the name of the emulator above the command.
Report:
Issue: cmd run prompt monkeyrunner is not an internal or external command, or a program or batch file that can be run.
Workaround: The computer environment variable is not configured, and the Monkeyrunner directory is located in the environment variable.
Variable name: Path
Variable Value: D:\android\android-sdk-windows\tools;d:\android\android-sdk-windows\platform-tools
5. Example
Example one: Uninstall the old app and install the new app
* Prepare
A. Connecting An Zuojin Machine Equipment
B. Run cmd to detect if the connection is successful
CMD>ADB devices
Report:
Issue: cmd run prompt adb is not an internal or external command, nor is it a program or batch file that can be run.
Workaround: The computer environment variable is not configured and the ADB directory is located in the environment variable.
* Way One:
1. Open cmd, run Monkeyrunner
2. After entering Monkeyrunner's shell command interaction mode, enter the following command one by one
1 from Com.android.monkeyrunner import Monkeyrunner, Monkeydevice, Monkeyimage
2 device = Monkeyrunner.waitforconnection ()
3 device.removepackage (' cn.richinfo.thinkdrive ')
4 device.installpackage (' e:\\java\\monkeyrunner\\test1\\thinkdrive_new.apk ')
Note: For the purpose of each command, see the annotations in method two
Real Exercise
C. Check if the mobile app has been updated
* Way Two:
A. Writing Python test code
# File:Test1.py
# vision:v1.0
# author:findyou
# modules used in the introduction of this program
From Com.android.monkeyrunner import Monkeyrunner, Monkeydevice, Monkeyimage
# Connect Mobile devices
device = Monkeyrunner.waitforconnection ()
#
result = Device.takesnapshot ()
# will be saved to a file
Result.writetofile (' e:\\java\\monkeyrunner\\test1\\test1_001.png ', ' png ')
# Uninstall App
Device.removepackage (' cn.richinfo.thinkdrive ')
Print (' Uninstall success! ')
# Pause for 5 seconds
Monkeyrunner.sleep (5)
#
result = Device.takesnapshot ()
Result.writetofile (' e:\\java\\monkeyrunner\\test1\\test1_002.png ', ' png ')
# Install a new app
Device.installpackage (' e:\\java\\monkeyrunner\\test1\\thinkdrive_new.apk ')
Print (' Install success! ')
#
result = Device.takesnapshot ()
Result.writetofile (' e:\\java\\monkeyrunner\\test1\\test1_003.png ', ' png ')
Note: Please remove the Chinese comment when copying the operation, or add #coding =utf-8 at the beginning
B. Execute test1.py Script
Monkeyrunner E:\JAVA\monkeyrunner\Test1\Test1.py
Real Exercise
C. Check if the mobile app has been updated
6. Learning Note # modules used in the introduction program from Com.android.monkeyrunner import Monkeyrunner, Monkeydevice, Monkeyimage
#引入程序所用的模块, use aliases
From Com.android.monkeyrunner import Monkeyrunner as Mr
From Com.android.monkeyrunner import Monkeydevice as MD
From Com.android.monkeyrunner import monkeyimage as Mi
#如果给导入的模块起了别名, you must use an alias, or an error will occur.
#比如连接设备或模拟器, after the above alias, the command should be as follows:
Device=mr.waitforconnection ()
#连接到设备或模拟器
#参数1: Time-out, unit seconds, floating-point number. The default is to wait indefinitely.
#参数2: String DeviceID, the specified device name. It is assumed that the current device (mobile phone priority, such as the mobile phone via USB cable connected to the PC, followed by the simulator).
#默认连接:
device = Monkeyrunner.waitforconnection ()
#参数连接:
device = monkeyrunner.waitforconnection (1.0, ' 4df74b8xxxxxxx ')
#向设备或模拟器安装APK
#以下两种方式都是对的
Device.installpackage (' e:/java/monkeyrunner/test1/thinkdrive_new.apk ')
Device.installpackage (' e:\\java\\monkeyrunner\\test1\\thinkdrive_new.apk ')
#参数可以为绝对路径, can also be a relative path
#卸载设备或模拟器中的APK
#参数为APK包名
Device.removepackage (' cn.richinfo.thinkdrive ')
#启动任意的Activity #device.startactivity (component= "package name/start activity")
#以下两种都OK
Device.startactivity (component= "cn.richinfo.thinkdrive/cn.richinfo.thinkdrive.ui.activities.navigateactivity")
Device.startactivity (component= "cn.richinfo.thinkdrive/.ui.activities.navigateactivity")
#手机
#获取设备的屏蔽缓冲区, resulting in a masking capture of the entire display. ()
Result=device.takesnapshot ()
#返回一个MonkeyImage对象 (bitmap wrapper), we can save the diagram to a file using the following command
Result.writetofile (' e:\\java\\monkeyrunner\\test1\\test1_001.png ', ' png ')
#暂停
#暂停目前正在运行的程序指定的秒数
#MonkeyRunner. Sleep (number of seconds, floating point)
Monkeyrunner.sleep (5)
#字符串发送到键盘 #device. Type (' string ')
Device.type (' findyou ')
#唤醒设备屏幕 # After the lock screen, the screen is off, you can wake up with the following command
Device.wake ()
#重起手机
Device.reboot ()
#模拟滑动
#device. Drag (x,y,d,s)
#X Start coordinates
#Y End coordinates
#D drag duration (in seconds), default 1 seconds
#S the steps to take when interpolating points. The default value is 10
Device.drag (100,1053), (520,1053), 0.1,10)
#在指定位置发送触摸事件
#device. Touch (x, Y, touch event type)
#x, the units of y are pixels
#触摸事件类型, see below for device.press description of Findyou
Device.touch (520,520, ' down_and_up ')
#发送指定类型指定键码的事件
#device. Press (Parameter 1: Key code, Parameter 2: Touch event type)
#参数1: See Android.view.KeyEvent
#参数2, there are three types of type-touch event types that are returned by Touchpresstype ().
#1, down sends a down event. Specifies that the down event type is sent to the device, corresponding to pressing a key or touching the screen.
#2, up sends an up event. Specifies that the up event type is sent to the device, corresponding to releasing a key or lifting from the screen.
#3, Down_and_up sends a down event, and then an up event. Corresponds to the input key or tap the screen.
The above three events are the press () parameter or the touch () parameter
#按下HOME键
Device.press (' Keycode_home ', monkeydevice.down_and_up)
#按下BACK键
Device.press (' Keycode_back ', monkeydevice.down_and_up)
#按下下导航键
Device.press (' Keycode_dpad_down ', monkeydevice.down_and_up)
#按下上导航键
Device.press (' keycode_dpad_up ', monkeydevice.down_and_up)
#按下OK键
Device.press (' Keycode_dpad_center ', monkeydevice.down_and_up)
KeyCode:
HOME key Keycode_home back key keycode_back send key Keycode_call end key Keycode_endcall navigation key keycode_dpad_up down navigation key Keycode_dpad_down Left navigation Keycode_dpad_left Right navigation key keycode_dpad_right OK keycode_dpad_center volume key keycode_volume_up down volume key Keycode_volume_down p Ower key Keycode_power CAMERA key Keycode_camera MENU key Keycode_menu
More KeyCode http://developer.android.com/reference/android/view/KeyEvent.html
More Monkeyrunner Learning: http://developer.android.com/tools/help/monkeyrunner_concepts.html
Android Automated Test Monkeyrunner Learning Notes (i) "Turn"