生活随笔
收集整理的這篇文章主要介紹了
Android必会的自动化测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
monkey測試?
adb shell? monkey -p com.slicejobs.ailinggong -v 500
monkey running
-------------------------命令自己寫
1 一條條命令
adb install xxx.apk 安裝apk文件adb shell am start -an com.xxx.xxx/.MainActivity 啟動APPadb shell am force-stop com.xxx.xxx 停止該APPadb shell input keyevent KEYCODE_HOME 模擬Android的HOME按鍵adb -s emulator-5554 shell input text test_to_input 針對特定的一個模擬器進行操作adb shell input tap x y 模擬屏幕touch操作adb shell input swipe x1 y1 x2 y2 模擬屏幕滑動操作adb devices 查看所有在線的Android設備.
啟動activity ??
adb shell am start -n com.slicejobs.ailinggong/com.slicejobs.ailinggong.ui.activity.SplashActivity
遇到SecurityException 異常
< activity ?? ????android:name = "com.siveco.bluebee.core.activity.SplashActivity " ?? ????android:label = "@string/title_activity_get_server_address" ?? ????android:exported = "true" ? > ?? </ activity >
2 用python執行腳本
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys from com.android.monkeyrunner import MonkeyRunner as mr ? from com.android.monkeyrunner import MonkeyDevice as md ? from com.android.monkeyrunner import MonkeyImage as mi #連接設備(等待時間,設備號) device = mr.waitForConnection(1.0,'dea1887f') if not device: print >> sys.stderr,"fail" sys.exit(1) #定義要啟動的activity componentName='com.slicejobs.ailinggong/com.slicejobs.ailinggong.ui.activity.SplashActivity' device.startActivity(component=componentName) #等待4秒 mr.sleep(3.0)? #1通過坐標找到控件 #device.touch(250, 450, 'DOWN_AND_UP') #device.type('helloworld') #device.press('KEYCODE_ENTER')? #device.press('KEYCODE_BACK')? #2.1通過id來操作,更容易找到元素,(必須是開發模式的手機才能用) # from com.android.monkeyrunner.easy import EasyMonkeyDevice, By # ease_device = EasyMonkeyDevice(device) # easy_device.touch(By.id('id/btn_login'), easy_device.DOWN_AND_UP) #2.2--------提取UI界面的元素(必須是開發模式的手機才能用) # from com.android.chimpchat.hierarchyviewer import HierarchyViewer # hViewer = device.getHierarchyViewer() # 對當前UI視圖進行解析 # content = hViewer.findViewById('id/btn_login') ?# 通過id查找對應元素 # print content.getText(); #2.3有些時侯碰到解析不出來情況是用 #github下載https://github.com/dtmilano/AndroidViewClient然后配置一下環境,增加ANDROID_VIEW_CLIENT_HOME(其實不配置也OK) # from com.dtmilano.android.viewclient import ViewClient # vc = ViewClient(device=device, serialno="emulator-5554") # content = hViewer.findViewById('id/btn_login')?
連接真機,使用monkeyrunner自動化測試工具運行腳本-提示腳本easy_device=EasyMonkeyDevice(device1)附近could not connect to view server
原因:
EasyMonkeyDevice模塊用來通過ID訪問控件,而EasyMonkeyDevice和這里沒用到的Hierarchy Viewer只能連接開發版手機或模擬器,我們普通的商業手機是無法連上的
老版本的Hierarchy Viewer可以),所以出現了以上錯誤。
這句錯誤意思是:不能連接到View Server,View Server需要模擬機或開發機,還有可能是開發機沒有開啟這項服務。
可以試試下面:
a.檢驗一臺手機是否開啟了View Server '
adb shell service call window 3
b.打開View Server
adb shell service call window 1 i32 4939
如果還不行就果斷換設備吧。
------------------命令自動錄制,直接按照下面步驟做
這才是真正實用的功能,直接看代碼,創建一個recoder.py:
[java] ?view plaincopy
#!/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:?? #?? #?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)?? 命令行下運行:
[java] ?view plaincopy
monkeyrunner?monkey_recorder.py?? 這時會彈出這樣的界面:
按鈕以及一些功能說明:
Button Description Wait 等待時間 Press a Button 發送,MENU,HOME,or SEARCH 按鈕.Press,Down,or Up事件 Type Something 發送一些字符串 Fling 用來操作虛擬鍵盤? Export Action 將我們的腳本導出來 Refresh Display 刷新當前界面
自己隨心所以創建一些事件腳本,想做什么就可以做什么,通過MonkeyRecorder這個工具來操作設備界面,事件編輯完后選擇Export Actions,導出到我們tools目錄下命名為:action.mr
我們看一下工具生成的action.mr腳本,如下:
[java] ?view plaincopy
TOUCH|{ 'x' : 297 , 'y' : 533 , 'type' : 'downAndUp' ,}?? WAIT|{'seconds' : 2.0 ,}?? TOUCH|{'x' : 136 , 'y' : 278 , 'type' : 'downAndUp' ,}?? WAIT|{'seconds' : 2.0 ,}?? TOUCH|{'x' : 123 , 'y' : 356 , 'type' : 'downAndUp' ,}?? WAIT|{'seconds' : 10.0 ,}?? PRESS|{'name' : 'HOME' , 'type' : 'downAndUp' ,}?? WAIT|{'seconds' : 2.0 ,}?? TOUCH|{'x' : 235 , 'y' : 720 , 'type' : 'downAndUp' ,}?? WAIT|{'seconds' : 2.0 ,}?? TOUCH|{'x' : 303 , 'y' : 630 , 'type' : 'downAndUp' ,}?? WAIT|{'seconds' : 2.0 ,}?? TOUCH|{'x' : 16 , 'y' : 71 , 'type' : 'downAndUp' ,}?? WAIT|{'seconds' : 2.0 ,}?? TOUCH|{'x' : 244 , 'y' : 735 , 'type' : 'downAndUp' ,}??
然后需要制作一個運行這一系列動作的腳本:monkey_playback.py,保存到tools目錄下:
[java] ?view plaincopy
#!/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:?? #?? #?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()??
接下來運行我們的保存的腳本,然后,你就看到真機(模擬器),進行你剛才一樣的操作~
[java] ?view plaincopy
E:\android-sdk-windows\tools>monkeyrunner?monkey_playback.py?action.mr ?
總結
以上是生活随笔 為你收集整理的Android必会的自动化测试 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。