monkeyrunner1

一、monkey与monkeyrunner比较:

         monkey:较难支持插件编写;无截屏功能;对数据流控制能力弱;无法完成录制功能。


二、monkeyrunner的API分为三类

          1、monkeyrunner API:提供用于连接monkeyrunner和设备的方法;其中有一个方法waitforConnection()引出monkeyDevice。

          2、MonkeyDevice API:提供安装卸载应用、启动应用、发送按键和触摸事件等操作的方法;其中一个方法takeSnapshot()引出monkeyImage

                       (1) broadcastIntent()发送广播       

                       (2) drag()拖动

                       (3)getProperty()获取当前设备属性    

                       (4) touch()点击

                       (5)type()输入        

                       (6) wake()唤醒设备       

                       (7) press()按键

           3、monkeyImage API:提供图像保存、格式转换、图像截取和坐标点获取等方法。


三、monkeyrunner的基本API:手势、输入、点击

            1、拖拽手势操作:如touch、hold、move等

                  (1)void drag(start,end,duration,steps)

                             start:起始位置坐标;数据类型为tuple数组形式

                             end:终止位置坐标;数据类型为tuple数组形式

                             duration:持续时间;数据类型为float;默认值为:1.0秒

                             steps:步数;数据类型为integer;默认值为10

                   (2)脚本式例:

                              如:向左滑动切换界面

                             #固定格式,导入MonkeyDevice API

                             from com.android.com.monkeyrunner import MonkeyRunner,MonkeyDevice


                             #设置拖动元素的值

                             start = (400,200) #初始坐标值

                             end = (60,200)#结束坐标值

                             duration = 1.0#持续时间(拖动时间)1.0秒

                            steps = 10


                             #连接monkeyrunner

                            device = MonkeyRunner.waitForConnection()


                             #执行拖动操作以滑动屏幕

                             device.drag(start,end,duration,steps)

            2、输入操作:void type(message)

                             message:输入的字符串;数据类型为String;

            3、点击操作:void touch(x,y,type)

                             x:坐标值x;数据类型integer

                             y:坐标值y;数据类型integer

                             type:key event按键类型;数据类型integer;如:DOWN、UP、DOWN_AND_UP

四、脚本式例:

                    实现目标:安装某应用并启动,打开文本框输入账号abc和密码111,登陆成功后滑动到第二页查看信息。

                    #固定格式,导入MonkeyDevice API

                    from com.android.com.monkeyrunner import MonkeyRunner,MonkeyDevice


                    #设置具体元素值:touch()点击文本框、type()输入字符串、drag()拖动界面

                    #drag(start,end,duration,steps)、type(message)、touch(x,y,type)

                     x1 = 290

                    y1 = 330         # 账号文本框坐标

                    x2 = 290

                    y2 = 430         # 密码文本框坐标

                     x3 = 380

                     y3 = 550        # 登陆坐标

                     type = DOWN_AND_UP

                     second1 = 2

                     second2 = 1

                     second3 = 3

                     message1 = abc

                     message2 = 111

                     start = (670,860)

                     end = (95,860)

                     duration = 1.0

                      steps = 10


                     #设置启动的包名和启动应用的主界面名并添加分隔符“/”

                     package = ‘com.wuba.zhuanzhuan’

                     activity = ‘.presentation.view.activity.LaunchActivity’

                     component = package +’/’+ activity


                     #连接monkeyrunner

                     device = MonkeyRunner.waitForConnection()


                     #安装wuba.zhuanzhuan.apk

                     device.installPackage(‘./wuba.zhuanzhuan.apk’)

                     print ‘Installing wuba.zhuanzhuan.apk...’


                     #启动app

                     device.startActivity(component)

                     print ‘Launching zhuanzhuan...’


                     #等待几秒钟使主界面加载完毕

                     MonkeyRunner.sleep(second1)


                     #打开文本框并输入账号和密码

                     device.touch(x1,y1,type)

                     device.type(message1)

                     device.press(‘KEYCODE_ENTER’ , ’DOWN_AND_UP’)#按下虚拟键盘中的enter键并发送,以确保字符串能够输入

                     device.touch(x2,y2,type)

                     device.type(message2)

                     device.press(‘KEYCODE_ENTER’ , ’DOWN_AND_UP’)


                     #等待1秒钟使之加载完毕

                     MonkeyRunner.sleep(second2)


                     #点击登陆按键

                     device.touch(x3,y3,type)


                     #等待几秒钟确保登陆并加载界面完成

                     MonkeyRunner.sleep(second3)


                     #向左滑动查看界面

                     device.drag(start,end,duration,steps)


                     #等待几秒钟确保加载界面完成

                     MonkeyRunner.sleep(second3)

你可能感兴趣的:(monkeyrunner1)