用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) 

将一个apk安装到手机里面
     

 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)

删除一些apk
       

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 模块的更多相关文章

  1. [Android]官网《monkeyrunner》中文翻译

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5050768.html 翻译自 Android Develope ...

  2. monkeyrunner API接口文档内容

    用python编写脚本   1.导入模块: MonkeyRunner MonkeyDevice MonkeyImage ps:如果给导入模块起别名,就应该使用别名,而不能使用原名,否则会出现错误. f ...

  3. Android自动化测试 - MonkeyRunner(一)介绍

    MonkeyRunner介绍: MonkeyRunner是Google提供的一个基于坐标点的Android黑盒自动化测试工具. Monkeyrunner工具提供了一套API让用户/测试人员来调用,调用 ...

  4. monkeyrunner 详细介绍

    MonkeyRunner: monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器.通过monkeyrunner,您可以写出一个 ...

  5. MonkeyRunner 学习

    monkeyrunner 本文档中包含 一个简单的monkeyrunner示例程序 monkeyrunner API简介 运行monkeyrunner monkeyrunner内建帮助 使用插件扩展m ...

  6. Monkeyrunner 使用说明

    monkeyrunner为android系统新公开的一个测试工具.有助于开发人员通过脚本部署较大规模的自动化测试. Monkeyrunner       本文档中包含 一个简单的monkeyrunne ...

  7. android 自动化(1)

    学习android自动化测试要感谢一个朋友耐心的指导 环境搭建:(需要java JDK 以及android SDK) JDK:http://www.oracle.com/technetwork/jav ...

  8. Monkey&Monkey Runner使用

    adb shell monkey -p com.ajb.sp -s 500 --ignore-crashes --ignore-timeouts --monitor-native-crashes -v ...

  9. android測试工具MonkeyRunner--google官网翻译

    近期在复习之前的笔记,在回想MonkeyRunner时看了看google官网的内容,写得不错.就翻译出来分享下.事实上google官网真是一个学习的好地方. 基础知识 MonkeyRunner工具提供 ...

随机推荐

  1. ImageButton和ImageView设置点击透明区域不响应

    思路 ImageView和ImageButton都可以设置background和设置src,两者的区别自行度娘.由于两者的不同,获取它们的图片资源的方法也不同.倘若设置的是background,那么需 ...

  2. Django基础篇--用户权限管理和组管理

    Django作为一个成熟的python后台开发框架,为开发者提供了很多内置的功能,开发者只需要做一些配置就可以完成原生操作中比较复杂的代码编写.这些内置功能中其中一个比较强大的功能就是后台用户管理类. ...

  3. eclipse下载教程

    Eclipse 是一个开放源代码的.基于 Java 的可扩展开发平台. Eclipse 是 Java 的集成开发环境(IDE),当然 Eclipse 也可以作为其他开发语言的集成开发环境,如C,C++ ...

  4. Django电商项目---完成注册页面和用户登录day1

    完成基本的创建项目.用户注册.登录.注销功能 创建Django项目,创建df_user的App 创建静态文件夹static(跟manage.py保持在同一级别下) 复制静态文件(css + image ...

  5. Pygame安装教程

    1.python --version  查看安装的Python版本, pip --version  查看安装的pip版本, 升级pip命令: python -m pip install --upgra ...

  6. Sring容器技术内幕之InstantiationStrategy类介绍

    引言 org.springframework.beans.factory.support.InstantiationStrategy负责根据BeanDefinition对象创建一个Bean实例.Spr ...

  7. Spring消息中间件ActiveMQ

    1:下载ActiveMQ (apache-activemq-5.15.6) 2:下载后的结构如下 3:启动activeMQ 可以通过cmd控制台的方式,也可以通过注册services服务启动的方式 3 ...

  8. <20180929>任性的甲方

    今天参观了朋友在监督的新项目, 这个项目周期大概在6到9个月,预计本年度11月竣工. 总共大楼有五层, 施工面积在一万平米左右. 位于三楼的机房使用的设备有点高大上,发上来鉴赏一下. 双专线, 第二条 ...

  9. 通过css3,实现加载转动贝塞尔曲线动画

    参考博客:http://blog.jobbole.com/94966/ css代码: .loading { position : relative; display : inline-block; w ...

  10. 关于一台机器部署多个tomcat的小记

    一台机器部署多个tomcat在很多时候都是有可能的,比如说多个tomcat配合nginx负载更可能好的利用CPU,或者更新程序时做主备切换等. 1.直接下载或者复制一个已有的tomcat,第一个tom ...