Android 自动化测试(3)<monkeyrunner> 根据ID查找对象&touch&type (python)
我在之前的两篇文章中用java来实现过 Android 自动化测试(1)如何安装和卸载一个应用(java)、Android 自动化测试(2)根据ID查找对象(java)。 但是本质上都是用monkeyrunner对应的java lib 来实现的,但是相关的文档非常少,如果真的要用monkeyrunner来做功能性的自动化测试,强烈还是推荐使用python语言
1、monkey runner
The monkeyrunner tool provides an API for writing programs that control an Android device or emulator from outside of Android code.
With monkeyrunner, you can write a Python program that installs an Android application or test package, runs it,
sends keystrokes to it, takes screenshots of its user interface, and stores screenshots on the workstation.
The monkeyrunner tool is primarily designed to test applications and devices at the functional/framework level and for running unit test suites,
but you are free to use it for other purposes.
monkeyrunner 工具提供了一个从Android源码外部写程序控制一个Android设备或者模拟器的API。
你可以用monkeyrunner写一个Python程序,来装一个Android应用和测试包,运行它,发key事件,截屏,存在本地。
monkeyrunner工具的设计起源于在功能/框架层面来测试应用和设备,和跑单元测试的测试套件
2、样例实现
2.1代码实现功能: 进入robot dream的页面mainActivity,找到id为center_image 的按钮发生点击事件,然后对弹出的对话框中再次找到id为button1的按钮,进行二次确认。
from com.android.monkeyrunner.easy import EasyMonkeyDevice, By from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice if __name__ == '__main__': import codecs codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None) print ('test start') device = MonkeyRunner.waitForConnection() easyMonkey = EasyMonkeyDevice(device) print ('start robot dream mainActivity') device.shell('am start com.robot.dream/com.robot.dream.mainActivity') MonkeyRunner.sleep(3) #easyMonkey.touch(MonkeyDevice.DOWN_AND_UP, By.id("id/center_image")); by = By.id("id/center_image"); print (by) easyMonkey.touch(by,MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(3) hierachy_view = device.getHierarchyViewer() print(hierachy_view) view_node = hierachy_view.findViewById('id/center_image') items_node = view_node.children print (len(view_node.children)) print ('touch 2') easyMonkey.touch(by,MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(3) by2 = By.id('id/button1') print (by2) print (easyMonkey.visible(by2)) vn2 = hierachy_view.findViewById('id/button1') print (dir(vn2)) print (getattr(vn2, 'name')) print (vn2.id) easyMonkey.touch(by2, MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(3)
2.2 代码实现功能: 进入robot dream的登陆页面LoginActivity,根据id为login_account找到登陆编辑框TextEditor输入用户名abc, 根据id为login_pwd找到密码编辑框TextEditor输入密码abc123, 最后找到id为login_button的登陆按钮,成功登陆
from com.android.monkeyrunner.easy import EasyMonkeyDevice, By from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice if __name__ == '__main__': import codecs codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None) print ('test start') device = MonkeyRunner.waitForConnection() easyMonkey = EasyMonkeyDevice(device) print ('start login') device.shell('am start com.robot.dream/com.robot.dream.LoginActivity') MonkeyRunner.sleep(3) by = By.id("id/login_account") print (easyMonkey.visible(by)) easyMonkey.type(by,'abc') MonkeyRunner.sleep(1) device.press('KEYCODE_ENTER') MonkeyRunner.sleep(1) device.press('KEYCODE_BACK') MonkeyRunner.sleep(1) by2 = By.id('id/login_pwd') print (easyMonkey.visible(by2)) easyMonkey.type(by2,"abc123") MonkeyRunner.sleep(1) device.press('KEYCODE_ENTER') MonkeyRunner.sleep(1) device.press('KEYCODE_BACK') MonkeyRunner.sleep(1) by3 = By.id('id/login_button') print (easyMonkey.visible(by3)) easyMonkey.touch(by3,MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(1) device.press('KEYCODE_BACK')
3、总结:
使用monkeyrunner 使用id来查找控件,使用上EasyMonkeyDevice的Touch、Type等几个基本操作,就可以完成很多基本的功能性测试了。
使用monkeyrunner 的好处,测试程序运行的环境是真实的场景下的环境。
使用monkeyrunner的局限性,无法像单元测试那样通过Assert来进行功能校验。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。