MonkeyRunner使用小结

最近在用MonkeyRunner做自动化测试。现把个人心得记录下来。

MonkeyRunner在mysdk/tools/目录下,为了方便,可以加到坏境变量PATH里.这里不再赘述。

关键命令使用方法如下:

#MonkeyRunner  
# Imports the monkeyrunner modules used by this program  
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage  
# Connects to the current device, returning a MonkeyDevice object  
device = 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 name  
package = 'com.example.android.myapplication'  
  
# sets a variable with the name of an Activity in the package  
activity = 'com.example.android.myapplication.MainActivity'  
  
# sets the name of the component to start  
runComponent = package + '/' + activity  
  
# Runs the component  
device.startActivity(component=runComponent)  
  
# Presses the Menu button  
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)  
  
# Takes a screenshot  
result = device.takeSnapshot()  
  
# Writes the screenshot to a file  
result.writeToFile('myproject/shot1.png','png')  
  
# 输入a s d  
device.type('asd')  
  
#如果不记得那么多的命令,可以使用recorder把可视化界面记录下来。  
#Monkey Recorder  
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder  
recorder.start(device)  

adb shell常用命令:
按下OK键	device.press('KEYCODE_DPAD_CENTER','DOWN_AND_UP')
长按某个按键:  device.drag((236,440),(236,440),2,10)
相应的按键对应的名称如下:
home键:KEYCODE_HOME
back键:KEYCODE_BACK
send键:KEYCODE_CALL
End键:	KEYCODE_ENDCALL
上导航键:KEYCODE_DPAD_UP
下导航键:KEYCODE_DPAD_DOWN
左导航	:KEYCODE_DPAD_LEFT
右导航键:KEYCODE_DPAD_RIGHT
OK键	:KEYCODE_DPAD_CENTER
上音量键:KEYCODE_VOLUME_UP
下音量键:KEYCODE_VOLUME_DOWN
power键	:KEYCODE_POWER
camera键:KEYCODE_CAMERA
meun键	:KEYCODE_MENU

参考文献:

http://developer.android.com/tools/help/monkeyrunner_concepts.html

posted on 2012-11-02 13:44  小尾巴猴子  阅读(169)  评论(0编辑  收藏  举报

导航