MonkeyRunner常用API
1、MonkeyRunner API -alert
警告框
void alert(string message,string title,string okTitle)
脚本类型.py
#!/usr/bin/python #-*- UTF-8 -*- from com.android.monkeyrunner import MonkeyRunner MonkeyRunner.alert(‘Hello friends‘,‘This is title‘,‘OK‘) //message,title,button按钮
执行:monkeyrunner d:\alert.py //此处alert.py的相对路径为相对monkeyrunner的路径,不是cmd的路径
若运行monkey runner提示:SWT folder ‘..\framework\x86_64‘ does not exist.参考https://www.jianshu.com/p/744324ac6ce8
2、MonkeyRunner API - waitForConnection
等待设备连接,有多个device id,需要指明具体哪个设备。
waitForConnection(float timeout,string deviceid)
float timeout:等待设备的超时时间,单位s
string deviceid:deviceid的字符串名称
3、MonkeyDevice API - drag
拖动
drag(tuple start,tuple end,float duration,integer steps)
start:起点位置
end:终点位置
duration:手势持续时间
steps:步数,默认是10
4、MonkeyDevice API - press
按键
press(string keycode,dictionary type)
keycode:键的keycode值
dictionary type:Down、UP、DOWN_AND_UP(按下、弹起、按下并弹起)
5、MonkeyDevice API - startActivity
启动应用
startActivity(package+‘/‘+activity)
6、MonkeyDevice API - touch
点击
touch(integer x,integer y,integer type)
x坐标值
y坐标值
type:DOWN,UP,DOWN_AND_UP(按下、弹起、按下并弹起)
7、MonkeyDevice API - type
输入
type(string message)
8、MonkeyDevice API - takeSnapshot
截屏
MonkeyImage takeSnapshot()
9、MonkeyImage API - sameAs
图像对比
boolean sameAs(MonkeyImage other,float percent)
10、MonkeyImage API - writeToFile
保存图像文件
void writeToFile(string path,string format)
string path:图像保存路径
string format:图像保存类型
实现在搜索框中输入查询词,并截图
#!/usr/bin/python
# -*- coding: utf-8 -*-
# __author__:汤继光
# 2020-05-10
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
#连接设备
device=MonkeyRunner.waitForConnection(3,"127.0.0.1:62001")
#启动APP
device.startActivity("com.UCMobile.x86/com.uc.browser.InnerUCMobile")
#等待2s
MonkeyRunner.sleep(2)
#点击搜索框
device.touch(500,250,"DOWN_AND_UP")
MonkeyRunner.sleep(1)
#输入查询词
device.type("test")
MonkeyRunner.sleep(1)
#点击回车键进行搜索
device.press(‘KEYCODE_ENTER‘,‘DOWN_AND_UP‘)
MonkeyRunner.sleep(6)
#截图
image=device.takeSnapshot()
image.writeToFile(‘./test.png‘,‘png‘)补充说明:
monkeyrunner需依靠python多线程或循环操作执行重复的操作过程