zoukankan      html  css  js  c++  java
  • MonkeyRunner(猴哥快跑)常用类方法总结

    MonkeyRunner的一些常用方法:

    (详细参考:http://developer.android.com/tools/help/MonkeyRunner.html

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

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

    例:MonkeyRunner.alert("hello world")

     

    弹出一个hello world对话框

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

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

    例:MonkeyRunner.choice("choice a sex",["man","women"])

     

    弹出一个选择性别的对话框,选择man会返回0,选择women会返回2

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

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

    例:MonkeyRunner.input("enter text")

     

    弹出一个输入框,输入后确认返回字符串

    void sleep(float seconds)

    暂停当前程序指定秒

    例:MonkeyRunner.sleep(2)

    暂停当前程序2

    MonkeyDevice waitForConnection (float timeout, string deviceId)

    timeout

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

    deviceId

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

     

    返回一个MonkeyDevice 实例

    常用写法为:

    device = MonkeRunner.waitForConnection()

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

     

    MonkeyImage loadImageFromFile(string path)

    从本地加载图片,返回MonkeyImage对象。参数path为本地文件路径

    例:pic3=MonkeyRunner.loadImageFromFile("d:/pic/1.png")

    加载本地1.png图片,返回pic3.后续可使用MonkeyImage的sameAs方法与截取到的图片对比

     

    MonkeyDevice的一些常用方法:

    (详细参考:http://wiki.eoeandroid.com/MonkeyDevice)

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

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

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

    object getProperty (string key)

    查询设备相关信息,参数参考:http://developer.android.com/tools/help/MonkeyDevice.html#table1

    void installPackage (string path)

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

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

    安装D2100.apk,路径中使用 /

    void press (string name, dictionary type)

    模拟按钮操作。

    name 为按键码,参考:http://docs.eoeandroid.com/reference/android/view/KeyEvent.html

    type 键盘事件类型可用的值有DOWN, UP, DOWN_AND_UP

    void removePackage (string package)

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

    例:device.removePackage("evertone.Piano")

    卸载piano程序,程序名通过aapt查看2100.apk文件,或者在shell模式下进入data/data目录下查找

    void shell (string cmd)

    执行shell命令并返回结果

    例:device.shell("date")

    执行shell命令,查询日期

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

    启动一个应用,对于需要测试的应用需要知道应用名和入口activity名称。可以调用sdkuilds-tools目录下的aapt获取

    aapt dump baging d:2100.apk

     

    startActivity常用形式为:

    例:device.startActivity(component = "evertone.Piano/evertone.Piano.loading")

    执行后启动手机弹钢琴

    MonkeyImage takeSnapshot()

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

    例:Pic = device.takeSnapshot()

    手机屏幕截图,结果保存在Pic里。picMonkeyImage实例

    void touch (integer x, integer y, integer type)

    屏幕点击(x,y)type 键盘事件类型可用的值有DOWN, UP, DOWN_AND_UP

    例:device.touch(93,143,"DOWN_AND_UP")

    点击屏幕(93,143)位置。屏幕坐标获取可使用sdk ools路径下的Hierarchyviewer工具

    void type (string message)

    向设备发送包含字符的信息,等同于多次调用press()方法。现在发现message中只能写数字、英文,不能有空格,否则会输入失败

    例:device.type("123adc")

    输入 123abc

    void wake ()

    唤醒屏幕。暂时没发现有什么用 ==

     

    MonkeyImage的一些常用方法:

    (详细参考:http://wiki.eoeandroid.com/MonkeyImage

    MonkeyImage对象一般不用直接创建,在获取了MonkeyDevice对象后,调用takeSnapshot()方法即可

    例:pic = MonkeyDevice.takeSnapshot()

    string convertToBytes (string format)

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

    tuple getRawPixel (integer x, integer y)

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

    MonkeyImage getSubImage (tuple rect)

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

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

    boolean sameAs (MonkeyImage other, float percent)

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

    例:pic2.sameAs(pic1,0.9)

    pic2pic190%范围内相似,则返回True,否则返回False

    void writeToFile (string path, string format)

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

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

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

    通过ID调用的touch方法:

    from com.android.monkeyrunner.easy import EasyMonkeyDevice,By

    easy_device = EasyMonkeyDevice(device)

    easy_device.touch(By.id("id/digit7"),MonkeyDevice.DOWN_AND_UP)

  • 相关阅读:
    一行代码教你屏蔽你的博客广告
    一步一步教你给博客主页添加自定义炫酷效果
    让资源管理器变得像Chrome一样标签化
    weblayer组件介绍
    Tiny模板编辑器
    Tiny流程编辑器
    Tiny界面编辑器
    Tiny模板运行器
    org.tinygroup.pageflowbasiccomponent-页面流
    Tiny服务编辑器
  • 原文地址:https://www.cnblogs.com/sgtb/p/3691511.html
Copyright © 2011-2022 走看看