錘子便簽的Monkeyrunner 測試腳本
MonkeyRunner可能大家已經聽過無數次了,大家在網上也看過了各種關于的它的資料了,我這里就不再過多的啰嗦它的用途了,它可以對app做功能測試也可以對手機Rom做功能測試,在沒有app源碼的情況下monkeyrunner可以做到很好的功能測試。MonkeyRunner有一個錄制腳本的工具和回放的功能,大家去下載monkeyrecody.py和monkeyplayback.py這兩個腳本就可以了,這個我這里也不講了,網上去google可以搜到很好的教程,
下面是我對錘子便簽的一個MonkeyRunner測試腳本。這里會用到點擊,拖動,截圖,截圖對比的一些方法,基本上我們寫monkeyrunner測試腳本中常調用到的方法都用到了,這里對錘子便簽測試思路是這樣的:先按照正常的操作使用步驟,一步步的操作下去,每操作一步都截圖,操作完成之后,再來對截圖進行對比并打印出對比結果在log文本里。這里用到的是坐標點的定位方法。
#!/usr/bin/env monkeyrunner# encoding=utf-8 #導入python中自帶的time模塊和sys模塊,腳本中都要用到它們。import timeimport sys#MonkeyRunner自帶的三個apifrom com.android.monkeyrunner import MonkeyRunner ,MonkeyDevice ,MonkeyImage#這個函數時確認年月日時分秒now=time.strftime("%Y-%m-%d-%H-%M-%S")#指定我們要保存圖片的位置和打印log的位置path='D:\\picture\\'logpath="D:\\log\\"#python中獲取當前運行的文件的名字name=sys.argv[0].split("\\")filename=name[len(name)-1]""" |
可以嘗試輸入這兩句語句就可以明白上面的兩個python方法了。
print(name) print(filename) """#新建一個log文件log=open(logpath+filename[0:-3]+"-log"+now+".txt",'w')#連接設備,兩個參數分別是等待的時間(這里的時間都是秒為單位),設備的序列號。device=MonkeyRunner.waitForConnection(5,'b4726a2d')#安裝錘子便簽apk。參數是apk文件的位置,因為python不支持中文輸入,所以在后面用了.decode('utf-8')這個方法轉碼。device.installPackage ('D:\\apk\\錘子便簽.apk'.decode('utf-8'))#打印出操作信息到log文件里log.write("安裝apk……\n")#等待2秒MonkeyRunner.sleep(2)#啟動app,參數里是app的包名/活動名device.startActivity(component='com.smartisan.notes/.NotesActivity')MonkeyRunner.sleep(2)#打印操作信息log.write("啟動app……\n")#截圖result = device.takeSnapshot()#保存截圖 result.writeToFile(path+"主頁面".decode('utf-8')+now+'.png','png')#點擊搜索款的位置坐標。device.touch(111,155,'DOWN_AND_UP')MonkeyRunner.sleep(2)#輸入smartisan字樣device.type("smartisan")#截圖result1=device.takeSnapshot()#保存截圖result1.writeToFile(path+"搜索框截圖".decode('utf-8')+'.png','png')#移動第一個便簽的位置到最后面去,參數是:一個起始點坐標,一個終點坐標,移動的時間,移動的步驟device.drag((232,235),(216,472),3,2)MonkeyRunner.sleep(3)#截圖result2=device.takeSnapshot()#保存截圖result2.writeToFile(path+"移動便簽".decode('utf-8')+now+".png",'png')#第一個便簽向右滑動device.drag((109,360),(322,360))MonkeyRunner.sleep(3)#截圖result3=device.takeSnapshot()#保存截圖result3.writeToFile(path+"右移動便簽".decode('utf-8')+now+".png",'png')#點擊最后一個便簽的位置device.touch(182,583,'DOWN_AND_UP')MonkeyRunner.sleep(5)#點擊發送的位置device.touch(324,73,'DOWN_AND_UP')MonkeyRunner.sleep(5)#點擊發送至長微博的位置device.touch(227,789,'DOWN_AND_UP')MonkeyRunner.sleep(5)#點擊生成長微博的位置device.touch(228,791,'DOWN_AND_UP')MonkeyRunner.sleep(5)#截圖result4=device.takeSnapshot()#保存圖片result4.writeToFile(path+"發長微博截圖".decode("utf-8")+now+'.png','png')#點擊下一步的位置device.touch(426,81,'DOWN_AND_UP')MonkeyRunner.sleep(3)#截圖result5=device.takeSnapshot()#保存截圖result5.writeToFile(path+"輸入微博賬號".decode("utf-8")+now+'.png','png')#點擊輸入微博賬號和密碼的幾個位置,分別輸入賬號和密碼device.touch(196,311,'DOWN_AND_UP')MonkeyRunner.sleep(3)device.type("powermo@126.com")MonkeyRunner.sleep(3)device.touch(168,378,'DOWN_AND_UP')MonkeyRunner.sleep(3)device.type("powermo1234")MonkeyRunner.sleep(3)#點擊登錄device.touch(237,449,'DOWN_AND_UP')MonkeyRunner.sleep(3)#截圖result6=device.takeSnapshot()#保存截圖result6.writeToFile(path+"登陸微博".decode("utf-8")+now+'.png','png')#下面就開始對之前的截圖進行對比了#第一張截圖做對比,去文件中找到我們要對比的圖片resultTrue=MonkeyRunner.loadImageFromFile('D:\\picture2\\shottrue.png')log.write("主頁面對比圖片……\n")#判斷圖片相識度是否是為90%if(result.sameAs(resultTrue,0.9)): #在命令行打印出信息 print("主頁面圖片對比成功") #打印信息到log文件 log.write("主頁面圖片對比成功……\n")else: #打印信息到命令行 print("主頁面圖片對比失敗") log.write("主頁面圖片對比失敗……\n")#去文件中找到我們規定的圖片用來對比result1True=MonkeyRunner.loadImageFromFile('D:\\picture2\\shottrue1.png')#判斷圖片相識度是否是為90%if(result1.sameAs(result1True,0.9)): print("搜索框圖片對比成功") log.write("搜索框圖片對比成功……\n")else: print("搜索框圖片對比失敗") log.write("搜索框圖片對比失敗……\n")#對移動便簽圖片對比result2True=MonkeyRunner.loadImageFromFile('D:\\picture2\\shottrue2.png')##判斷圖片相識度是否是為80%if(result2.sameAs(result2True,0.8)): print("移動便簽對比成功") log.write("移動便簽對比成功……\n")else: print("移動便簽圖片對比失敗") log.write("移動便簽對比失敗……\n")#對移動便簽圖片進行對比,去文件中找我們規定的圖片result3True=MonkeyRunner.loadImageFromFile('D:\\picture2\\shottrue3.png')##判斷圖片相識度是否是為80%if(result3.sameAs(result3True,0.8)): print("右移便簽圖片對比成功") log.write("右移便簽圖片對比成功……\n")else: print("右移便簽圖片對比失敗") log.write("右移便簽圖片對比失敗……\n")#對長微博圖片對比result4True=MonkeyRunner.loadImageFromFile('D:\\picture2\\shottrue4.png')if(result4.sameAs(result4True,0.8)): print("發長微博圖片對比成功") log.write("發長微博圖片對比成功……\n")else: print("發長微博圖片對比失敗") log.write("發長微博圖片對比失敗……\n")result5True=MonkeyRunner.loadImageFromFile('D:\\picture2\\shottrue5.png')if(result5.sameAs(result5True,0.8)): print("輸入微博賬號圖片對比成功") log.write("輸入微博賬號圖片對比成功……\n")else: print("輸入微博賬號圖片對比失敗") log.write("輸入微博賬號圖片對比失敗……\n")result6True=MonkeyRunner.loadImageFromFile('D:\\picture2\\shottrue6.png')if(result6.sameAs(result6True,0.8)): print("登陸微博圖片對比成功") log.write("登陸微博圖片對比成功……\n")else: print("登陸微博圖片對比失敗") log.write("登陸微博圖片對比失敗……\n") |
posted on 2014-06-11 11:10 順其自然EVO 閱讀(285) 評論(0) 編輯 收藏 所屬分類: android