MonkeyRunner
介绍
monkey是直接运行在设备的adb shell中的一个工具 monkeyRunner工具给提供了一些API,可以通过调用这些API编写python脚本实现自动化测试:实现模拟点击、截图等操作,其主要功能是用于自动化测试
MoneyRunnerAPI
from com.android.monkeyrunner import MonkeyRunner
MonkeyRunner.alert("Hello", "This is title", "ok")
执行结果:
- waitForConnection:等待设备连接,有多个device id,需要指明是哪个设备
MoneyDeviceAPI
-
drag:拖动 drag(tuple start, tuple end, float duration, integer steps) start:起点位置 end:终点位置 duration:手势持续时间 steps:插值点的步数,默认10 -
press press(string keycode, dictionary type) keycode:按键 type:Down、UP、DOWN_AND_UP -
startActivity:启动应用 startActivity(package+"/"+activity) -
touch:点击 touch(x, y, type) x 坐标值 y 坐标值 type:Down、UP、DOWN_AND_UP -
type:输入 type(string message) -
takeSnapshot:截屏 MonkeyImage takeSnapshot()
MoneyImageAPI
-
sameAs:图像对比 boolean sameAs(MonkeyImage other, float percent) -
writetoFile:保存图像文件 void writeToFile(String path, string format)
具体实践
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
device = MonkeyRunner.waitForConnection(3, device)
device.startActivity("packageName/launchActivity")
MonkeyRunner.sleep(2)
device.touch(100, 100, "DOWN_AND_UP")
device.type("test")
image = device.takeSnapshot()
image.writeToFile("./test.png", "png")
执行脚本
monkeyrunner test.py
KeyCode
home键 KEYCODE_HOME
back键 KEYCODE_BACK
send键 KEYCODE_CALL
end键 KEYCODE_ENDCALL
上导航键 KEYCODE_DPAD_UP
下导航键 KEYCODE_DPAD_DOWN
左导航 KEYCODE_DPAD_LEFT
右导航键 KEYCODE_DPAD_RIGHT
ok键 KEYCODE_DPAD_CENTER
上音量键 KEYCODE_VOLUME_UP
下音量键 KEYCODE_VOLUME_DOWN
power键 KEYCODE_POWER
camera键 KEYCODE_CAMERA
menu键 KEYCODE_MENU
|