可以写一个pyhon工程,安装在android进行测试,还可以截屏操作。Monkeyrunner为framework层开发。MonkeyRunner本身是Java做的,为了和Python连接,做了一个叫Jython的东东,
1. 多种多设备控制
2. 功能测试
3. 回归测试
4. 可扩展功能测试
官网提供的最简单的例子:
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# 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. 此处取apk的绝对路径
device.installPackage('myproject/bin/MyApplication.apk')
# sets a variable with the package's internal name,给apk所在的包取出来,赋给变量package
package = 'com.example.android.myapplication'
# sets a variable with the name of an Activity in the package ,得到Activity并存在变量中
activity = 'com.example.android.myapplication.MainActivity'
# sets the name of the component to start,这个和Instrumentation很类似啊,包名/Activity名
runComponent = package + '/' + activity
# Runs the component ,没有使用device.touch
device.startActivity(component=runComponent)
# Presses the Menu button,这种press是按下、起来
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')
主要API
com.android.monkeyrunner.MonkeyDevice.drag
com.android.monkeyrunner.MonkeyDevice.touch 传touch的点的(x,y)坐标
实践一个调用calculator100次的script,可以work。calculator的包名可以用ddms监控得到(eclipse - window - open perspective -ddms )。使用PowerShell操作命令行感觉真好啊~
#coding=utf-8
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
device = MonkeyRunner.waitForConnection()
device.shell("monkey -p com.android.calculator2 -v 1")
def tapButton(Number):
import random
for i in range(0,Number):
keyValue = random.randint(1,20)
if keyValue == 1:
device.touch(200,265,'DOWN_AND_UP') #Tap 'Clear' Button
elif keyValue == 2:
device.drag((200,265),(200,265),1.5,10) #Long touch "Clear" button;
elif keyValue == 3:
device.touch(465,275,'DOWN_AND_UP') # Tap "Delete" button;
elif keyValue == 4:
device.drag((465,275),(465,275),1.5,10) #Long touch "Delete" button;
elif keyValue == 5:
device.touch(70,430,'DOWN_AND_UP') #Tag "7" button;
elif keyValue == 6:
device.touch(220,430,'DOWN_AND_UP') #Tap "8" button;
elif keyValue == 7:
device.touch(340,430,'DOWN_AND_UP') #Tap "9" button;
elif keyValue == 8:
device.touch(470,430,'DOWN_AND_UP') #Tap "division" button;
elif keyValue == 9:
device.touch(70,580,'DOWN_AND_UP') #Tap "9" button;
elif keyValue == 10:
device.touch(220,580,'DOWN_AND_UP') #Tap "9" button;
elif keyValue == 11:
device.touch(340,580,'DOWN_AND_UP') #Tap "9" button;
elif keyValue == 12:
device.touch(470,580,'DOWN_AND_UP') #Tap "9" button;
elif keyValue == 13:
device.touch(70,730,'DOWN_AND_UP') #Tap "9" button;
elif keyValue == 14:
device.touch(220,730,'DOWN_AND_UP') #Tap "9" button;
elif keyValue == 15:
device.touch(340,730,'DOWN_AND_UP') #Tap "-" button;
elif keyValue == 16:
device.touch(470,730,'DOWN_AND_UP') #Tap "." button;
elif keyValue == 17:
device.touch(70,875,'DOWN_AND_UP') #Tap "9" button;
elif keyValue == 18:
device.touch(220,875,'DOWN_AND_UP') #Tap "0" button;
elif keyValue == 19:
device.touch(340,875,'DOWN_AND_UP') #Tap "=" button;
elif keyValue == 20:
device.touch(470,875,'DOWN_AND_UP') #Tap "+" button;
else:
pass
MonkeyRunner.sleep(0.5)
tapButton(100)
drag\touch都是MonkeyDevice中的方法。MonkeyRunner有3个modules,也就是类: MonkeyDevice、MonkeyImage、MonkeyRunner
MonkeyRecorder可以跟踪手机状态。从android源码中剥离出monkeyrunner的源码,导入到工程里,创建一个test类,使用java借助monkeyrecorder玩一玩:手机上的各种key,在android.view中有KeyEvent.class,里面有keycode对应的数字,查看MonkeyDevice.type帮助文档,里面的keycode值就和这个对应。test的思路:new monkeyrecorder对象,等待device连接上,设置recorder frame可见。
Public class MyTest{ static IMonkeyDevice device; public static void main(String [] args){ device = new AdbBacken().waitforConnection();
if (device ! = null){
MonkeyRecorderFrame frame = new MonkeyRecorderFrame(device);
frame.setVisible(true);}
}
keyPress事件流其实就是调用了keyEvent里面的几个事件,复写了其方法
关于脚本移植后好用的问题:比如手机屏幕不同导致分辨率变更
320*480 -》 480*800 可以这样换算一下
device.touch(340,580,'DOWN_AND_UP')这句可以写成:320/x=480/newx;480/580=800/newY
Hierarchyviewer(tools里一个工具)获取布局,即各个控件的坐标,再定位;各种控件在底层也是获取坐标点。4.1以下需要root权限,4.1以上有ui工具了 http://blog.csdn.net/zm2714/article/details/7985061 这是使用hierachyviewer的心得文章
后续持续集成:
2014年1月推出RobotiumRecord工具,http://robotium.myshopify.com/products/robotium-recorder 可以直接录制,然后好歹改下脚本就能跑,很方便的
Robotium是基于控件的工具,对于跨应用比较弱,结合monkeyrunner这种跨应用强的工具,就算不用uiautomatic,也可以解决一般的APP测试问题
测试云:所有脚本保存在云端,云端可以做个数据库,比如mysql。通过云端发起远程控制你的终端,使用脚本发起一个事件流,通过monkeyrunner远程调用和控制;终端可以录制等工作,完成后传到云端进行管理。类似Itestin、易测云
用例管理、网页展示
**************
网上找到常用方法: