Tuesday, March 4, 2014

MonkeyRunner简介(1)

MonkeyRunner是Android app的测试框架,可提供自动和app交互的功能,既然是测试框架,我们就可以用于收集数据。


安装:
1. PyDev Project (check updates to install it) [http://pydev.org/updates]. This is for python projects.
2. New a PyDev project->Project Type is JyPhon->Config interpreter->Download Jython lib (http://www.jython.org/downloads.html) [jython-standalone-2.5.3.jar]
3. Add monkeyRunner.jar to project: right click the proj->property->PyDev - PYTHONPATH->External libraries->add monkeyrunner.jar located in Android SDK path (open Android SDK manager[in window menu in eclipse] and SDK path and search it in this path)

Start to write your python code:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
in a .py file
or open PyDev Console [in the Console window] and put the code there






There is an error while you run "device = MonkeyRunner.waitForConnection()". It throws up NULL pointer error.
So I run it in MoneyRunner.bat with the python file
1. The batch file is in Android SDK path: C:\Users\KK\AppData\Local\Android\android-sdk\tools and you put your a.py file there.
2.  In cmd, run monkeyrunner a.py

a.py:
Snake:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
device = MonkeyRunner.waitForConnection()
device.installPackage('C:/Users/KK/Documents/Hard-Disk_bk/Backup/Study/coding/Java/Snake/bin/Snake.apk')
device.startActivity(component="com.example.android.snake/.Snake")
MonkeyRunner.sleep(5)
result=device.takeSnapshot()
result.writeToFile('C:\\Users\\KK\\Documents\\Coding\\Python\\Test1_001.png','png')
device.removePackage('com.example.android.snake') 

vTasters:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
device = MonkeyRunner.waitForConnection()
device.installPackage('C:/Users/KK/Documents/Hard-Disk_bk/Backup/Study/coding/Java/Snake/bin/vTasters-android.apk')
device.startActivity(component="com.vtasters/.ViewsMenu")
MonkeyRunner.sleep(5)
result=device.takeSnapshot()

result.writeToFile('C:\\Users\\KK\\Documents\\Coding\\Python\\Test1_001.png','png')



To fill out the package/activity name, you need to open the AndroidManifest.xml to check them out.
It will generate a snapshot for your device. Snake is an example app from Android.

Interact with your app:

device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
MonkeyRunner.sleep(2)
device.press("DPAD_CENTER", MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(2)





It will select the second item in the menu and click it.
References:
http://www.cnblogs.com/yyangblog/archive/2011/03/10/1980086.html
http://www.cnblogs.com/findyou/p/3420936.html

Download the apk

http://www.apkfiles.com/apk-46/pandora-internet-radio

To decode the AndroidManifest.xml

From http://stackoverflow.com/questions/4191762/how-to-view-androidmanifest-xml-from-apk-file, download the apktool.jar

Execute in CMD: >apktool.jar d Pandora_apkfiles.com.apk ./Pandora
The AndroidManifest.xml will be decrypted.

下一篇将会介绍实际App的完整测试:MonkeyRunner简介(2)

No comments:

Post a Comment