採用MonkeyRunner自動化測試(二)

來源:互聯網
上載者:User

既然是自動化測試,而且又需要寫指令碼,那是不是可以自動產生測試指令碼呢?帶著這個問題,我找到了下面的代碼 monkeyrecoder.py

#!/usr/bin/envmonkeyrunner# Copyright 2010, TheAndroid Open Source Project## Licensed under theApache License, Version 2.0 (the "License");# you may not usethis file except in compliance with the License.# You may obtain acopy of the License at##     http://www.apache.org/licenses/LICENSE-2.0## Unless required byapplicable law or agreed to in writing, software# distributed underthe License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIESOR CONDITIONS OF ANY KIND, either express or implied.# See the License forthe specific language governing permissions and# limitations underthe License. fromcom.android.monkeyrunner import MonkeyRunner as mrfromcom.android.monkeyrunner.recorder import MonkeyRecorder as recorder device =mr.waitForConnection()recorder.start(device)

首先,串連你已經開啟偵錯模式的ANDROID裝置,然後運行上面的指令碼 monkeyrunner.bat monkeyrecoder.py

執行下面的代碼後,將運行錄製指令碼的程式,如下:

 

這個軟體就可以產生指令碼。但是這個軟體產生的指令碼不是monkeyrunner可以直接啟動並執行

TOUCH|{'x':243,'y':746,'type':'downAndUp',}
TOUCH|{'x':244,'y':763,'type':'downAndUp',}
DRAG|{'start':(384,320),'end':(76,320),'duration':1.0,'steps':10,}

這種指令碼需要另外一個monkeyrunner的指令碼來解釋執行。monkeyplayback.py

#!/usr/bin/envmonkeyrunner# Copyright 2010, TheAndroid Open Source Project## Licensed under theApache License, Version 2.0 (the "License");# you may not usethis file except in compliance with the License.# You may obtain acopy of the License at##     http://www.apache.org/licenses/LICENSE-2.0## Unless required byapplicable law or agreed to in writing, software# distributed underthe License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIESOR CONDITIONS OF ANY KIND, either express or implied.# See the License forthe specific language governing permissions and# limitations underthe License. import sysfromcom.android.monkeyrunner import MonkeyRunner # The format of thefile we are parsing is very carfeully constructed.# Each linecorresponds to a single command.  Theline is split into 2# parts with a |character.  Text to the left of the pipedenotes# which command torun.  The text to the right of the pipeis a python# dictionary (it canbe evaled into existence) that specifies the# arguments for thecommand.  In most cases, this directlymaps to the# keyword argumentdictionary that could be passed to the underlying# command. # Lookup table to mapcommand 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 singlefile 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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.