生活随笔
收集整理的這篇文章主要介紹了
Android必会的自动化测试
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
monkey測(cè)試?
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 啟動(dòng)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 針對(duì)特定的一個(gè)模擬器進(jìn)行操作adb shell input tap x y 模擬屏幕touch操作adb shell input swipe x1 y1 x2 y2 模擬屏幕滑動(dòng)操作adb devices 查看所有在線的Android設(shè)備.
啟動(dòng)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執(zhí)行腳本
#!/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 #連接設(shè)備(等待時(shí)間,設(shè)備號(hào)) device = mr.waitForConnection(1.0,'dea1887f') if not device: print >> sys.stderr,"fail" sys.exit(1) #定義要啟動(dòng)的activity componentName='com.slicejobs.ailinggong/com.slicejobs.ailinggong.ui.activity.SplashActivity' device.startActivity(component=componentName) #等待4秒 mr.sleep(3.0)? #1通過坐標(biāo)找到控件 #device.touch(250, 450, 'DOWN_AND_UP') #device.type('helloworld') #device.press('KEYCODE_ENTER')? #device.press('KEYCODE_BACK')? #2.1通過id來操作,更容易找到元素,(必須是開發(fā)模式的手機(jī)才能用) # 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界面的元素(必須是開發(fā)模式的手機(jī)才能用) # from com.android.chimpchat.hierarchyviewer import HierarchyViewer # hViewer = device.getHierarchyViewer() # 對(duì)當(dāng)前UI視圖進(jìn)行解析 # content = hViewer.findViewById('id/btn_login') ?# 通過id查找對(duì)應(yīng)元素 # print content.getText(); #2.3有些時(shí)侯碰到解析不出來情況是用 #github下載https://github.com/dtmilano/AndroidViewClient然后配置一下環(huán)境,增加ANDROID_VIEW_CLIENT_HOME(其實(shí)不配置也OK) # from com.dtmilano.android.viewclient import ViewClient # vc = ViewClient(device=device, serialno="emulator-5554") # content = hViewer.findViewById('id/btn_login')?
連接真機(jī),使用monkeyrunner自動(dòng)化測(cè)試工具運(yùn)行腳本-提示腳本easy_device=EasyMonkeyDevice(device1)附近c(diǎn)ould not connect to view server
原因:
EasyMonkeyDevice模塊用來通過ID訪問控件,而EasyMonkeyDevice和這里沒用到的Hierarchy Viewer只能連接開發(fā)版手機(jī)或模擬器,我們普通的商業(yè)手機(jī)是無法連上的
老版本的Hierarchy Viewer可以),所以出現(xiàn)了以上錯(cuò)誤。
這句錯(cuò)誤意思是:不能連接到View Server,View Server需要模擬機(jī)或開發(fā)機(jī),還有可能是開發(fā)機(jī)沒有開啟這項(xiàng)服務(wù)。
可以試試下面:
a.檢驗(yàn)一臺(tái)手機(jī)是否開啟了View Server '
adb shell service call window 3
b.打開View Server
adb shell service call window 1 i32 4939
如果還不行就果斷換設(shè)備吧。
------------------命令自動(dòng)錄制,直接按照下面步驟做
這才是真正實(shí)用的功能,直接看代碼,創(chuàng)建一個(gè)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)?? 命令行下運(yùn)行:
[java] ?view plaincopy
monkeyrunner?monkey_recorder.py?? 這時(shí)會(huì)彈出這樣的界面:
按鈕以及一些功能說明:
Button Description Wait 等待時(shí)間 Press a Button 發(fā)送,MENU,HOME,or SEARCH 按鈕.Press,Down,or Up事件 Type Something 發(fā)送一些字符串 Fling 用來操作虛擬鍵盤? Export Action 將我們的腳本導(dǎo)出來 Refresh Display 刷新當(dāng)前界面
自己隨心所以創(chuàng)建一些事件腳本,想做什么就可以做什么,通過MonkeyRecorder這個(gè)工具來操作設(shè)備界面,事件編輯完后選擇Export Actions,導(dǎo)出到我們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' ,}??
然后需要制作一個(gè)運(yùn)行這一系列動(dòng)作的腳本: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()??
接下來運(yùn)行我們的保存的腳本,然后,你就看到真機(jī)(模擬器),進(jìn)行你剛才一樣的操作~
[java] ?view plaincopy
E:\android-sdk-windows\tools>monkeyrunner?monkey_playback.py?action.mr ?
總結(jié)
以上是生活随笔 為你收集整理的Android必会的自动化测试 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。