|
2#
楼主 |
发表于 2018-4-16 14:15:28
|
只看该作者
自己随心所以创建一些事件脚本,想做什么就可以做什么,通过MonkeyRecorder这个工具来操作设备界面,
事件编辑完后选择Export Actions,导出到我们tools目录下命名为:action.mr
我们看一下工具生成的action.mr脚本,如下:
- [java] view plain copy
- TOUCH|{'x':297,'y':533,'type':'downAndUp',}
- WAIT|{'seconds':2.0,}
- TOUCH|{'x':136,'y':278,'type':'downAndUp',}
- WAIT|{'seconds':2.0,}
- TOUCH|{'x':123,'y':356,'type':'downAndUp',}
- WAIT|{'seconds':10.0,}
- PRESS|{'name':'HOME','type':'downAndUp',}
- WAIT|{'seconds':2.0,}
- TOUCH|{'x':235,'y':720,'type':'downAndUp',}
- WAIT|{'seconds':2.0,}
- TOUCH|{'x':303,'y':630,'type':'downAndUp',}
- WAIT|{'seconds':2.0,}
- TOUCH|{'x':16,'y':71,'type':'downAndUp',}
- WAIT|{'seconds':2.0,}
- TOUCH|{'x':244,'y':735,'type':'downAndUp',}
- 然后需要制作一个运行这一系列动作的脚本:monkey_playback.py,保存到tools目录下:
- [java] view plain copy
- #!/usr/bin/env monkeyrunner
- # Copyright 2010, The Android Open Source Project
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- import sys
- from com.android.monkeyrunner import 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):
- for line in fp:
- (cmd, rest) = line.split('|')
- try:
- # Parse the pydict
- rest = eval(rest)
- except:
- print 'unable to parse options'
- continue
- if cmd 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()
复制代码
接下来运行我们的保存的脚本,然后,你就看到真机(模拟器),进行你刚才一样的操作~
[java] view plain copy
E:\android-sdk-windows\tools>monkeyrunner monkey_playback.py action.mr
我的脚本是执行一些press down 和press up动作,同时会有延迟,之后按下home按钮,最后打开桌面的短
信程序,并打开某一条信息~是不是有点像按键精灵的感觉?但是得依靠命令行执行脚本~~~
至此,monkey和monkeyrunner介绍完毕。
|
|