生活随笔
收集整理的這篇文章主要介紹了
Android自动化测试之MonkeyRunner录制和回放脚本
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Android自動化測試之MonkeyRunner錄制和回放腳本(十一)
分類: 自動化測試 Android自動化 2013-02-22 10:57 7346人閱讀 androidAndroidANDROIDMonkeyRecordermonkeyrunnerMonkeyRunnerMonkeyrunner
對于MonkeyRunner,有些人可能會想,既然是Android自動化測試,離不開測試腳本,那么,我們可不可以錄制測試腳本呢,答案是可以的。
我們先看看以下monkeyrecoder.py腳本:
[html] view plaincopy
#Usage:?monkeyrunner?recorder.py??#recorder.py??http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_recorder.py???com.android.monkeyrunner?import?MonkeyRunner?as?mr???com.android.monkeyrunner.recorder?import?MonkeyRecorder?as?recorder?????device?=?mr.waitForConnection()???recorder.start(device)??#END?recorder.py?? ?
首先,連接你已經(jīng)打開調(diào)試模式的ANDROID設(shè)備或模擬器,然后運(yùn)行上面的腳本,例如在cmd窗口中執(zhí)行命令: monkeyrunner monkeyrecoder.py
執(zhí)行下面的代碼后,將運(yùn)行錄制腳本的程序:
#Press ExportAction to save recorded scrip to a file
#Example of result:
#PRESS|{""name"":""MENU"",""type"":""downAndUp"",}
#TOUCH|{""x"":180,""y"":175,""type"":""downAndUp"",}
#TYPE|{""message"":"""",}
=================================================
這種腳本需要另外一個monkeyrunner的腳本來解釋執(zhí)行。monkeyplayback.py
[html] view plaincopy
#Usage:?monkeyrunner?playback.py?"myscript"????#playback.py???http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_playback.py????import?sys???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()?? ?=================================================
Usage:monkeyrunner playback.py "myscript"
總結(jié)
以上是生活随笔為你收集整理的Android自动化测试之MonkeyRunner录制和回放脚本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。