MonkeyRunner的API(2)
2022-01-21 本文已影响0人
Chaweys
4、MonkeyImage类
MonkeyImage类在测试过程中用来保存各种格式的测试截图,并可以进行图像对比
convertToBytes(string format) 转换图像格式
getRawPixel(integer x, integer y) 获取当前坐标像素元组
getRawPixelInt(integer x, integer y) 获取当前坐标像素值
getSubImage(tuple rect) 截取子图像
sameAs(MonkeyImage other, float percent) 图像对比
writeToFile(string path, string format) 保存图像文件
常用方法:
takeSnapshot() 进行屏幕截图
writeToFile(string path, string format) 保存图像文件
综合举例:
连接设备,自动安装并启动考研帮app,
输入账户(zxw123/zxw123)登录,然后截图保存
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 device")
device = mr.waitForConnection(1,'127.0.0.1:62001')
print("install app")
device.installPackage(r"C:\Users\chawey\Desktop\kaoyanbang.opdown.com.apk")
mr.sleep(5)
print("launch app")
packageName = 'com.tal.kaoyan'
activity = 'com.tal.kaoyan.ui.activity.SplashActivity'
component = packageName+'/'+activity
device.startActivity(component=component)
mr.sleep(5)
print("touch cancel update button")
device.touch(618,895,'DOWN_AND_UP')
mr.sleep(2)
print("touch skip button")
device.touch(804,67,'DOWN_AND_UP')
mr.sleep(2)
print("input username and password")
device.touch(57,373,'DOWN_AND_UP')
mr.sleep(2)
device.type("zxw123")
device.touch(152,480,'DOWN_AND_UP')
mr.sleep(2)
device.type("zxw123")
print("take screenshot")
screenshot = device.takeSnapshot()
screenshot.writeToFile(r"D:\monkeyrunner\kyb.png","png")