MonkeyRunner API(一)

MonkeyRunner API

MonkeyRunner工具主要有三个类:

  1. MonkeyRunner
  2. MonkeyDevice
  3. MonkeyImage

官方API文档 :http://www.android-doc.com/tools/help/monkeyrunner_concepts.html#

1.MonkeyRunner类:

MonkeyRunner提供连接真机和模拟器、输入、暂停、警告框等方法。

常用方法

waitForConnection(float timeout,string deviceid),

from com.android.monkeyrunner import MonkeyRunner as mr

print("connect devices...")

 

device=mr.waitForConnection()

# device=mr.waitForConnection(5,'127.0.0.1:62001')

2.MonkeyDevice类

MonkeyDevice类提供了安装和卸载程序包、开启Activity、发送按键和点击事件、运行测试包等方法。

常用方法

  • installPackage (string path)
  • removePackage (string package)
  • startActivity (string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, flags)
  • touch (integer x, integer y, integer type)

touch参数说明

integer x,x坐标值。
integer y,y坐标值。
integer type,key event类型(如DOWN、UP、DOWN_AND_UP)。

DOWN为按下事件 UP为弹起事件 DOWN_AND_UP为按下弹起事件。

  • drag (tuple start, tuple end, float duration, integer steps)

drag参数详细说明如下:

tuple start,拖拽起始位置,为tuple类型的(x,y)坐标点。
tuple end,拖拽终点位置,为tuple类型的(x,y)坐标点。
float duration,拖拽手势持续时间,默认为1.0s。
-integer steps,插值点的步数,默认值为10。

实践案例:

连接设备,安装考研帮App并启动

代码实现

代码执行方式

monkeyrunner scripfile

 

C:\Users\admin>monkeyrunner D:\download\kao.py

执行前截图

执行后截图

发布了100 篇原创文章 · 获赞 71 · 访问量 184万+

猜你喜欢

转载自blog.csdn.net/dengachao/article/details/99679418