[android]android自動化測試十三之monkeyRunner自動化框架

大搜車-自娛發表於2012-06-15
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)

相關文章