1. 程式人生 > >monkeyrunner自動化測試工具--錄製回放

monkeyrunner自動化測試工具--錄製回放

一、錄製回放前檢查

1.sdk是否已經下載並解壓,sdk解壓後的tools目錄中含有monkeyrunner.bat

2.網上下載錄製指令碼,起名為recorder.py,最好和monkeyrunner.bat在一個目錄下

#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder
device = mr.waitForConnection()
recorder.start(device)


二、錄製回放

1.手機和PC連線後,在PC命令列輸入monkeyrunner recorder.py,等待一會,就會出現monkeyrecorder介面



2.使用monkeyrecorder來滑鼠操作app介面,在右邊將會錄製下你的所有動作

3.要錄製的操作完成後,點選export actions匯出操作指令碼檔案,可以命名為recorder.mr,放在PC的某一位置,下面使用時要用它的絕對路徑.這時候可以把cmd命令列和monkeyrecorder介面關掉了。(在回放時要關掉錄製介面,否則回放執行不了)。錄製好的指令碼可以二次修改,更加接近你想要的效果。

備註個問題:我錄製了一段,回放之後總是出問題。後來發現是回放時的指令碼基本上是沒有延遲的一個接一個的連續操作,而反應在真機上就有問題了,比如點選某處彈出介面就需要時間,所有在指令碼上某些可能真機需要延遲的地方要加等待時間。等待函式:WAIT|{'seconds':5.0,} 。成功回放。

下面是我修改了以後的指令碼,可以實現自動登入然後下線的操作。

TOUCH|{'x':159,'y':565,'type':'downAndUp',}
WAIT|{'seconds':5.0,} 
TOUCH|{'x':279,'y':588,'type':'downAndUp',}
TOUCH|{'x':204,'y':761,'type':'downAndUp',}
TOUCH|{'x':204,'y':761,'type':'downAndUp',}
TOUCH|{'x':204,'y':761,'type':'downAndUp',}
TOUCH|{'x':204,'y':761,'type':'downAndUp',}
TOUCH|{'x':204,'y':761,'type':'downAndUp',}
TOUCH|{'x':204,'y':761,'type':'downAndUp',}
TOUCH|{'x':190,'y':491,'type':'downAndUp',}
TOUCH|{'x':268,'y':648,'type':'downAndUp',}
TOUCH|{'x':213,'y':671,'type':'downAndUp',}
WAIT|{'seconds':5.0,} 
TOUCH|{'x':379,'y':396,'type':'downAndUp',}
WAIT|{'seconds':5.0,} 
TOUCH|{'x':223,'y':501,'type':'downAndUp',}
TOUCH|{'x':399,'y':506,'type':'downAndUp',}
TOUCH|{'x':207,'y':508,'type':'downAndUp',}
TOUCH|{'x':301,'y':495,'type':'downAndUp',}
WAIT|{'seconds':5.0,} 
TOUCH|{'x':210,'y':390,'type':'downAndUp',}
WAIT|{'seconds':5.0,} 
TOUCH|{'x':421,'y':745,'type':'downAndUp',}
WAIT|{'seconds':5.0,} 
TOUCH|{'x':187,'y':405,'type':'downAndUp',}
WAIT|{'seconds':5.0,} 
TOUCH|{'x':154,'y':621,'type':'downAndUp',}
WAIT|{'seconds':5.0,} 
TOUCH|{'x':259,'y':186,'type':'downAndUp',}
WAIT|{'seconds':5.0,} 
TOUCH|{'x':358,'y':518,'type':'downAndUp',}

4.將下列程式碼存成一個檔案,起名為recorder.py,最好和monkeyrunner.bat在一個目錄下
#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from com.android.monkeyrunner import MonkeyRunner
# The format of the file we are parsing is very carfeully constructed.
# Each line corresponds to a single command.  The line is split into 2
# parts with a | character.  Text to the left of the pipe denotes
# which command to run.  The text to the right of the pipe is a python
# dictionary (it can be evaled into existence) that specifies the
# arguments for the command.  In most cases, this directly maps to the
# keyword argument dictionary that could be passed to the underlying
# command. 
# Lookup table to map command strings to functions that implement that
# command.
CMD_MAP = {
    'TOUCH': lambda dev, arg: dev.touch(**arg),
    'DRAG': lambda dev, arg: dev.drag(**arg),
    'PRESS': lambda dev, arg: dev.press(**arg),
    'TYPE': lambda dev, arg: dev.type(**arg),
    'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)
    }
# Process a single file for the specified device.
def process_file(fp, device):
    for line in fp:
        (cmd, rest) = line.split('|')
        try:
            # Parse the pydict
            rest = eval(rest)
        except:
            print 'unable to parse options'
            continue

        if cmd not in CMD_MAP:
            print 'unknown command: ' + cmd
            continue

        CMD_MAP[cmd](device, rest)

def main():
    file = sys.argv[1]
    fp = open(file, 'r')
    device = MonkeyRunner.waitForConnection()
    
    process_file(fp, device)
    fp.close();
    
if __name__ == '__main__':
    main()

5.重新開啟一個命令列輸入monkeyrunner playback.py recorder.mr(必須是絕對路徑)執行回放動作,直到結束。

ps1:monkeyrecorder 選單欄功能介紹

Button Description
Wait 等待時間
Press a Button 傳送,MENU,HOME,or SEARCH 按鈕.Press,Down,or Up事件
Type Something 傳送一些字串
Fling 用來操作虛擬鍵盤 
image
Export Action 將我們的指令碼匯出來
Refresh Display 重新整理當前介面
ps2:monkeyrecorder還可以獲取手機螢幕座標