MonkeyRunner 综合实践
综合实践
测试场景
- 连接设备,自动安装并启动考研帮app
- 启动后登录账号(账号zxw1234 密码:zxw123456),然后截图并保存到指定文件位置。
思路分析
- 连接设备
- 安装app
- 启动app
- 输入用户名密码点击登录按钮
- 截图
脚本实现
kyb_login.py
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()
print(" install app")
device.installPackage(r'C:\Users\Shuqing\Desktop\kaoyan3.1.0.apk')
print("launch app...")
package='com.tal.kaoyan'
activity='com.tal.kaoyan.ui.activity.SplashActivity'
runComponent=package+'/'+activity
device.startActivity(component=runComponent)
mr.sleep(3)
print("touch cancel button")
device.touch(618,895,'DOWN_AND_UP')
mr.sleep(1)
print("touch skip button")
device.touch(804,67,'DOWN_AND_UP')
mr.sleep(1)
print("input username and password")
device.touch(57,373,'DOWN_AND_UP')
mr.sleep(2)
device.type('zxw1234')
device.touch(152,480,'DOWN_AND_UP')
mr.sleep(2)
device.type('zxw123456')
mr.sleep(2)
print("touch login button")
device.touch(331,634,'DOWN_AND_UP')
print("takeSnapshot")
screenshot=device.takeSnapshot()
screenshot.writeToFile(r'D:\monkeyrunner\kyb.png','png')
注意事项
方法调用错误
AttributeError: type object 'com.android.monkeyrunner.XXXXX' has no attribute XXXXXX
检查调用的方法名是否写错,特别是注意区分大小写。
字符编码错误
SyntaxError: Non-ASCII character in file 'E:\monkeyrunner_script\kyb.py', but no encoding declared;
需要在代码顶部补充 # -- coding: utf-8 -- 或者去掉代码中的中文字符