博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 自己主动化測试(3)<monkeyrunner> 依据ID查找对象&touch&type (python)...
阅读量:6266 次
发布时间:2019-06-22

本文共 3804 字,大约阅读时间需要 12 分钟。

    我在之前的两篇文章中用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 的button发生点击事件,然后对弹出的对话框中再次找到id为button1的button,进行二次确认。

from com.android.monkeyrunner.easy import EasyMonkeyDevice, Byfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDeviceif __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输入usernameabc, 依据id为login_pwd找到password编辑框TextEditor输入passwordabc123, 最后找到id为login_button的登陆按钮。成功登陆

from com.android.monkeyrunner.easy import EasyMonkeyDevice, Byfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDeviceif __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来进行功能校验。

4、后面会介绍些 Android monkeyrunner & &Android Code Coverage Test

转载地址:http://cbdpa.baihongyu.com/

你可能感兴趣的文章
基于 IdentityServer3 实现 OAuth 2.0 授权服务数据持久化
查看>>
是什么时候开始学习gulp了
查看>>
【Cocos2d-x游戏开发】细数Cocos2d-x开发中那些常用的C++11知识
查看>>
otl使用存储过程或是LEFT JOIN时提示输出类型未知的问题
查看>>
集群(cluster)原理(转)
查看>>
小数格式:
查看>>
【MyBatis学习06】_parameter:解决There is no getter for property named in class java.lang.String...
查看>>
Eclipse导入别人的项目报错:Unable to load annotation processor factory 'xxxxx.jar' for project...
查看>>
与孩子一起学编程10章
查看>>
【再探backbone 03】博客园单页应用实例(提供源码)
查看>>
android 圆角编写(懒得去找,写给自己看的)
查看>>
chrome 搜索 jsonView
查看>>
chrome浏览器:chrome 69 恢复默认UI
查看>>
Irony - 一个 .NET 语言实现工具包
查看>>
Java之Static静态修饰符详解
查看>>
修改weblogic部署的应用名称
查看>>
aaronyang的百度地图API之LBS云与.NET开发 Javascript API 2.0【基本地图的操作】
查看>>
Java Nio 多线程网络下载
查看>>
C++不让程序一闪而过
查看>>
C# 中的枚举类型 enum (属于值类型)
查看>>