之前摸索了好久。學習Python語言。安裝工具。拉拉溜溜也慢慢地一點點進步。每天就瘋狂的上網找資料。雖然大牛們寫的很詳細。但是自己就是笨的不知怎么做。最后找了一篇文章,真的就是萬事俱備只欠東風的感覺,因為我就是不會操作。最后厚着臉皮給了開發鏈接讓他看了教我。最后人家幾分鍾就搞定,當真是有一種人與人直接的差別怎么就這么大,最后不管怎么說我學會了這個技能,為了不忘記,我就把記錄一下,也方便和我一樣的菜鳥少走彎路。
1、第一步, 首先環境建好:需要哪些環境或者工具。
1、首先得有一個android的sdk
2、 有了運行的環境還得有一個編寫Python的工具,notepad和pycharm都可以
首先在notepad或者在pycharm把代碼保存文件夾命名monkey_record.py 然后把文件放在sdk/tools里面。
#!/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)
存放路徑:
3、Python文件放到tools里面以后回到tools界面
然后在按shift加鼠標右鍵打開命令窗口
4、命令窗口打開以后我們就可以運行腳本啦
首先手機連接電腦-打開usb調試模式-手機可以傳輸文件。然后在命令框內輸入命令monkeyrunner monkey_record.py運行便會會彈出一個MonkeyRecord窗口界面。
該窗口的功能:
1、可以自動顯示手機當前的界面
2、自動刷新手機的最新狀態
3、點擊手機界面即可對手機進行操作,同時會反應到真機,而且會在右側插入操作腳本
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里面。
5、腳本錄制好以后我們要回放時怎么辦?
1、在次打開notepad把腳本復制並保存。命名為monkey_playback.py 把文件保存到tools里面。操作步驟和上面一樣。
#!/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()
2、然后我們再次打開命令窗口,運行命令 monkeyrunner monkey_playback.py lianxi (lianxi就是我們剛才導出的腳本的名稱)
上面可以看到我跑成功兩次。
備注:以上路徑都是絕對路徑,錄制后的腳本可以進行二次更改,而且每一步操作需要有時間間隔,這樣才能保證測試的正確性
總結:現在就是錄制和回放我們已經都完成了。下次給大家講一下怎么跑自己寫的腳本。。