MonkeyRunner 模块

简介: 用python编写脚本 1.导入模块:MonkeyRunnerMonkeyDeviceMonkeyImageps:如果给导入模块起别名,就应该使用别名,而不能使用原名,否则会出现错误。from com.

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

在设备上开始一个活动
相关文章
|
机器学习/深度学习 调度
详解 Diffusion (扩散) 模型
详解 Diffusion (扩散) 模型
2354 0
|
数据安全/隐私保护 Windows
OKCC系统搭建部署后如何使用?
【客户名称】:运营商登录的客户名称默认为dipcc 【用户名】:登录的用户名为admin 【密码】:初次登录系统
2291 0
OKCC系统搭建部署后如何使用?
|
机器学习/深度学习 自然语言处理 算法
未来语音交互新纪元:FunAudioLLM技术揭秘与深度评测
人类自古以来便致力于研究自身并尝试模仿,早在2000多年前的《列子·汤问》中,便记载了巧匠们创造出能言善舞的类人机器人的传说。
14955 116
|
5月前
|
机器学习/深度学习 人工智能 弹性计算
通过阿里云服务器部署AI agent怎么选?OpenClaw与Hermes区别及选择建议参考
本文对比了两款可通过阿里云轻量应用服务器一键部署的AI Agent平台——OpenClaw与Hermes。OpenClaw定位为"日常事务万能工具箱",全面支持飞书、钉钉、企业微信等国内IM集成,适合办公自动化与团队协作,部署极简;Hermes则是"会自我进化的数字合伙人",擅长复杂任务拆解与深度研究,随使用持续学习优化,但不支持国内IM。选型建议:日常办公选OpenClaw,复杂研究选Hermes。当前阿里云轻量服务器2核2G低至38元/年、2核4G仅9.9元/月,新用户可享限时特惠,最低成本即可拥有专属AI助手。
|
5月前
|
自然语言处理 Java API
阿里云机器翻译对接使用完全指南:从开通服务到生产级部署
本文提供了一份完整的阿里云机器翻译对接使用指南。首先需登录控制台开通机器翻译服务,新用户享有每月100万字符免费额度。阿里云机器翻译提供通用版、专业版和定制版三类引擎,分别适用于日常翻译、垂直领域精准翻译和专属模型定制场景。对接方式支持控制台快速测试、SDK编程调用(Java、Python、Go、PHP、.NET等)以及原生HTTP API调用。API采用RPC风格签名机制,调用TranslateGeneral接口即可实现文本翻译,支持text和html两种输入格式。此外还提供批量翻译、异步长文本翻译、图片翻译和文档翻译等多种能力。安全管理方面建议使用RAM子账号和最小权限原则,避免主账号Ac
|
6月前
|
人工智能 弹性计算 运维
OpenClaw 阿里云实战指南:保姆级部署+高效使用+避坑全解
OpenClaw(社区昵称“小龙虾”)是2026年备受关注的开源AI智能体执行网关,核心定位是让大模型从“对话”升级为“行动”,通过自然语言指令自动完成文件管理、浏览器操控、服务器运维、流程自动化等真实任务,成为个人与团队的24小时数字员工。其具备本地优先、完全开源、永久免费(仅需支付API调用成本)、高适配性等核心优势,完美兼容阿里云各类云服务,成为阿里云用户部署AI代理的首选方案。
550 0
‘conda‘不是内部或外部命令,也不是可运行的程序或批处理文件。
‘conda‘不是内部或外部命令,也不是可运行的程序或批处理文件。
6625 1
‘conda‘不是内部或外部命令,也不是可运行的程序或批处理文件。
|
11月前
|
人工智能 自然语言处理 供应链
破局跨境电商运营困境,构建未来核心竞争力
全球跨境电商迅猛发展,企业面临数据孤岛、供应链脆弱、合规风险等挑战。本报告剖析大型卖家在“人、货、场、管”中的核心痛点,提出以一体化数字中台为核心的解决方案,涵盖主数据管理、全链路协同、业财融合与AI赋能,助力企业实现精细化运营与智能决策,构建全球化竞争壁垒。
破局跨境电商运营困境,构建未来核心竞争力
|
语音技术 网络架构
首个支持普通话和方言混说的TTS大模型:河南话、上海话、粤语说得溜
【9月更文挑战第6天】近年来,人工智能技术的进步推动了文本到语音(TTS)系统的高质量语音生成能力。针对现有TTS系统在中文方言生成上的不足,研究人员开发了Bailing-TTS模型。此模型利用大规模数据集与连续半监督学习方法,结合特定的Transformer架构及多阶段训练流程,实现了自然流畅的方言语音合成。实验结果显示,Bailing-TTS在客观和主观测试中均能生成接近真实的人类发音,并具备零样本学习能力。尽管仍面临复杂方言质量和多样性等挑战,但Bailing-TTS为中文方言语音合成提供了新的可能,并有望在未来技术发展中发挥更大作用。
2125 2

热门文章

最新文章