MonkeyRunner通过ID找控键

上一篇 / 下一篇  2015-11-11 15:17:10

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的按钮,进行二次确认。

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. from com.android.monkeyrunner.easy import EasyMonkeyDevice, By  
  2. from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice  
  3.   
  4. if __name__ == '__main__':  
  5.   
  6.     import codecs  
  7.     codecs.register(lambda name: codecs.lookup('utf-8'if name == 'cp65001' else None)  
  8.   
  9.     print ('test start')  
  10.     device = MonkeyRunner.waitForConnection()  
  11.   
  12.     easyMonkey = EasyMonkeyDevice(device)  
  13.     print ('start robot dream mainActivity')  
  14.   
  15.     device.shell('am start com.robot.dream/com.robot.dream.mainActivity')  
  16.   
  17.     MonkeyRunner.sleep(3)  
  18.     #easyMonkey.touch(MonkeyDevice.DOWN_AND_UP, By.id("id/center_image"));  
  19.   
  20.     by = By.id("id/center_image");  
  21.     print (by)  
  22.     easyMonkey.touch(by,MonkeyDevice.DOWN_AND_UP)  
  23.   
  24.     MonkeyRunner.sleep(3)  
  25.   
  26.     hierachy_view = device.getHierarchyViewer()  
  27.     print(hierachy_view)  
  28.   
  29.     view_node = hierachy_view.findViewById('id/center_image')  
  30.     items_node = view_node.children  
  31.     print (len(view_node.children))  
  32.   
  33.     print ('touch 2')  
  34.     easyMonkey.touch(by,MonkeyDevice.DOWN_AND_UP)  
  35.   
  36.     MonkeyRunner.sleep(3)  
  37.   
  38.     by2 = By.id('id/button1')  
  39.     print (by2)  
  40.     print (easyMonkey.visible(by2))  
  41.   
  42.     vn2 = hierachy_view.findViewById('id/button1')  
  43.     print (dir(vn2))  
  44.     print (getattr(vn2, 'name'))   
  45.     print (vn2.id)  
  46.   
  47.     easyMonkey.touch(by2, MonkeyDevice.DOWN_AND_UP)  
  48.   
  49.     MonkeyRunner.sleep(3)  


2.2 代码实现功能: 进入robot dream的登陆页面LoginActivity,根据id为login_account找到登陆编辑框TextEditor输入用户名abc, 根据id为login_pwd找到密码编辑框TextEditor输入密码abc123, 最后找到id为login_button的登陆按钮,成功登陆

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. from com.android.monkeyrunner.easy import EasyMonkeyDevice, By  
  2. from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice  
  3.   
  4. if __name__ == '__main__':  
  5.     import codecs  
  6.     codecs.register(lambda name: codecs.lookup('utf-8'if name == 'cp65001' else None)  
  7.   
  8.     print ('test start')  
  9.     device = MonkeyRunner.waitForConnection()  
  10.     easyMonkey = EasyMonkeyDevice(device)  
  11.   
  12.     print ('start login')  
  13.     device.shell('am start com.robot.dream/com.robot.dream.LoginActivity')  
  14.   
  15.     MonkeyRunner.sleep(3)  
  16.   
  17.     by = By.id("id/login_account")  
  18.     print (easyMonkey.visible(by))  
  19.     easyMonkey.type(by,'abc')  
  20.     MonkeyRunner.sleep(1)  
  21.     device.press('KEYCODE_ENTER')  
  22.     MonkeyRunner.sleep(1)  
  23.     device.press('KEYCODE_BACK')  
  24.     MonkeyRunner.sleep(1)  
  25.   
  26.     by2 = By.id('id/login_pwd')  
  27.     print (easyMonkey.visible(by2))  
  28.     easyMonkey.type(by2,"abc123")  
  29.     MonkeyRunner.sleep(1)  
  30.     device.press('KEYCODE_ENTER')  
  31.     MonkeyRunner.sleep(1)  
  32.     device.press('KEYCODE_BACK')  
  33.     MonkeyRunner.sleep(1)  
  34.   
  35.     by3 = By.id('id/login_button')  
  36.     print (easyMonkey.visible(by3))  
  37.     easyMonkey.touch(by3,MonkeyDevice.DOWN_AND_UP)  
  38.     MonkeyRunner.sleep(1)  
  39.   
  40.     device.press('KEYCODE_BACK')  


3、总结:

使用monkeyrunner 使用id来查找控件,使用上EasyMonkeyDevice的Touch、Type等几个基本操作,就可以完成很多基本的功能性测试了。

使用monkeyrunner 的好处,测试程序运行的环境是真实的场景下的环境。

使用monkeyrunner的局限性,无法像单元测试那样通过Assert来进行功能校验。


TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-21  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 30809
  • 日志数: 12
  • 建立时间: 2015-10-29
  • 更新时间: 2017-04-20

RSS订阅

Open Toolbar