移動APP穩(wěn)定性測試工具monkeyrunner錄制和回放

在上一篇文章中,我們介紹到Monkeyrunner工具是在工作站上通過API定義的特定命令和事件控制設備或模擬器,支持自己編寫插件,控制事件,隨時截圖,簡而言之,任何你在模擬器/設備中能干的事情,MonkeyRunner都能干,而且還可以記錄和回放。 (http://www.itdecent.cn/p/aa2bad6b8313
在這一章里面,我們來看看Monkeyrunner如何實現(xiàn)腳本的錄制和回放。

一:準備條件
在電腦端配置Android SDK環(huán)境(自行百度)

二: 用到的錄制、回放腳本
錄制腳本: recorder.py

#!/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)  

回放腳本:MonkeyPlay.py

#!/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() 

三: 使用方法,在CMD下進入android的sdk下的tools目錄下,進行下面的操作
錄制:

1、在cmd下輸入monkeyrunner recorder.py,將打開下面的窗口


該窗口的功能:
1、可以自動顯示手機當前的界面
2、自動刷新手機的最新狀態(tài)
3、點擊手機界面即可對手機進行操作,同時會反應到真機,而且會在右側(cè)插入操作腳本
4:、wait: 用來插入下一次操作的時間間隔,點擊后即可設置時間,單位是秒
Press a Button:用來確定需要點擊的按鈕,包括menu、home、search,以及對按鈕的press、down、up屬性
Type Something:用來輸入內(nèi)容到輸入框
Fling:用來進行拖動操作,可以向上、下、左、右,以及操作的范圍
Export Actions:用來導出腳本
Refresh Display:用來刷新手機界面,估計只有在斷開手機后,重新連接時才會用到
錄制的時候每一步幾乎都有彈出的對話框進行確認,類似于下圖:
22

錄制好腳本后用 Export Actions來導出腳本,取名 test
PS: 錄制后的腳本可以進行二次更改,而且每一步操作需要有時間間隔,以保證測試的正確性。(在此錄制工具上本身的間隔需要長一點時間,否則腳本不容易被錄制)
在Cmd里面運行 在cmd下輸入monkeyrunner MonkeyPlay.py test(語法格式:monkeyrunner 回放腳本名 保存的腳本)
33

通過上面的步驟,就可以達到MonkeyRunner中的錄制回放的操作了,動手試一下吧。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容