1 概述
目前很多手机厂商都把Android benchmark工具的性能评测结果作为衡量手机好坏的参考标准。 楼主所在的公司也不例外。Benchmark有很多种,例如:antutu、androdrench等等。安兔兔只是系统性能评测的benchmark,androdrench是ROM评测工具。如果每一个benchmark工具都手动去执行,时间将花费很多。
本文以benchmark测试为切入点,介绍使用Uiautomator进行UI测试。介绍的uiautomator是使用jsonrpc技术包装过后的uiautomator。使用jsonrpc包装的目的是让开发人员更快的操作uiautomator。学习前最好去google官网上了解一下uiautomator自动化测试框架的介绍。uiautomator的测试框架可以让你通过创建UI测试自动化代码,来测试您的用户界面(UI)有效地功能,可以针对你的应用程序运行在一个或更多的设备。我们并不知道程序内部的结构,只是单纯通过界面来点击,返回,退出等来对程序进行测试,相当于黑盒测试。
2 Uiautomator的基本操作
2.1 按键
1、亮屏
# Turn on screen
d.screen.on()
# Turn off screen
d.screen.off()
if d.screen =="on": # ofd.screen != "off"
# do something in case of screen on
pass
if d.screen =="off": # of d.screen!= "on"
# do something in case of screen off
pass
2、hard\soft按键
# press home key
d.press.home()
# press back key
d.press.back()
# the normal way to press back key
d.press("back")
# press keycode 0x07('0') with META ALT(0x02) on
d.press(0x07, 0x02)
2.2 手势交互
1、点击
d.click(x, y)
2、长按
d.long_click(x, y)
3、滑动
# swipe from (sx, sy) to (ex, ey)
d.swipe(sx, sy, ex, ey)
# swipe from (sx, sy) to (ex, ey) with 10 steps
d.swipe(sx, sy, ex, ey, steps=10)
4、拖动
# drag from (sx, sy) to (ex, ey)