测试环境的搭建就不多说了,大家可以问问度娘。
因为之前写自动化测试用例都是依靠一套商业软件。最近突然接触到 MonkeyRunner 感觉好神奇,可以实现很多标准的测试用例。
MonkeyRunner 提供三个工具类 :MonkeyDevice、MonkeyImage、MonkeyRunner 和多个非常有用的方法。可以实现自动化脚本中常用的操作:点击坐标、点击物理键、图片比对等等。API 文档移步:http://developer.android.com/tools/help/monkeyrunner_concepts.html
搭建好测试环境后,确保手机可以被 adb 正确识别。
随便一个文字编辑器编写脚本(如Sublime Text )。
测试用例代码(Python):
rom com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
device = MonkeyRunner.waitForConnection()
def GUIContactStart():
conditon=False
print "verify mainscreen..."
device.press('KEYCODE_HOME', MonkeyDevice.DOWN_AND_UP)
dstimg=MonkeyRunner.loadImageFromFile('E:\myproject\ContactMainScreen.png','png').getSubImage((100,761,78,78))
MonkeyRunner.sleep(1.0)
result= device.takeSnapshot().getSubImage((100,761,78,78))
MonkeyRunner.sleep(1.0)
conditon=dstimg.sameAs(result,0.8)
if conditon == True:
print "In The mainscreen..."
else:
print "Back To mainscreen..."
device.press('KEYCODE_HOME', MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1.0)
print "start concatapp..."
device.touch(145,812,MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(2.0)
print "verify ContactMainScreen..."
dstimg=MonkeyRunner.loadImageFromFile('E:\myproject\NewContactSceen.png','png').getSubImage((190,40,93,73))
result= device.takeSnapshot().getSubImage((190,40,93,73))
conditon=dstimg.sameAs(result,0.8)
if conditon == True:
print "start success..."
else:
print "start failed..."
return conditon
def GUIClearAllContacts():
i=0
conditon=False
print "verify ContactMainScreen..."
dstimg=MonkeyRunner.loadImageFromFile('E:\myproject\NewContactSceen.png','png').getSubImage((190,40,93,73))
result= device.takeSnapshot().getSubImage((190,40,93,73))
conditon=dstimg.sameAs(result,0.8)
if conditon == False:
GUIContactStart()
print "Is Have Contact ?"
dstimg=MonkeyRunner.loadImageFromFile('E:\myproject\HaveContact.png','png').getSubImage((352,320,62,65))
result= device.takeSnapshot().getSubImage((352,320,62,65))
conditon=dstimg.sameAs(result,0.8)
if conditon == False:
print "no contact..."
return True
print "start delete contact..."
while conditon:
device.touch(74,349,MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1.0)
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1.0)
device.touch(85,669,MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1.0)
device.touch(349,541,MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1.0)
print "clear one contact..."
dstimg=MonkeyRunner.loadImageFromFile('E:\myproject\HaveContact.png','png').getSubImage((352,320,62,65))
result= device.takeSnapshot().getSubImage((352,320,62,65))
conditon=dstimg.sameAs(result,0.8)
i=i+1
if i > 10 :
break
print "delete over..."
return conditon
GUIContactStart()
GUIClearAllContacts()
运行方式:
打开cmd 切换到 monkeyrunner.bat 所在目录。键入:monkeyrunner.bat test.py(文件路径)
运行效果:
基本可以实现测试用例的执行和验证,效果也不错啊!