[android]android自動化測試四之Monkey與MonkeyRunner

大搜車-自娛發表於2012-02-02
請大家用下面的範例來做Monkey test . 時間延遲設為300毫秒,基本上符合實際上的觸控間隔。各位只需要改變標記為黃色的部分。

adb shell monkey -p com.xxxx.news -s 12345 --pct-trackball 0 --pct-nav 0 --throttle 300 --pct-anyevent 4 --ignore-crashes 30000 &
adb shell monkey -p com.xxxx.systeminfo -s 12345 --pct-trackball 0 --pct-nav 0 --throttle 300 --pct-anyevent 4 --ignore-crashes 30000 &
adb shell monkey -p com.xxxx.todo -s 12345 --pct-trackball 0 --pct-nav 0 --throttle 300 --pct-anyevent 4 --ignore-crashes 30000


具體引數檢視
http://developer.android.com/guide/developing/tools/monkey.html


一個簡單的monkey指令碼:

# monkey
tap 100 180
type 123
tap 100 280
press DEL
press DEL
press DEL
press DEL
press DEL
press DEL
press DEL
press DEL
type -460.3


呼叫monkey指令碼
$ adb shell am start -n com.example.aatg.tc/.TemperatureConverterActivity
This is informed in the log by this line:
Starting: Intent { cmp=com.example.aatg.tc/.TemperatureConverterActivity }
Once the application has started you can send the events using the script and the
netcat utility:
$ nc localhost 1080 < monkey.txt


一個monkeyRunner的指令碼:monkey_playback.py


#! /usr/bin/env monkeyrunner
'''
Created on 2011-03-12
@author: diego
'''
import sys
# 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()
if not device:
print >> sys.stderr, "Couldn't get connection"
sys.exit(1)
device.startActivity(component='com.example.aatg.tc/.
TemperatureConverterActivity')
MonkeyRunner.sleep(3.0)
device.type("123")
# Takes a screenshot
MonkeyRunner.sleep(3.0)
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('/tmp/device.png','png')
device.press('KEYCODE_BACK', 'DOWN_AND_UP')

更詳細的文章
http://www.cnblogs.com/yyangblog/archive/2011/03/10/1980086.html

相關文章