Appium学习之---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)  启动activity
  • 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并启动

代码实现

kyb_start.py

from com.android.monkeyrunner import MonkeyRunner as mr

from com.android.monkeyrunner import MonkeyDevice as md

 

#连接设备

print("connect devices...")

device=mr.waitForConnection()

 

#安装app

print("install app...")

device.installPackage(r'C:\Users\lj\Desktop\kaoyan.apk')

 

package = 'com.tal.kaoyan'

activity = 'com.tal.kaoyan.ui.activity.SplashActivity'

runComponent = package + '/' + activity

 

#打开SplashActivity'

print("launch App...")

device.startActivity(component=runComponent)

代码执行方式

monkeyrunner scripfile

 

C:\Users\lj>monkeyrunner E:\monkeyrunner_script\kyb.py

 

 

3.MonkeyImage类

MonkeyImage类在测试过程中用来保存各种格式的测试截图,并可以进行图像对比。

常用方法

  • takeSnapshot() 进行屏幕截图
  • writeToFile() 保存图像文件到指定的文件路径

 

用法示例

from com.android.monkeyrunner import MonkeyImage as mi  

print("takeSnapshot")

screenshot=device.takeSnapshot()  

screenshot.writeToFile(r'E:\monkeyrunner_script\test.png','png')

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值