MonkeyRunner 写自动化测试用例

测试环境的搭建就不多说了,大家可以问问度娘。

因为之前写自动化测试用例都是依靠一套商业软件。最近突然接触到 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(文件路径)

运行效果:


基本可以实现测试用例的执行和验证,效果也不错啊!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值