●基於控件id的MonkeyRunner進階使用模塊android
•EasyMonkeyDevice •Bygit
monkeyrunnershell
form com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage命令行
from com.android.monkeyrunner.easy import EasyMonkeyDevice,By3d
device = MonkeyRunner.waitForConnection()orm
device.startActivity('com.android.calculator2/.Calculator')server
從新打開命令行:對象
adb shell service call window 1 i32 4939 //啓動設備上的viewserverblog
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值