Android MonkeyRunner 工具回放讲解

来源:互联网 发布:高中刷题软件 编辑:程序博客网 时间:2024/05/29 15:25
 

MonkeyRunner

  如果,把现阶段的monkey比做是幼儿园的小孩,那么monkeyrunner就是一个初中生了…它支持,自己编写插件,控制事件,随时截图,简而言之,任何你在模拟器/设备中能干的事情,MonkeyRunner都能干,而且还可以记录和回放!!!

具体介绍…看官方文档.这里还是不重复造轮子

http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html

注意:android sdk r14并没有把一个关键的jar包放lib目录中,所以,将无法运行,…然后请将SDK TOOLS 直接更新到最新的R15

下面提供一些常用的脚本,自己看着来改吧..

monkey_recorder.py

monkey_placback.py

help.py

http://115.com/file/e6r0sln9#
monkeyrunner_py脚本.rar

虽然,少了些东西,但是,并不影响我们大部分的需要.接下来用一段典型的monkeyRunner代码讲解!

注意!如果monkeyrunner脚本文件要使用中文,记得格式保存为utf8,不然会ASCNII(忘了怎么拼写了..)无法支持错误

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#导入我们需要用到的包和类并且起别名
import sys
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner import MonkeyDevice as md
from com.android.monkeyrunner import MonkeyImage as mi
  
#connect device 连接设备
#第一个参数为等待连接设备时间
#第二个参数为具体连接的设备
device =mr.waitForConnection(1.0,'emulator-5554')
if not device:
    print>> sys.stderr,"fail"
    sys.exit(1)
#定义要启动的Activity
componentName='kg.monkey/.MonkeyActivity'
#启动特定的Activity
device.startActivity(component=componentName)
mr.sleep(3.0)
#do someting 进行我们的操作
#输入 a s d
device.type('asd')
#输入回车
device.press('KEYCODE_ENTER')
#return keyboard 点击返回用于取消等下看到截图的下方的白条
#device.press('KEYCODE_BACK')
#------
#takeSnapshot截图
mr.sleep(3.0)
result =device.takeSnapshot()
  
#save to file 保存到文件
result.writeToFile('takeSnapshot\\result1.png','png');

result1 

以上代码就是用monkeyrunner 实现操作特定操作以后,并且截图的功能,看上去貌似挺麻烦的…如果你有很多设备要一起测试,你就会发现以上代码是多么爽丫丫的事情.这个脚本的实质就是一个python脚本,懂点,python的朋友,可以利用这个实现非常强悍的功能.这里就打住了,各位想到什么好玩,实用的记得回复一下…大家一起交流.

monkeyRunner 的记录和回放

  前面讲的都是一些在命令行上的操作,我可记不住那么多的指令操作,我可不知道,我点击的这个点的坐标是多少,我多么希望,我能够在可视化界面里面讲我的操作记录下来,然后,直接重新播放,就像宏一样,我可以很高兴的告诉你,MonkeyRunner有这个功能实现起来也非常简单,我提供的打包文件中有一个,monkey_recorder.py,直接在命令行中打上:

?
1
monkeyrunner monkey_recorder.py

 

image

例如我们删掉我们刚才的asd字符串它就会记录下我们所有的操作!,就会看到如上截图!!!

ButtonDescriptionWait等待时间Press a Button发送,MENU,HOME,or SEARCH 按钮.Press,Down,or Up事件Type Something发送一些字符串Fling用来操作虚拟键盘
imageExport Action将我们的脚本导出来Refresh Display刷新当前界面

 

接下来运行我们的保存的脚本,然后,你就看到模拟器,进行你刚才一样的操作

?
1
monkeyrunner monkey_playback.py monkey_test.mr

打开我们的文件可以看到其实就是一些monkeyrunner的一些脚本

TOUCH|{'x':329,'y':132,'type':'downAndUp',}
TOUCH|{'x':100,'y':100,'type':'downAndUp',}
TOUCH|{'x':296,'y':407,'type':'downAndUp',}
TOUCH|{'x':296,'y':407,'type':'downAndUp',}
TOUCH|{'x':296,'y':407,'type':'downAndUp',}
TOUCH|{'x':296,'y':407,'type':'downAndUp',}
TOUCH|{'x':351,'y':227,'type':'downAndUp',}

 

当然,有界面为什么不用呢~~~呵呵~

补充一点:如果我们要进行多设备测试怎么办呢?

我们可以打开monkey_playback.py文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
importsys
from com.android.monkeyrunnerimport MonkeyRunner
  
# The format of the file we are parsing is very carfeully constructed.
# Each line corresponds to a single command.  The line is split into 2
# parts with a | character.  Text to the left of the pipe denotes
# which command to run.  The text to the right of the pipe is a python
# dictionary (it can be evaled into existence) that specifies the
# arguments for the command.  In most cases, this directly maps to the
# keyword argument dictionary that could be passed to the underlying
# command. 
  
# Lookup table to map command strings to functions that implement that
# command.
CMD_MAP = {
    'TOUCH': lambda dev, arg: dev.touch(**arg),
    'DRAG': lambda dev, arg: dev.drag(**arg),
    'PRESS': lambda dev, arg: dev.press(**arg),
    'TYPE': lambda dev, arg: dev.type(**arg),
    'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)
    }
  
# Process a single file for the specified device.
def process_file(fp, device):
    forline in fp:
        (cmd, rest) = line.split('|')
        try:
            # Parse the pydict
            rest =eval(rest)
        except:
            print'unable to parse options'
            continue
  
        ifcmd not in CMD_MAP:
            print'unknown command: ' + cmd
            continue
  
        CMD_MAP[cmd](device, rest)
  
  
def main():
    file= sys.argv[1]
    fp =open(file,'r')
    #在这里指定你的设备吧
    device = MonkeyRunner.waitForConnection()
      
    process_file(fp, device)
    fp.close();
      
  
if __name__ == '__main__':
    main()

小结

至此,monkeyrunner的常用方式就这样完了,这里不打算说怎么编写一个自己的monkeyrunner插件,因为,我觉得我以上介绍的功能在实际开发中基本够用,而且,monkeyrunner估计,在下一个版本中会有一些更新,有兴趣的同学,自己查阅官方文档,当然,也可以联系本人…

原创粉丝点击