table of Contents
Record & Playback function ---- hidden features
Easy Monkey Device-- hidden features
Monkey Runner
Installation Environment: JDK SDK Python Pycharm monkeyrunner environment variable configuration
Configuration environment variable
Enter monkeyrunner encounter problems under dos command window
SWT folder '..\framework\x86_64' does not exist.Please set ANDROID_SWT to po
修改monkeyrunner.bat
.set frameworkdir=lib (纠正)==》set frameworkdir=..\lib
.Dcom.android.monkeyrunner.bindir=..\framework -jar %jarpath% %* (纠正)==》-Dcom.android.monkeyrunner.bindir=..\..\platform-tools -jar %jarpath% %*
monkeyrunner introduced
Introduction 1. monkeyrunner
monkeyrunner工具是使用Jython(Java编程语言实现的python)写出来的,它提供了多个API,
通过monkeyrunner API可以写一个Python程序来模拟操控Android设备中的app,
测试其稳定性并通过截屏记录出现的问题
monkeyrunner tool provides an API for writing a program to realize the control device or emulator Android from outside the Android code. By the monkeyrunner, Python can write a program to install Android application or test package, and it runs send key event, the user interface screenshots taken and stored in the working area. monkeyrunner tool primarily relates to a frame or the level of functional testing and application equipment, and operating unit test suite
2. monkeyrunner function
(1) Multi-Device Control: the API across multiple devices, all of the starting primary embodiment simulator test suite
(2) functional test: application automatically performs a functional test, and then look at the output screenshots
(3) can be extended Automation: monkeyrunner is an API tool kit, based on the entire system can be developed to control module Python Android device
3. monkeyrunner and monkey difference
monkeyrunner no direct relationship with the monkey, monkey equipment is run directly adb shell command to generate random events to be tested; and monkeyrunner specific command is sent by the API to events and a control device
MonkeyRunner类
Source: https://developer.android.google.cn/studio/test/monkeyrunner/MonkeyRunner?hl=zh_cn
method | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void | alert (string message, string title, string okTitle) 针对运行当前程序的进程显示一个提醒对话框。 |
||||||||||
integer | choice (string message, iterable choices, string title) 显示一个对话框,其中列出了针对运行当前程序的进程的选项。 |
||||||||||
void | help (string format) 使用指定格式以类似于 Python 的 |
||||||||||
string | input (string message, string initialValue, string title, string okTitle, string cancelTitle) 显示一个接受输入的对话框。 |
||||||||||
void | sleep (float seconds) 将当前程序暂停指定的秒数。 |
||||||||||
MonkeyDevice |
waitForConnection (float timeout, string deviceId) 尝试在 |
提供连接真机和模拟器、输入、暂停、警告等方法
alert()
:警告框choice()
:选项列表框help()
:API帮助文档input()
:输入sleep()
:暂停waitFor Connection()
:等待设备连接
常用方法:waitForConnection(float timeout,string deviceid)
timeout:超时时间限制
devices:具体连接的设备
# 导入MonkeyRunner类,并创建别名
from com.android.monkeyrunner import MonkeyRunner as mr
print("connect devices...")
# 通过MonkeyRunner类调用其下的等待连接设备方法
device=mr.waitForConnection()
# device=mr.waitForConnection(5,127.0.0.1:62001)
MonkeyDevice类
来源: https://developer.android.google.cn/studio/test/monkeyrunner/MonkeyDevice?hl=zh_cn
方法 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void | broadcastIntent (string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, iterable flags) 向此设备广播 Intent,就像 Intent 来自应用一样。 |
||||||||||
void | drag (tuple start, tuple end, float duration, integer steps) 在此设备的屏幕上模拟拖动手势(轻触、按住和移动)。 |
||||||||||
object | getProperty (string key) 在给定系统环境变量的名称后,返回此设备的相应值。此方法的详细说明中列出了可用的变量名称。 |
||||||||||
object | getSystemProperty (string key) . |
||||||||||
void | installPackage (string path) 将包含在 packageFile 中的 Android 应用或测试软件包安装到此设备上。如果应用或测试软件包已安装,则会被替换。 |
||||||||||
dictionary | instrument (string className, dictionary args) 在 Android 插桩下运行指定的组件,并在字典中返回结果,该字典的具体格式由正在运行的组件决定。该组件必须已存在于此设备上。 |
||||||||||
void | press (string name, dictionary type) 将 type 指定的按键事件发送到键码指定的按键。 |
||||||||||
void | reboot (string into) 将此设备重新启动到 bootloadType 指定的引导加载程序中。 |
||||||||||
void | removePackage (string package) 从此设备中删除指定的软件包,包括其数据和缓存。 |
||||||||||
object | shell (string cmd) 执行 |
||||||||||
void | startActivity (string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, flags) 通过发送根据提供的参数构造的 Intent,在此设备上启动一个 Activity。 |
||||||||||
MonkeyImage |
takeSnapshot() 捕获此设备的整个屏幕缓冲区,并生成一个 |
||||||||||
void | touch (integer x, integer y, integer type) 将 type 指定的轻触事件发送到由 x 和 y 指定的屏幕位置。 |
||||||||||
void | type (string message) 将消息中包含的字符发送到此设备,就好像这些字符是在设备键盘上输入的一样。这等同于使用 |
||||||||||
void | wake () 唤醒此设备的屏幕。 |
创建连接
newdevice = MonkeyRunner.waitForConnection()
提供了安装和卸载程序包、开启Activity、发送按键和点击事件、运行测试包等方法
broadcastIntent()
:发送广播
drag()
:拖动getProperty()
:获取当前设备属性getSystemProperty()
:获取系统配置信息
installPackage()
:安装应用instrument()
:执行测试用例press()
:按键reboot()
:重启
removePackage()
:删除指定的Packageshell()
:执行命令startActivity()
:启动应用touch()
:点击
1)常用方法
installPackage(string path)
path:程序包的路径
removePackage(string package)
package:程序包名
startActivity(string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, flags)
通常传递包名、activity名
touch(integer x, integer y, integer type)
touch参数说明:
integer x:x坐标值
integer y:y坐标值
integer type:key event类型(DOWN、UP、DOWN_AND_UP)
DOWN为按下事件UP为弹起事件 DOWN AND UP为按下弹起事件
drag(tuple start, tuple end, float duration, integer steps)
drag参数说明:
tuple start:拖拽起始位置,为tuple类型的(x,y)坐标点
tuple end:拖拽终点位置,为tuple类型的(x,y)坐标点
float duration:拖拽手势的持续时间,默认为1s
integer steps:插值点的步数,默认值为10
# -*- coding: UTF-8 -*-
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner import MonkeyDevice as md
# 连接设备
print("connect devices...")
device=mr.waitForConnection()
# 安装APP
print("install app...")
device.installPackage(r'C:\Users\Administrator\Desktop\kaoyan_4.3.7beta.243.apk')
# 配置APP信息
package='com.tal.kaoyan'
activity='com.tal.kaoyan.ui.activity.SplashActivity'
runComponent=package+'/'+activity
# 启动应用
print("launch app...")
device.startActivity(component=runComponent)
MonkeyImage类
来源: https://developer.android.google.cn/studio/test/monkeyrunner/MonkeyImage?hl=zh_cn
方法 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
string | convertToBytes (string format) 将当前图片转换为特定格式并以字符串形式返回该图片,然后您可以将其作为二进制字节的可迭代对象来访问。 |
||||||||||
tuple | getRawPixel (integer x, integer y) 以 (a,r,g,b) 格式的整数元组形式返回图片位置 (x,y) 处的单个像素。 |
||||||||||
integer | getRawPixelInt (integer x, integer y) 以 32 位整数形式返回图片位置 (x,y) 处的单个像素。 |
||||||||||
MonkeyImage |
getSubImage (tuple rect) 根据当前图片的矩形选择区创建一个新的 |
||||||||||
boolean | sameAs (MonkeyImage other, float percent) 将此 |
||||||||||
void | writeToFile (string path, string format) 将当前图片以 |
from com.android.monkeyrunner import MonkeyImage as mi
print("takeSnapshot")
screenshot-device.takeSnapshot()
screenshot.writeToFile(r'D:\CI_Env\Appium\monkeyrunner\file\test.png','png')
# -*- coding: UTF-8 -*-
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner import MonkeyDevice as md
from com.android.monkeyrunner import MonkeyImage as mi
# 连接设备
print("connect devices...")
device=mr.waitForConnection()
# 安装APP
print("install app...")
device.installPackage(r'C:\Users\Administrator\Desktop\kaoyanbang_3.3.7beta.243.apk')
# 配置APP信息
package='com.tal.kaoyan'
activity='com.tal.kaoyan.ui.activity.SplashActivity'
runComponent=package+'/'+activity
# 启动应用
print("launch app...")
device.startActivity(component=runComponent)
mr.sleep(12) # 根据APP启动响应速度调整等待时间
# 点击"跳过"按钮
print("touch skip button")
device.touch(645,53,'DOWN_AND_UP')
mr.sleep(3)
# 输入用户名+密码
print("input username and password")
device.touch(121,282,'DOWN_AND_UP')
mr.sleep(2)
device.type("xxxxxx") # 实际的用户名
device.touch(117,360,'DOWN_AND_UP')
mr.sleep(2)
device.type("yyyyyy") # 实际的密码
mr.sleep(2)
# 点击登录按钮
print("touch login button")
device.touch(356,464,'DOWN_AND_UP')
mr.sleep(10)
# 截图并保存
print("take snapshot")
screenshot=device.takeSnapshot()
screenshot.writeToFile(r'D:\CI_Env\Appium\monkeyrunner\file\kyb.png','png')
执行命令:
monkeyrunner D:\CI_Env\Appium\monkeyrunner\kyb_login.py
录制回放功能----隐藏功能
Monkey_recorder 和 Monkey_playback
Easy Monkey Device——隐藏功能
device=MonkeyRunner.waitForConnection()
device.startActivity('com.android.calculator2/.Calculator')
easy=EasyMonkeyDevice(device)
easy.touch(By.id('id/start').MonkeyDevice.DOWN_AND_UP)