monkeyrunner简单用法

  1. MonkeyRunner主要有三个类:

    MonkeyRunner

    MonkeyDevice

    MonkeyImage

  2. MonkeyRunner类:

    1. void alert (string message, string title, string okTitle)

      弹出一个对话框,默认标题“警告”,按钮显示“确定”,可暂停当前的程序

      例:MonkeyRunner.alert("hello world") 弹出一个hello world对话框

      2. integer choice (string message, iterable choices, string title)

      显示带有一列可选项的对话框,可暂停当前的程序,选择后返回integer对象,代表选择项的序列index

      例:MonkeyRunner.choice("choice a sex",["man","women"])  弹出一个选择性别的对话框,选择man会返回0,选择women会返回2

      3. string input (string message, string initialValue, string title, string okTitle, string cancelTitle)

      显示一个输入框,接受输入后返回字符串

      例:MonkeyRunner.input("enter text")   弹出一个输入框,输入后确认返回字符串

      4. void sleep(float seconds)

      暂停当前程序指定秒

      例:MonkeyRunner.sleep(2)   暂停当前程序2秒

      5. MonkeyDevice waitForConnection (float timeout, string deviceId)

timeout

等待连接时间,默认为一直等待.

deviceId

设备名,可通过adb devices查看当前连接设备名,一般可以不指定.

      返回一个MonkeyDevice 实例常用写法为:

      device = MonkeRunner.waitForConnection()

      6. 通过获取到的device对象,可以调用MonkeyDevice的方法对设备进行操作。

  3.  MonkeyDevice类:

    1. void drag (tuple start, tuple end, float duration, integer steps)

      例:device.drag((120,240),(200,240),1,10)

      屏幕解锁操作,从(120,240)向(200,240)滑动,用时1秒,10为默认值

      2. object getProperty (string key)

      查询设备相关信息

      3. void installPackage (string path)

      安装指定路径的APK文件。如果改文件已安装,则会覆盖安装

      例:device.installPackage(r"d:/2100.apk")

      安装D盘2100.apk,路径中使用 /

      4. void press (string name, dictionary type)

      模拟按钮操作。

      name 为按键码, type 为键盘事件类型. 可用的值有DOWN, UP, 和DOWN_AND_UP

      5. void removePackage (string package)

      删除指定的包,包括清除其数据和缓存

      例:device.removePackage("evertone.Piano")  卸载piano程序,程序名通过aapt查看2100.apk文件,或者在shell模式下进入data/data目录下查找

      6. void shell (string cmd)

      执行shell命令并返回结果

      例:device.shell("date")  执行shell命令,查询日期

      7. void startActivity (string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, flags)

      启动一个应用,对于需要测试的应用需要知道应用名和入口activity名称。可以调用sdk\builds-tools目录下的aapt获取aapt dump baging d:\2100.apk

      startActivity常用形式为:

      例:device.startActivity(component = "evertone.Piano/evertone.Piano.loading")  执行后启动手机弹钢琴

  4. MonkeyImage类:  

      1. takeSnapshot()

      屏幕截图,返回包含当前显示截图的MonkeyImage对象

      例:pic = MonkeyDevice.takeSnapshot()

      2. string convertToBytes (string format)

      将当前图像转换成特定格式,并且作为字符串返回

      3. tuple getRawPixel (integer x, integer y)

      返回图像位置坐标(x,y)上的单个像素点,作为一个整数元组,以(a,r,g,b)格式。可对特定点进行取色

      4. MonkeyImage getSubImage (tuple rect)

      从当前图片中取出部分区域,创建一个新的MonkeyImage对象。参数为元祖,指定截图区域

      例:pic_new=pic.getSubImage((0,0,100,100))

      5. boolean sameAs (MonkeyImage other, float percent)

      对比两个MonkeyImage对象是否相等(比较截图是否一致)。参数percent指定两个图像之间差异在多少百分比之内可以看做“相等”。可进行自动化结果校验

      例:pic2.sameAs(pic1,0.9)

      pic2和pic1在90%范围内相似,则返回True,否则返回False

      6. void writeToFile (string path, string format)

      指定路径和格式,保存图片文件

      例:pic1.writeToFile("d:/pic2.png","png")

      指定图片格式为png,保存在本地d:/pic2.png路径。保存成功则返回True

  5. 运行脚本代码:

      在cmd中执行:monkeyrunner <包括路径的脚本名.py>

posted @ 2019-06-18 16:13  不上进的小猫  阅读(564)  评论(0编辑  收藏  举报