之前的系列給出了Appium,Robotium,Instrumentation和UIAutomator創建一個Note實例的例子:
- 《[Appium創建一個Note的實例](http://blog.csdn.net/zhubaitian/article/details/39502069)》
- 《[Robotium創建一個Note的實例](http://blog.csdn.net/zhubaitian/article/details/39502119)》
- 《[UIAutomator創建一個Note的實例](http://blog.csdn.net/zhubaitian/article/details/39508513)》
- 《[SDK Instrumentation創建一個Note的實例](http://blog.csdn.net/zhubaitian/article/details/39546371)》
那么用MonkeyRunner又是如何實現這些功能的呢?今天花了點時間學習了下MonkeyRunner的基本API然后嘗試實現了該功能,給我作為一個初學者的感觸如下:
- MonkeyRunner可以通過坐標點擊對象,在引入EasyMonkeyDevice后可以根據ID進行點擊
- Eclipse上Jython代碼很多對象沒有成員函數提示(jar包以導入),如MonkeyRunner.waitForConnection獲得device對象后,后面devie.不能自動提示可用成員函數
- 感覺腳本跑得很慢
- 沒有任何junit的繼承,應該可以通過Junit4框架來使用MonkeyRunner,下次有時間再嘗試下
- 也許是不熟悉,感覺跟Robotium,UIAutomator等的編寫效率差一些
~~~
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
from com.android.monkeyrunner.easy import EasyMonkeyDevice,By
#Connect to the target device
device = MonkeyRunner.waitForConnection("10000", "emulator-5554")
easy_device = EasyMonkeyDevice(device) #touch a button by id would need this
device.startActivity(component="com.example.android.notepad/com.example.android.notepad.NotesList")
#time.sleep(2000)
#invoke the menu options
MonkeyRunner.sleep(3)
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP);
#Touch on the "Add note" menu entry by coordinate
MonkeyRunner.sleep(3)
device.touch(118,253,MonkeyDevice.DOWN_AND_UP)
#Type in the text for the note
MonkeyRunner.sleep(3)
device.type('Note1')
#easy_device.type(By.id('id/note'),'Note2')
#invoke the menu options
MonkeyRunner.sleep(3)
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP);
#Touch on the "save" menu entry by coordinate
MonkeyRunner.sleep(3)
device.touch(59,257,MonkeyDevice.DOWN_AND_UP)
#Simulate long press on the new added note by id with EasyMonkeyDevice
MonkeyRunner.sleep(3)
easy_device.touch(By.id('id/text1'),MonkeyDevice.DOWN) #Touch down for 10 seconds
MonkeyRunner.sleep(10)
easy_device.touch(By.id('id/text1'),MonkeyDevice.UP) #Then release the touch
#Touch on the "delete" menu entry of the context menu options to delete the note
MonkeyRunner.sleep(6)
device.touch(84,172,MonkeyDevice.DOWN_AND_UP)
~~~
- 前言
- MonkeyRunner創建一個Note的實例
- MonkeyRunner在Windows下的Eclipse開發環境搭建步驟(兼解決網上Jython配置出錯的問題)
- MonkenRunner通過HierarchyViewer定位控件的方法和建議(Appium/UIAutomator/Robotium姊妹篇)
- MonkeyDevcie API 實踐全記錄
- MonkeyImage API 實踐全記錄
- EasyMonkeyDevice vs MonkeyDevice&HierarchyViewer API Mapping Matrix
- adb概覽及協議參考
- MonkeyRunner源碼分析之-誰動了我的截圖?
- MonkeyRunner源碼分析之與Android設備通訊方式
- MonkeyRunner源碼分析之啟動
- Monkey源碼分析之運行流程
- Monkey源碼分析之事件源
- Monkey源碼分析番外篇之WindowManager注入事件如何跳出進程間安全限制
- Monkey源碼分析番外篇之Android注入事件的三種方法比較
- Monkey源碼分析之事件注入
- monkey源碼分析之事件注入方法變化
- MonkeyRunner源碼分析之工作原理圖
- Android自動化測試框架新書:<<MonnkeyRunner實現原理剖析>>交流