用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. (其他)最常用的15大Eclipse开发快捷键技巧

    转自CSDNJava我人生(陈磊兴)   原文出处 引言 做java开发的,经常会用Eclipse或者MyEclise集成开发环境,一些实用的Eclipse快捷键和使用技巧,可以在平常开发中节约出很多 ...

  2. Vue组件的基础用法(火柴)

    前面的话 组件(component)是Vue最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码,根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己的需要,使用 ...

  3. 使用 WebStorm IDE 调试 Pomelo 应用程序

    使用得心应手的IDE来开发应用程序,可以使我们的工作事半功倍.而调试则更可以让我们准确的定位BUG,发现问题.本文讲述如何使用 WebStorm 这个怪兽级JavaScript IDE来调试 Chat ...

  4. Django学习---快速搭建搜索引擎(haystack + whoosh + jieba)

    Django下的搜索引擎(haystack + whoosh + jieba) 软件安装 haystack是django的开源搜索框架,该框架支持Solr,Elasticsearch,Whoosh, ...

  5. Docker学习—Docker简介

    什么是Docker Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go 语言实现. 项目后来加入了 Linu ...

  6. office(Word、Excel、PPT等图标异常和桌面无新建解决方案)

    前言吐槽: 前不久因为安装了WPS,然后觉得不好用卸载WPS装回office就出现了一个很恶心的问题:word文档.excel文档.PPT这些办公软件图标异常,显示的是下面这样: 打开倒是可以正常用w ...

  7. C语言变量定义与数据溢出(初学者)

    1.变量定义的一般形式为:类型说明符.变量名标识符等:例:int a,b,c;(abc为整型变量) 在书写变量定义时应注意以下几点: (1)允许在一个类型说明符后,定义多个相同类型的变量.各变量之间用 ...

  8. 图的M 着色问题

    题目描述给定无向连通图G 和M 种不同的颜色,用这些颜色为图G 的各顶点着色,每个顶点着一种颜色.如果有一种着色法使G 中每条边的2 个顶点着不同的颜色,则称这个图是M 可着色的.图的M 着色问题是对 ...

  9. maven 打包生成doc和源码插件

    <!--配置生成Javadoc包--> <plugin> <groupId>org.apache.maven.plugins</groupId> < ...

  10. (3)lscpu详解 (每周一个linux命令系列)

    (3)lscpu详解 (每周一个linux命令系列) linux命令 lscpu详解 引言:今天的命令是用来看cpu信息的lscpu lscpu 我们先看man lscpu display infor ...