Monkeyrunner 錄製腳本&回放

  本文主要解釋如何使用monkeyrunner來實現腳本的錄製和回放java

一:準備條件python

     在電腦端配置 Android SDK環境   java 環境
android

下載好 SDK後添加環境變量   E:\android-sdk-windows\tools   添加到path上shell

查看adb環境搭配成功與否express

輸入cmd-回車-輸入adb shell  apache

顯示以下windows

 

OK 這樣就adb配置環境好了app

java環境同上原理 就不一一說了less

 


二: 用到的錄製、回放腳本測試

錄製腳本: monkey_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)

 

 

 

 


回放腳本: monkey_playback.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目錄下,進行下面的操做

     錄製: 一、在cmd下輸入monkeyrunner monkey_recorder.py,將打開下面的窗口

該窗口的功能:

     一、能夠自動顯示手機當前的界面

     二、自動刷新手機的最新狀態

     三、點擊手機界面便可對手機進行操做,同時會反應到真機,並且會在右側插入操做腳本

     4:、wait: 用來插入下一次操做的時間間隔,點擊後便可設置時間,單位是秒

            Press a Button:用來肯定須要點擊的按鈕,包括menu、home、search,以及對按鈕的press、down、up屬性

            Type Something:用來輸入內容到輸入框

             Fling:用來進行拖動操做,能夠向上、下、左、右,以及操做的範圍

             Export Actions:用來導出腳本

             Refresh Display:用來刷新手機界面,估計只有在斷開手機後,從新鏈接時纔會用到


 

 

 

 


        

最後一個爲錄製的文件,這裏須要使用絕對路徑


PS: 錄製後的腳本能夠進行二次更改,並且每一步操做須要有時間間隔,以保證測試的正確性

 

歡迎關注老王公衆號

 

相關文章
相關標籤/搜索