MonkeyRunner--小结
#monkeyrunner导入模块
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
#monkeyrunner连接设备
device = MonkeyRunner.waitForConnection()
if not device:
print "Please connect a device to start!"
else:
print "Start "
#monkeyrunner启动一个Activity
componentName="com.tencent.mobileqq/com.tencent.mobileqq.activity.SplashActivity"
device.startActivity(component=componentName)
#monkeyrunner按键
发送指定键的关键事件: device.press(参数1:键码, 参数2:触摸事件类型)
参数1:常用键内容:
相应的按键对应名称:
#monkeyrunner卸载包
device.removePackage ('com.example.android.notepad')
print ('卸载成功')
#monkeyrunner安装包
device.installPackage('ApiDemos.apk')
print ('安装成功')
#monkeyrunner单击控件
方式1:device.touch(507,72,"DOWN_AND_UP")
方式2:easy_device.touch(By.id('id/qingchu'),device.DOWN_AND_UP)
用后者(方式2)需要导入:
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
#根据ID找到ViewNode,对viewnode的一些操作等
from com.android.monkeyrunner.easy import EasyMonkeyDevice
#提供了根据ID进行访问方法touch、drag等
from com.android.monkeyrunner.easy import By
#根据ID返回PyObject的方法
from com.android.hierarchyviewerlib.models import ViewNode as vn
#代表一个控件,可获取控件属性
#monkeyrunner长按控件
方式1:
device.touch(507,72,MonkeyDevice.DOWN)
MonkeyRunner.sleep(1)
device.touch(507,72,MonkeyDevice.UP)
方式2:
easy_device.touch(By.id('id/qingchu'),MonkeyDevice.DOWN)
MonkeyRunner.sleep(1)
easy_device.touch(By.id('id/qingchu'),MonkeyDevice.UP)
用后者需要导入
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
#根据ID找到ViewNode,对viewnode的一些操作等
from com.android.monkeyrunner.easy import EasyMonkeyDevice
#提供了根据ID进行访问方法touch、drag等
from com.android.monkeyrunner.easy import By
#根据ID返回PyObject的方法
from com.android.hierarchyviewerlib.models import ViewNode as vn
#代表一个控件,可获取控件属性
#monkeyrunner滑动屏幕
for i in range(1,70):
device.drag((250,110),(250,850),0.1,10)
MonkeyRunner.sleep(1)
#monkeyrunner延时
MonkeyRunner.sleep(3)
#monkeyrunner截图
result = device.takeSnapshot()
result.writeToFile('C:\\Users\\Martin\\Desktop\\test.png','png')
#monkeyrunner截图对比
result1.sameAs(result0,1.0)
#monkeyrunner局部图片(前两个值是左上角左边,后两个值是右下角减左上角的坐标。)
pic0= result0.getSubImage((4,41,400,700))
#monkeyrunner重启设备
device.reboot()
#monkeyrunner单击电源键,熄灭屏幕
device.press('KEYCODE_POWER',MonkeyDevice.DOWN_AND_UP)
#monkeyrunner唤醒屏幕
device.wake()
#monkeyrunner输入文本
Cotent='1234'
device.type(Cotent)
#monkeyrunner获取文本
方式1:#通过By来获取
text1=easy_device.getText(By.id('id/text'))
print text1.encode('utf-8')
前提:
from com.android.monkeyrunner.easy import EasyMonkeyDevice
#提供了根据ID进行访问方法touch、drag等
from com.android.monkeyrunner.easy import By
#根据ID返回PyObject的方法
easy_device = EasyMonkeyDevice(device)
#初始化EasyMonkeyDevice模块,必须放在startActivity之后,用来通过ID访问控制
方式2:通过 HierarchyViewer
content = hViewer.findViewById('id/text')
text0 = hViewer.getText(content)
print text0.encode('utf-8')
前提:
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
#根据ID找到ViewNode,对viewnode的一些操作等
from com.android.hierarchyviewerlib.models import ViewNode as vn
#代表一个控件,可获取控件属性
hViewer = device.getHierarchyViewer()
# 对当前UI视图进行解析
网友评论