主要參考了這篇文章,感謝做者的貢獻php
http://forum.xda-developers.com/showthread.php?t=2566234html
1.Monkeyrunnerpython
Monkeyrunner是一個自動化的測試工具,咱們用它作產品的黑盒測試。它提供了Python API使咱們不用修改Android代碼,而是用python程序來控制咱們的設備模擬器。咱們能夠用python來安裝應用,測試包,運行,發起按鍵事件,獲取並保存用戶界面的截圖。android
Monkeyrunner工具設計的主要目的,是在框架的層面上測試應用和設備併爲運行單元測試套件。固然,你也能夠用它來作其餘的事。框架
Monkeyrunner官方網站eclipse
http://developer.android.com/tools/help/monkeyrunner_concepts.htmlide
2.基本條件:工具
1. Windows 7/8 OS單元測試
2. Java 6測試
3. Eclipse
4. Anroid設備最好要能運行 2.3.4及以上版本(非強制性)
5. Python
6. Jython
7. 一個待測試的應用
3.安裝
下載安裝Python:http://www.python.org/getit/
接下來經過eclipse的market place下載eclipse的PyDev插件
help->Install New Soft
配置 Jython interpreters:參考這篇博客(其中也包含了Pydev的安裝過程),我就不重複造車了
http://blog.csdn.net/zhubaitian/article/details/39803205
配置 Python interpreters:
Window->Preferences->PyDev->Interpreters->-Python interpreters
4.建工程
這樣最基本的配置就完成了,接下來新建一個文件
New -> PyDev Project -> 爲工程命名並選擇 Jython
以下:
右鍵你的工程,new-->PyDev-->new module
選擇empty
寫入代碼
關於monkeyrunner的語法,官網講的很清楚:
http://developer.android.com/tools/help/monkeyrunner_concepts.html
例如我本身的代碼以下
# Imports the monkeyrunner modules used by this program from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice from symbol import if_stmt from sys import exit # Connects to the current device, returning a MonkeyDevice object device = MonkeyRunner.waitForConnection() if not device: print("no device") exit # Installs the Android package. Notice that this method returns a boolean, so you can test # to see if the installation worked. device.installPackage('F:/android-space/OrangeLife_2.0/bin/OrangeLife_2.0.apk') # sets a variable with the package's internal name package = 'com.curry.ams' # sets a variable with the name of an Activity in the package activity = 'com.curry.ams.user.LoginActivity' # sets the name of the component to start runComponent = package + '/' + activity # Runs the component device.startActivity(component=runComponent) # Presses the Menu button device.touch(285,488, MonkeyDevice.DOWN_AND_UP) device.type('13313933088') device.touch(432,648, MonkeyDevice.DOWN_AND_UP) device.type('ysclyy2413567') device.touch(573,1133, MonkeyDevice.DOWN_AND_UP) # Takes a screenshot result = device.takeSnapshot() # Writes the screenshot to a file result.writeToFile('F:/android-space/shot1.png','png')
5.運行
運行前要先進行配置:
Run-->Extenal Tools-->Extenal Tools configration
Locations:這裏填的是你android sdk文件夾下,tool中的monkeyrunner.bat
Working Directory:你的工做目錄,你的.apk文件要在該工做目錄中
Arguments:要執行的.py文件的路徑
點擊run 就開始運行了