[android]android自动化测试十三之monkeyRunner自动化框架

1.MonkeyRunner自动化遇到的问题 
一、元素坐标不好定位 
二、不利于代码复用 
三、出现问题不好调试 
四、无封装大量多余代码 

针对以上四点问题,尝试了一些自动化测试框架,虽不尽如意,但是也把成果展示如下: 


1.制作一个应用,该应用可以得到你应用上所点击的IP
$ ./andrec --help
Usage: andrec [options] apkfile

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -a ANDROID_SDK, --android-sdk=ANDROID_SDK
                        android sdk path
  -i, --interact        run in jdb interact mode
  -p PORT, --port=PORT  local debug port
  -o OUTPUT, --output=OUTPUT
                        output file
  -v, --verbose         print verbose info


实例: 
$ ./andrec TestRecord.apk 
preparing apk...
installing apk...
starting...
connecting debugger...
start recording...

click com.example.R.button1
click com.example.R.submit
click com.example.R.button2
click item 2 in view -1
show menu for item 4 in view -1
click menu item "Edit"
show menu for item 5 in view -1
click menu item "Delete"
click item 18 in view -1
back pressed
show menu
click menu item "New game"
show menu
click menu item "Help"
back pressed
^C


结果导出: 
若运行时传入-o参数,则会将结果存入指定文件:
$ ./andrec -o test TestRecord.apk 
.
.
.
^C
$ cat test 
{"type": "click", "target": "com.example.R.button1"}
{"type": "click", "target": "com.example.R.submit"}
{"type": "click", "target": "com.example.R.button2"}
{"position": 1, "type": "click", "target": -1}
{"position": 3, "type": "menu", "target": -1}
{"content": "\"Edit\"", "type": "click"}
{"position": 5, "type": "menu", "target": -1}
{"content": "\"Delete\"", "type": "click"}
{"type": "back"}
{"type": "menu"}
{"content": "\"New game\"", "type": "click"}
{"type": "menu"}
{"content": "\"Help\"", "type": "click"}
{"type": "back"}


支持的Event: 
class Recorder(object):

    def click(self, view):
        print 'click', view

    def change_text(self, view, text):
        print 'text in view', view, 'changed to', text

    def click_list_item(self, view, position):
        print 'click item', position, 'in view', view

    def show_list_item_context_menu(self, view, position):
        print 'show menu for item', position, 'in view', view

    def show_menu(self):
        print 'show menu'

    def click_menu_item(self, text):
        print 'click menu item', text

    def back(self):
        print 'back pressed'

此应用只适合于拥有ID的应用,本人所在公司的APK大多数都是没有ID的都是动态生成的,所以此法不太适用。 

2.若你的应用拥有大多数ID,那么可以进行第二步,此框架可以根据ID获取该元素在布局中的坐标位置(X,Y) 

其中example为例子,src中是对布局树的解析以及封装,此框架可以配合python的UnitTest进行适用 


3.调式的问题,若你的第2部也已经完成,那么可以导入python的logging来进行log解析,而且可以把项目导入到Eclipse中进行调式,此处唯一需要注意的是,python的解释拦截器转为monkeyrunner即可 

4.另附带1个MonkeyRunner脚本
# coding=UTF-8
import time
import string
import random
import os
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device = MonkeyRunner.waitForConnection(61)

device.wake()
ImagePath="/home/eamon/tmp_tmp/"
device.removePackage("cn.opda.a.phonoalbumshoushou")
device.installPackage("DXAndroidOptimizer_DXD_2.6.0.425.apk")
time.sleep(5)
device.startActivity(action='android.intent.action.MAIN',component='cn.opda.a.phonoalbumshoushou/cn.com.opda.android.mainui.MainActivity')
time.sleep(10)
#进入功能列表
device.drag((110,400),(10,400),0.1,5)
time.sleep(5)
device.touch(90,140,MonkeyDevice.DOWN_AND_UP)
time.sleep(2)
device.touch(160,240,MonkeyDevice.DOWN_AND_UP)
time.sleep(5)
device.takeSnapshot().writeToFile(ImagePath+"cpu.png","png")
device.press("KEYCODE_BACK",MonkeyDevice.DOWN_AND_UP)
time.sleep(5)
device.touch(160,300,MonkeyDevice.DOWN_AND_UP)
device.takeSnapshot().writeToFile(ImagePath+"memory.png","png")
device.press("KEYCODE_BACK",MonkeyDevice.DOWN_AND_UP)

#功能验证界面
device.touch(200,160,MonkeyDevice.DOWN_AND_UP)
time.sleep(5)
device.takeSnapshot().writeToFile(ImagePath+"functionCheckButton.png","png")
time.sleep(5)
device.touch(160,240,MonkeyDevice.DOWN_AND_UP)
time.sleep(5)
device.takeSnapshot().writeToFile(ImagePath+"functionBlack.png","png")
time.sleep(5)

上一篇:全共享智库!浪潮中高端存储中标中国工程科技知识中心


下一篇:云服务器ECS还原安全组规则功能介绍 安全组规则的备份与还原