采用MonkeyRunner自动化测试(一)

时间:2023-01-08 05:40:18

我目前使用的SDK版本是R14

在使用monkeyrunner之前,请大家到这里  http://www.skycn.com/search.php?ss_name=activepython&sf=index 下载activepython 并安装好它。

在命令行运行monkeyrunner.bat  (这个批处理在android_sdk_windows\tools目录下)

android_sdk_windows\tools\monkeyrunner.bat  运行之后,如果提示如下:

Exception in thread "main" java.lang.NoClassDefFoundError: com/android/chimpchat/ChimpChat
        at com.android.monkeyrunner.MonkeyRunnerStarter.<init>(MonkeyRunnerStarter.java:60)
        at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:188)
Caused by: java.lang.ClassNotFoundException: com.android.chimpchat.ChimpChat
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more

说明SDK中缺少了 ChimpChat.jar,大家可以去 http://code.google.com/p/aster/downloads/list 下载一个最新的aster 的压缩包。这个aster 的意思是 Android System Testing Environment and Runtime. 在这个压缩包内有 aster\dist\jar\chimpchat.jar  请将这个jar包复制到你的SDK\tools\lib 目录。

然后将你的设备打开调试模式,连上电脑

你可以编写一个monkeyrunner的脚本如下:

# This is a monkeyrunner jython script that opens a connection to an Android
# device and continually sends a stream of swipe and touch gestures.
#
# See http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html
#
# usage: monkeyrunner swipe_monkey.py
#

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Connects to the current device
device = MonkeyRunner.waitForConnection()

# A swipe left from (x1, y) to (x2, y) in 2 steps
y = 400
x1 = 100
x2 = 300
start = (x1, y)
end = (x2, y)
duration = 0.2
steps = 2
pause = 0.2

for i in range(1, 250):
# Every so often inject a touch to spice things up!
if i % 9 == 0:
device.touch(x2, y, 'DOWN_AND_UP')
MonkeyRunner.sleep(pause)
# Swipe right
device.drag(start, end, duration, steps)
MonkeyRunner.sleep(pause)
# Swipe left
device.drag(end, start, duration, steps)
MonkeyRunner.sleep(pause)


在命令行运行 monkeyrunner.bat swip_monkey.py

你就会看到你的设备正在按照你编写的monkeyrunner脚本进行自动测试。

如果monkeyrunner连不上你的设备,据网上别的文章说,需要先打开你的模拟器连接一次