●基于控件id的MonkeyRunner进阶使用模块
•EasyMonkeyDevice •By
monkeyrunner
form com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice,By
device = MonkeyRunner.waitForConnection()
device.startActivity('com.android.calculator2/.Calculator')
重新打开命令行:
adb shell service call window 1 i32 4939 //启动设备上的viewserver
adb shell service call window 3 //检查viewserver有没有启动
表示已经启动,8个0就未启动
回到原先的命令行
easy = EasyMonkeyDevice(device)
进入sdk/tools目录,打开hierarchyviewer.bat,选择进程,点击load hierarchyview,
可以获取按键的id
easy.touch(By.id('id/digit9'),MonkeyDevice.DOWN_AND_UP)
easy.touch(By.id('id/mul'),MonkeyDevice.DOWN_AND_UP)
easy.touch(By.id('id/4'),MonkeyDevice.DOWN_AND_UP)
easy.touch(By.id('id/equal'),MonkeyDevice.DOWN_AND_UP)
easy.getFocusedWindowId() //目前Activity的id的参数
easy.visible(By.id('id/digit7')) //判断控件是否可见
说明7按键可见
easy.exists(By.id('id/digit1')) //判断按键是否存在
说明1按键是存在的
easy.getText(By.id('id/digit2')) //获取控件的文本
easy.locate(By.id('id/digit7'))//返回控件在应用里具体位置
横纵坐标宽度高度
•GetHierarchyViewer //获取HierarchyViewer控件树的清单,在此基础得到控件对象
hv=device.GetHierarchyViewer()
view=hv.findViewById('id/digit0') //得到0控件的对象信息
view.namedProperties.get('text:mText')//得到0按键的mText值