【转】Android自动化测试之MonkeyRunner录制和回放脚本(四)

测试脚本录制:

方案一:

我们先看看以下monkeyrecoder.py脚本:

  1. #Usage: monkeyrunner recorder.py
  2. #recorder.py  http://mirror.yongbok.net/linux/ ... ey_recorder.py;
  3. com.android.monkeyrunner import MonkeyRunner as mr
  4. com.android.monkeyrunner.recorder import MonkeyRecorder as recorder
  5. device = mr.waitForConnection()
  6. recorder.start(device)
  7. #END recorder.py

首先,连接你已经打开调试模式的ANDROID设备或模拟器,然后运行上面的脚本,例如在cmd窗口中执行命令: monkeyrunner monkeyrecoder.py

执行下面的代码后,将运行录制脚本的程序:

#Press ExportAction to save recorded scrip to a file

#Example of result:
#PRESS|{""name"":""MENU"",""type"":""downAndUp"",}
#TOUCH|{""x"":180,""y"":175,""type"":""downAndUp"",}
#TYPE|{""message"":"""",}

=================================================

这种脚本需要另外一个monkeyrunner的脚本来解释执行。monkeyplayback.py

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

=================================================

Usage:monkeyrunner playback.py "myscript"

时间: 2024-08-04 13:40:04

【转】Android自动化测试之MonkeyRunner录制和回放脚本(四)的相关文章

Android自动化测试之MonkeyRunner录制和回放脚本

Android自动化测试之MonkeyRunner录制和回放脚本(十一) 分类: 自动化测试 Android自动化 2013-02-22 10:57 7346人阅读 评论(2) 收藏 举报 androidAndroidANDROIDMonkeyRecordermonkeyrunnerMonkeyRunnerMonkeyrunner 对于MonkeyRunner,有些人可能会想,既然是Android自动化测试,离不开测试脚本,那么,我们可不可以录制测试脚本呢,答案是可以的. 我们先看看以下monk

[转] Android自动化测试之MonkeyRunner录制和回放脚本(四)

测试脚本录制: 方案一: 我们先看看以下monkeyrecoder.py脚本: #Usage: monkeyrunner recorder.py #recorder.py  http://mirror.yongbok.net/linux/ ... ey_recorder.py; com.android.monkeyrunner import MonkeyRunner as mr com.android.monkeyrunner.recorder import MonkeyRecorder as 

Android自动化测试之Monkeyrunner学习笔记(一)【转】

因项目需要,开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括Monkey.Monkeyrunner.Athrun.appium,目前仍在了解学习android测试框架.CTS等.本文主要为前段时间学习总结,由于刚接触不久,故本文难免会有些肤浅,大神请绕走~ [目录] 1.Monkeyrunner简介 2.Monkeyrunner工具特性 3.Monkeyrunner工具同Monkey工具的差别 4.运行Monkeyrunner 5.实例 6.学习笔记

[转] android自动化测试之MonkeyRunner使用实例(三)

一.使用CMD命令打开模拟器 运行monkeyrunner之前必须先运行相应的模拟器或连上设备,不然monkeyrunner无法连接设备. 1.1  用Elipse打开Android模拟器或在CMD中用Android命令打开模拟器. 1.2  这里重点讲一下在CMD中用Android命令打开模拟器 命令:emulator -avd test (注意:test为虚拟设备的名称——AVD的全称为:Android Virtual Device,就是Android运行的虚拟设备,如下图所示:) 上面命令

Android自动化测试之monkeyrunner工具

一.什么是monkeyrunner monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器.通过monkeyrunner,您可以写出一个Python程序去安装一个Android应用程序或测试包,运行它,向它发送模拟击键,截取它的用户界面图片,并将截图存储于工作站上.monkeyrunner工具的主要设计目的是用于测试功能/框架水平上的应用程序和设备,或用于运行单元测试套件,但您当然也可以将其用于其它目的. 二.monkeyr

Android自动化测试之——MonkeyRunner(1)

一.MonkeyRunner是什么 MonkeyRunner是使用Jython(使用Java编程语言实现的Python)写出来的,它提供了多个API,通过monkeyrunner API 可以写一个Python的程序来模拟操作控制Android设备app,测试其稳定性并通过截屏可以方便地记录出现的问题. 二.MonkeyRunner能干什么 1.多设备控制:monkeyrunner API可以跨多个设备或模拟器应用一个或多个测试套件.可以连接所有设备或同时启动所有仿真器(或两者),依次以编程方式

Android自动化测试之monkeyrunner基本要素(七)

1. #导入模块;     from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage 2. #连接当前设备,并返回一个MonkeyDevice对象;     device = MonkeyRunner.waitForConnection()     if not device:         print "Please connect a device to start!"     els

Android自动化测试之——MonkeyRunner(2)

一.MonkeyRunner API MonkeyRunner API包含了三个模块在com.android.monkeyruner包中: 1.MonkeyRunner 一类用于MonkeyRunner程序的实用方法.该类提供了一种将MonkeyRunner连接到设备或仿真器的方法.它还提供了为monkeyrunner程序创建UI以及显示内置帮助的方法 2.MonkeyDevice 表示设备或仿真器.这个类提供了安装和卸载包.启动Activity.以及向app发送键盘或触摸事件的方法.您还可以使

android自动化测试之Monkey--从参数讲解、脚本制作到实战技巧

金阳光视频: http://v.youku.com/v_show/id_XODcyMjM1MDA4.html?from=y1.2-1-87.4.4-1.1-1-2-3 1.安装jdk,sdk,TT 2.对app进行压力测试(性能测试),模拟器和真机都可以,发送伪随机事件流(一段时间内不重复)(随机事件流:抛硬币) 3.monkey是android系统自带的,位置/system/bin/monkey/monkey.jar 4.adb shell monkey +选项 adb shell进入shel