標籤:android測試 jython monkeyrunner
monkeyrunner是Android提供的自動化測試載入器,而且它還提供了Python介面,使我們能夠使用程式控制應用的行為,例如:安裝應用,根據包名啟動應用,點擊、滑動事件等。在eclipse中使用monkeyrunner需要一些配置:
1.下載並安裝python;
2.eclipse中安裝python外掛程式;
3.下載並安裝jython.jar;
4.配置eclipse;
由於前3步都比較簡單,這裡就不過多描述了,直接第4步。
a.配置python解譯器,點擊Window->Preferences->PyDev->Interpreters,如所示:
b.配置jython,點擊Window->Preferences->PyDev->Interpreters,另外,還要把monkeyrunner.jar添加到Libraries中,如所示:
c.接下來就可以建立jython項目,
d.最後就是建立python代碼,一段簡單的代碼,從Android官網上拷貝的,這段程式碼封裝括行為有:安裝MyApplication.apk,開啟MainActivity,點擊Menu
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice# Connects to the current device, returning a MonkeyDevice objectdevice = MonkeyRunner.waitForConnection()# Installs the Android package. Notice that this method returns a boolean, so you can test# to see if the installation worked.device.installPackage('myproject/bin/MyApplication.apk')# sets a variable with the package's internal namepackage = 'com.example.android.myapplication'# sets a variable with the name of an Activity in the packageactivity = 'com.example.android.myapplication.MainActivity'# sets the name of the component to startrunComponent = package + '/' + activity# Runs the componentdevice.startActivity(component=runComponent)# Presses the Menu buttondevice.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)# Takes a screenshotresult = device.takeSnapshot()# Writes the screenshot to a fileresult.writeToFile('myproject/shot1.png','png')e.運行代碼,實際上我執行這段代碼,要靠Android提供的monkeyrunner.bat的,所以不能像平常運行程式一樣,右鍵->Run->選擇執行程式,這時候要用到eclipse的External Tools,點擊Run->External Tools->External Tools Configurations,然後添加相關配置資訊,如:
以後,每次執行的時候,就不用重新添加了。
參考資料:http://forum.xda-developers.com/showthread.php?t=2566234
monkeyrunner on eclipse