Android自动化大讲堂05--monkeyrunner手势、输入和点击

 

Android自动化大讲堂05monkeyrunner手势输入点击...



本文摘自《深入理解Android自动化测试》第三章第3.1~3.2.1节

第3章monkey之子monkeyrunner 使用详解



3.1     monkeyrunner 概述





正如monkey工具反思中所言,monkey虽然很强大,但它毕竟是一款为稳定性测试准备的小工具——它很难支持插件编写,也不提供截屏功能,对数据流控制的能力很微弱,更无法完成像录制、回放这样的功能(事实上,通过调用monkey完成录制回放的脚本和小工具很多,这里是针对它自身情况而言)。

既然monkey做不到,那我们就将希望寄托在它儿子身上——monkeyrunner应运而生!

下面,就让我们一起见证长江后浪推前浪,青出于蓝而胜于蓝的测试利器monkeyrunner 吧!

3.2     monkeyrunner API详解

代码清单3-1     monkeyrunner示例脚本
# This is a monkeyrunner script created byBugBen

# This script will test the bugben.apk

# See http://developer.android.com/tools/help/MonkeyRunner.html

#

# Usage: monkeyrunner TestMonkeyRunner_Demo.py

# Import monkeyrunner modules

import sys

from com.android.monkeyrunner importMonkeyRunner, MonkeyDevice, MonkeyImage

# Parameters

txt1_x = 327

txt1_y = 231

txt2_x = 327

txt2_y = 375

bold_x = 500

bold_y = 600

small_x = 327

small_y = 744

submit_x = 555

submit_y = 999

type = 'DOWN_AND_UP'

seconds = 1

txt1_msg = 'bugben'

txt2_msg = 'bugben'

# Package name and activity name

package = 'com.xuben.hellobugben'

activity = '.ChangeActivity'

component = package + '/' + activity

# Connect device

device = MonkeyRunner.waitForConnection()

# Install HelloBugben.apk

device.installPackage('./HelloBugben.apk')

print 'Installing HelloBugben.apk...'

# Launch bugben

device.startActivity(component)

print 'Launching bugben...'

# Wait 1s

MonkeyRunner.sleep(seconds)

# Input txt1

device.touch(txt1_x, txt1_y, type)

device.type(txt1_msg)

device.press('KEYCODE_ENTER','DOWN_AND_UP')

print 'Inputing txt1...'

# Input txt2

device.touch(txt2_x, txt2_y, type)

device.type(txt2_msg)

device.press('KEYCODE_ENTER','DOWN_AND_UP')

print 'Inputing txt2...'

# Select bold

device.touch(bold_x, bold_y, type)

print 'Selecting bold...'

# Select small

device.touch(small_x, small_y, type)

print 'Selecting small...'

# Wait 1s

MonkeyRunner.sleep(seconds)

# Submit

device.touch(submit_x, submit_y, type)

print 'Submitting...'

# Wait 1s

MonkeyRunner.sleep(seconds)

# Get the snapshot

picture = device.takeSnapshot()

picture.writeToFile('./bugben_pic.png','png')

print 'Complete! See bugben_pic.png in currentfolder'

# Back to home

device.press('KEYCODE_HOME','DOWN_AND_UP')

print 'Back to HOME.'

注意:

为避免乱码,本章脚本注释一律用英文,首行注释为作者信息,第二行注释为脚本用途,第三行为相关API地址。

第五行Usage是其Linux上的用法,Windows上相应改为“monkeyrunner.bat    XXX.py”,其中XXX.py为脚本名,如当前脚本在Windows命令行下应改为:

“monkeyrunner.bat    TestMonkeyRunner_Demo.py”。

为节省本书空间,后续将只保留Usage这行注释,大家在正式写脚本时建议仍以此为准。

将以上脚本存为TestMonkeyRunner_Demo.py,运行,运行完成后打开同级目录中保存的结果截图:bugben_pic.png,如图3-1所示。



图3-1 运行结果截图:bugben_pic.png

命令行实时运行情况如图3-2所示。

图3-2 命令行运行情况
注意:

如果在Windows环境下运行,需要:

  • 配置Python环境。
  • 通过SDK(如“~android-sdk-windowsXXXools”)下的MonkeyRunner.bat运行:monkeyrunnerTestMonkeyRunner_Demo.py
  • 本脚本需提前将HelloBugben.apk拷贝到与脚本同级目录中。




提示:

MonkeyRunner、MonkeyDevice和MonkeyImage的API官网地址分别是:

  • MonkeyRunner:    http://developer.android.com/tools/help/MonkeyRunner.html。
  • MonkeyDevice:     http://developer.android.com/tools/help/MonkeyDevice.html。
  • MonkeyImage:      http://developer.android.com/tools/help/MonkeyImage.html。
 

monkeyrunner的API主要分为三类,如图3-3所示。



图3-3 monkeyrunner的API分类
先来看看MonkeyRunner API,这个类提供了用于连接monkeyrunner和设备或仿真器的方法,也为创建monkeyrunner程序的用户界面和显示帮助提供了方法,如图3-4所示。
图3-4 MonkeyRunner API
从MonkeyRunner API中的waitForConnection()方法引出了MonkeyDevice,接下来咱们看看MonkeyDevice,这个类为安装、卸载应用、发送广播、启动Activity、获取设备属性、运行测试包、基本操作、发送按键和触摸事件等提供了方法,可以看出,这部分API是咱们关注的焦点所在,如图3-5所示。
图3-5 MonkeyDevice API
从MonkeyDevice API中的takeSnapshot()方法又引出了MonkeyImage,这个类为截图、图像保存、图像对比、将位图转换成各种格式、图像截取、获取坐标点像素值等提供了方法,如图3-6所示。
图3-6 MonkeyImage API


3.2.1 monkeyrunner基本API:手势、输入和点击



下面咱们对比一下monkey和monkeyrunner在手势、输入和点击操作上的异同,如图3-7所示。
图3-7 monkey父子在手势、输入和点击操作上的异同


1.        拖拽

void drag(tuple start, tuple end, floatduration, integer steps)

提示:

在设备屏幕上模拟拖拽手势(如touch, hold, move等)。

对于drag,我们只需要了解其起止点的位置即可,持续时间和步数都可以使用默认。

参数详细说明如下:

  • tuplestart,拖拽起始位置,为tuple类型的(x, y)坐标点;
  • tupleend,拖拽终点位置,为tuple类型的(x,y)坐标点;
  • floatduration,拖拽手势持续时间,默认为1.0秒;
  • integersteps,插值点的步数,默认值为10。


脚本样例如代码清单3-2所示。
代码清单3-2     drag_monkeyrunner_bugben.py
# Usage: monkeyrunner drag_monkeyrunner_bugben.py

# Import the monkeyrunner modules

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice

# Prepare start and end with 12 steps in 1.5s

start = (400, 200)

end = (60, 200)

duration = 1.5

steps = 12

# Connect to the current device

device = MonkeyRunner.waitForConnection()

# Swipe from start to end

device.drag(start, end, duration, steps)

提示:

以上为巴哥奔当前手机下应用菜单从第一页翻到第二页的坐标拖拽(这里start为巴哥奔当前手机最右侧应用坐标,end为当前手机最左侧应用坐标),具体坐标请大家自行尝试。

2.        输入

void type(string message)

提示:

输入字符串到设备。

 对于type,我们只需直接输入字符串即可实现输入操作。

参数详细说明如下:

string message,输入字符串。

脚本样例如代码清单3-3所示。
代码清单3-3     type_monkeyrunner_bugben.py
# Usage: monkeyrunner type_monkeyrunner_bugben.py

# Import the monkeyrunner modules

from com.android.monkeyrunner importMonkeyRunner, MonkeyDevice

# Parameters

txt1_x = 327

txt1_y = 231

type = 'DOWN_AND_UP'

seconds = 1

txt1_msg = 'xiaojianjie'

# Package name and activity name

package = 'com.xuben.hellobugben'

activity = '.ChangeActivity'

component = package + '/' + activity

# Connect device

device = MonkeyRunner.waitForConnection()

# Install HelloBugben.apk

device.installPackage('./HelloBugben.apk')

print 'Installing HelloBugben.apk...'

# Launch bugben

device.startActivity(component)

print 'Launching bugben...'

# Wait 1s

MonkeyRunner.sleep(seconds)

# Input txt1

device.touch(txt1_x, txt1_y, type)

device.type(txt1_msg)

device.press('KEYCODE_ENTER','DOWN_AND_UP')

print 'Inputing txt1...'

进入bugben应用并在文本框1中输入“xiaojianjie”,如图3-8所示。


图3-8 输入“xiaojianjie”






虽然理论上不发送回车键也可以实现输入,但在实际项目中就可能出错,如图3-9所示。
图3-9 输入出错


当我们删掉发送回车键这句后,脚本执行的结果是“xiaojianjie”还在搜狗输入法中,并没有真正输入到文本框1中。



3.        点击

void touch(integer x, integer y, integer type)

提示:

发送touch    event事件对设备模拟点击。

 

对于touch,我们只需要了解其坐标值和按键类型(一般为DOWN_AND_UP)即可。

参数详细说明如下:

  • integer x,x坐标值;
  • integer y,y坐标值;
  • integer type,key event 类型(如:DOWN, UP, DOWN_AND_UP)。
注意:

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

脚本样例如代码清单3-4所示。
代码清单3-4     touch_monkeyrunner_bugben.py
# Usage: monkeyrunner touch_monkeyrunner_bugben.py

# Import the monkeyrunner modules

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice

# Prepare x, y, type

x = 45

y = 185

type = 'DOWN_AND_UP'

# Connect to the current device

device = MonkeyRunner.waitForConnection()

# touch the first application

device.touch(x, y, type)

找到桌面左上方第一个应用bugben如图3-10所示,自动打开该应用如图3-11所示。



图3-10 bugben应用图标



图3-11 打开bugben应用

monkeyrunner基本API总结如图3-12所示。



图3-12 monkeyrunner基本API总结
 

更多内容,请参考《深入理解Android自动化测试》一书,点击“阅读原文”将自动跳转,谢谢!


    关注 巴哥奔


微信扫一扫关注公众号

0 个评论

要回复文章请先登录注册