标签:
用python编写脚本
 
1.导入模块:
MonkeyRunner
MonkeyDevice
MonkeyImage
ps:如果给导入模块起别名,就应该使用别名,而不能使用原名,否则会出现错误。
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner import MonkeyDevice  as md
from com.android.monkeyrunner import MonkeyImage as mi 
例:
device=mr.waitForConnection()
 
2.MonkeyRunner 模块
 
2.1 com.android.monkeyrunner.MonkeyRunner.waitForConnection
注释:等待连接设备,与模拟器链接,返回monkeydevice对象,代表链接的设备。没有报错的话说明连接成功。
参数:timeout:超时时间,单位秒,浮点数。默认是无限期等待。
          deviceId:指定的设备名称。默认为当前的设备(手机有限,比如手机通过usb线链接到pc、其次为模拟器)。
实例:默认连接:device=MonkeyRunner.waitForConnection()
           参数连接:device=mr.waitForConnection(1.0,‘emulator-5554‘)
 
 
 
 
2.2  com.android.monkeyrunner.MonkeyRunner.installPackage
 
注释: #参数是相对或绝对APK路径 路径级别用“/”,不能用“\”
参数:path:被安装到设备上的apk包在电脑上的文件路径
实例:device.installPackage(‘myproject/bin/MyApplication.apk‘) 
 
 
 
 
2.3 com.android.monkeyrunner.MonkeyRunner.removePackage
 
注释:删除连接设备中的应用包,删除成功返回true
参数:pachage:将被删除的文件的包名
实例:device.removePackage(‘myproject/bin/MyApplication.apk‘)
 
 
 
 
2.4  com.android.monkeyrunner.MonkeyRunner.startActivity
 
注释:启动任意的Activity,此时可以向模拟器发送如按键、滚动、截图、存储等操作了。 执行一个adb shell命令,并返回结果,如果有的话
device.shell("...")
参数:uri - The URI for the Intent.
           action - The action for the Intent.
           data - The data URI for the Intent
           mimetype - The mime type for the Intent.
           categories - A Python iterable containing the category names for the Intent.
           extras - A dictionary of extras to add to the Intent. Types of these extras are inferred from the python types of the values.
           component - The component of the Intent.
           flags - An iterable of flags for the Intent.All arguments are optional. The default value for each argument is null.
实例:device.startActivity(component="your.www.com/your.www.com.TestActivity") 
           device.startActivity(component="your.www.com/.TestActivity") 
 
 
 
 
2.5 com.android.monkeyrunner.MonkeyRunner.sleep
 
注释:暂停目前正在运行的进程相应的时间
参数:second
实例:MonkeyRunner.sleep(秒数,浮点数)
 
 
 
 
2.6  com.android.monkeyrunner.MonkeyRunner.takeSnapshot
 
注释:获取设备的屏蔽缓冲区,产生了整个显示器的屏蔽捕获。(截图)
参数:无
实例:result=device.takeSnapshot()
           返回一个MonkeyImage对象(点阵图包装),我们可以用一下命令将图保存到文件
           result.writeToFile(‘takeSnapshot\\result1.png‘,‘png‘)
 
 
 
 
2.7  com.android.monkeyrunner.MonkeyImage.writeToFile
 
注释:将截图保存到文件 写成功返回true,否则返回false
参数:path:输出文件名和路径
          format:目标格式
实例:result.writeToFile(‘takeSnapshot\\result1.png‘,‘png‘)
 
 
 
 
2.8  com.android.monkeyrunner.MonkeyDevice.type
注释:键盘上的类型指定字符串,这相当于要求字符中按(键码,DOWN_AND_UP)字符串发送到键盘
参数:message:要键入的字符串
实例:device.type(‘字符串’)
 
2.9  com.android.monkeyrunner.MonkeyDevice.wake
注释:唤醒设备屏幕(在设备屏幕上唤醒)
参数:无
实例:device.wake()
 
2.10 com.android.monkeyrunner.MonkeyDevice.reboot
注释:重新引导到指定的引导程序指定的设备
参数:into-the bootloader to reboot into :bootloader,recovery,or None
实例:device.reboot()
 
3.Monkeyrunner类的方法介绍
 
3.1 com.android.monkeyrunner.MonkeyRunner.alert
注释:弹出一个对话框直到用户确认为止
参数:message:会话弹出的内容
          title:会话标题,默认alert
          okTitle:会话确认按钮,默认ok
实例:Alert(string message,string title,string okTile)
 
3.2 com.android.monkeyrunner.MonkeyRunner.choice
注释:显示一个对话框,选择你要添加的那个py文件
参数:message:显示在对话框中的提示信息
           choices:一个迭代的包含一系列选择的python类型
           title:对话框的标题,默认input
实例:Choice(string message,iterable choices,string title)
3.3 com.android.monkeyrunner.MonkeyRunner.help
 
注释:Help(string format)monkeyrunner 的一些帮助,这个和api差不多
 
 
 
3.4com.android.monkeyrunner.MonkeyRunner.input
 
注释:用户可以在一个对话框里面输入内容
参数:message:对话框显示信息
           initialValue:提供给用户的初始化值,默认为空字符串
           title:对话标题,默认为input
           onTitle: the text to use in the  dialog‘s confirmation button. default is ‘OK‘.
           canceTitle: the text to use in the  dialog‘s ‘cancel‘ button. default is ‘Cancel‘.
           返回:the text entered by the user ,or None if the user canceled the input.
实例:MonkeyRunner.input(string message,initialValue,title,okTitle,cancelTitle)
 
4 MonkeyDevice类的方法介绍
 
 
4.1  com.android.monkeyrunner.MonkeyDevice.ibroadcastIntent
 
注释:对设备发送一个广播信号
参数:uri - The URI for the Intent.
          action - The action for the Intent.
          data - The data URI for the Intent
           mimetype - The mime type for the Intent.
          categories - An iterable of category names for the Intent.
          extras - A dictionary of extras to add to the Intent. Types of these extras are inferred from the python types of the values.
          component - The component of the Intent.
          flags - An iterable of flags for the Intent.All arguments are optional. The default value for each argument is null.(see         android.content.Context.sendBroadcast(Intent))
实例:BroadcastIntent (string uri,string action,string data, string mimetype,iterable categories dectionnary extras,component component,iterable flages)
 
 
 
4.2  com.android.monkeyrunner.MonkeyDevice.drag
 
注释:拖动屏幕,也是划屏的一些操作
参数:start:拖曳开始坐标 (a tuple(x,y)in pixels)
           end:拖曳结束坐标点(a tuple(x,y)in pixels)
           duration:持续时间(default 1.0 seconds)
           steps:拖曳步骤(default is 10)
实例:MonkeyRunner.drag(start,end,duration,steps)
 
4.3 
 
 com.android.monkeyrunner.MonkeyDevice.getProperty(string key)
 com.android.monkeyrunner.MonkeyDevice.getSystemProperty(string key)
com.android.monkeyrunner.MonkeyDevice.installPackage(string path) 
 com.android.monkeyrunner.MonkeyDevice.instrument(string className,dictionary args)
com.android.monkeyrunner.MonkeyDevice.press(string name,dectionary type)
com.android.monkeyrunner.MonkeyDevice.reboot(string package)
 com.android.monkeyrunner.MonkeyDevice.removePackage(string package)
com.android.monkeyrunner.MonkeyDevice.shell(string cmd)执行adb shell
 com.android.monkeyrunner.MonkeyDevice.takeSnapshot()
 com.android.monkeyrunner.MonkeyDevice.touch(integer x,integer y,integer type)
 com.android.monkeyrunner.MonkeyDevice.type(string message)
 com.android.monkeyrunner.MonkeyDevice.wake()
5.MonkeyImage 类的方法介绍
 com.android.monkeyrunner.MonkeyImage.convertBytes(string format)
将图片转换为其他特殊的格式,将结果作为字符串返回,用这个方法将像素存取为特殊的格式,输出的字符串是一种更好的表现(format 目标格式 默认值为png)
           
 com.android.monkeyrunner.MonkeyImage.getRawPixel(integer x, integer y)
 
在x,y位置处获取一个单一的ARGB像素,参数x,y都是基于0坐标,表示一个像素尺寸,x向右增益,y向下增益,得到一个图标的坐标,这个方法返回一个数组
          
 
com.android.monkeyrunner.MonkeyImage.getSubImage(tuple rect)
复制一个图片的矩形,rect:A tuple(x,y,w,h),x,y,指定矩形区域的左上角,w为矩形宽,h为矩形高
         
 
 com.android.monkeyrunner.MonkeyImage.sameAs(MonkeyImage other,float percent)
 com.android.monkeyrunner.MonkeyImage.writeToFile(string path,string format)
 com.android.monkeyrunner.MonkeyImage.getHierarchyViewer()
com.android.monkeyrunner.MonkeyImage.startActivity()
monkeyrunner API接口文档内容
标签:
原文地址:http://www.cnblogs.com/WXBai/p/5939314.html