來自 http://www.cnblogs.com/jenny-jenny/p/6600174.htmlhtml
一、第一步, 首先環境建好:須要哪些環境或者工具。python
一、首先得有一個android的sdkandroid
二、 有了運行的環境還得有一個編寫Python的工具,notepad和pycharm均可以express
首先在notepad或者在pycharm把代碼保存文件夾命名monkey_record.py 而後把文件放在sdk/tools裏面。apache
#!/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)app
存放路徑:less
三、Python文件放到tools裏面之後回到tools界面工具
而後在按shift加鼠標右鍵打開命令窗口測試
四、命令窗口打開之後咱們就能夠運行腳本啦ui
首先手機鏈接電腦-打開usb調試模式-手機能夠傳輸文件。而後在命令框內輸入命令monkeyrunner monkey_record.py運行便會會彈出一個MonkeyRecord窗口界面。
該窗口的功能:
一、能夠自動顯示手機當前的界面
二、自動刷新手機的最新狀態
三、點擊手機界面便可對手機進行操做,同時會反應到真機,並且會在右側插入操做腳本
4:、wait: 用來插入下一次操做的時間間隔,點擊後便可設置時間,單位是秒
Press a Button:用來肯定須要點擊的按鈕,包括menu、home、search,以及對按鈕的press、down、up屬性
Type Something:用來輸入內容到輸入框
Fling:用來進行拖動操做,能夠向上、下、左、右,以及操做的範圍
Export Actions:用來導出腳本,不須要後綴名,也能夠添加後綴名.mr
Refresh Display:用來刷新手機界面,估計只有在斷開手機後,從新鏈接時纔會用到
咱們試着點擊一下圖庫就會發現進入圖庫界面,右面會提示圖庫的座標。而後pressbutton home就能夠回到主界面。
咱們錄製完腳本之後 點擊Export Actions:用來導出腳本,不須要後綴名,也能夠添加後綴名.mr保存路徑依舊是tools裏面。
五、腳本錄製好之後咱們要回放時怎麼辦?
一、在次打開notepad把腳本複製並保存。命名爲monkey_playback.py 把文件保存到tools裏面。操做步驟和上面同樣。
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()
注意縮進問題
二、而後咱們再次打開命令窗口,運行命令 monkeyrunner monkey_playback.py action.mr (action就是咱們剛纔導出的腳本的名稱)
上面能夠看到我跑成功兩次。
備註:以上路徑都是絕對路徑,錄製後的腳本能夠進行二次更改,並且每一步操做須要有時間間隔,這樣才能保證測試的正確性
總結:如今就是錄製和回放咱們已經都完成了。下次給你們講一下怎麼跑本身寫的腳本。。