用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. gitlab-ci的注意点

    在使用gitlab搭配gitlab-runner进行ci配置时,发现两个问题,略记如下备忘: 1. 若发现ci job控制台显示无法下载该项目,但当前提交代码人和ci用户都确实具备该项目的访问权限时, ...

  2. [20171106]修改show spparameter的显示宽度.txt

    [20171106]修改show spparameter的显示宽度.txt --//很多年前做的,修改show parameter的显示宽度.--//链接: [20121023]改变show para ...

  3. axure元件库导入后重启程序元件库消失问题

    把元件库文件放在我的文档里的 \Documents\Axure\元件库 就可以了,重新启动程序不会消失

  4. 高通非adsp 架构下的sensor的bug调试

    高通 sensor 从native到HAL 高通HAL层之Sensor HAL 高通HAL层之bmp18x.cpp 问题现象: 当休眠后,再次打开preesure sensor的时候,会出现隔一段时候 ...

  5. xunit-ICollectionFixture

      https://github.com/dmetzgar/dotnetcoreinaction

  6. tkinter内嵌Matplotlib系列(二)之函数曲线绘制

    目录 目录 前言 (一)对matplotlib画布的封装: (二)思路分析: 1.需求说明: 2.框架的设置: 3.文件说明: (三)各文件的源代码 1.main.py 2.widget.py 3.f ...

  7. python scrapy爬取知乎问题和收藏夹下所有答案的内容和图片

    上文介绍了爬取知乎问题信息的整个过程,这里介绍下爬取问题下所有答案的内容和图片,大致过程相同,部分核心代码不同. 爬取一个问题的所有内容流程大致如下: 一个问题url 请求url,获取问题下的答案个数 ...

  8. python windows环境下文档备份

    #python 2.7 #Filename:backup.py import os import time source = [r'C:\Users\zeng.shufang\Desktop\mess ...

  9. call()和apply()

    call()和apply()方法类似,区别是,call()方法接受的是若干个参数的列表,而apply()方法接受的是一个包含多个参数的数组. 当一个函数在其主体中使用 this 关键字时,可以通过使用 ...

  10. Tensorflow张量

    张量常规解释 张量(tensor)理论是数学的一个分支学科,在力学中有重要应用.张量这一术语起源于力学,它最初是用来表示弹性介质中各点应力状态的,后来张量理论发展成为力学和物理学的一个有力的数学工具. ...