python_adb 圖形界面獲取app測試數據,並展示部分測試報告v1.0版本
想到平時想用adb 我就忍不住去翻開筆記,腦子記不住,不好使,不知道大家有沒有這個想法呢,不管你有沒有,反正我有了,ttm,太煩人了,於是乎,我就開始給自己寫個需求文檔,
這就是我寫的,產品需求,合理不,管他合不合理,我看著能舒服就可以,
其實做出來的真實的效果圖差很遠,待會會給你們展示的,這裡我要說的是電量和幀率展示這個版本沒有坐進來,沒有考慮pass值不存在的情況,因為有些APP或許沒有,
既然有了需求,那麼開始寫代碼吧。我選擇python3.6.1 (備註:這裡不支持python2.7)
下面來展示下代碼,先看獲取值的代碼
import os,time,platform,time
def starttime_app(packagename,packagenameactivicy):#啟動耗時
cmd="adb shell am start -W -n %s"%packagenameactivicy
me=os.popen(cmd).read.split("
")[-7].split(":")#獲取啟動時間
cmd2="adb shell am force-stop %s"%packagename
os.system(cmd2)
return me
def makesure_system:
if platform.system =="Windows":
find="findstr"
else:
find="grep"
def liulang(packagename):
cmd="adb shell cat /data/system/packages.list | findstr %s"%packagename
cm=os.popen(cmd).read.split[1]
cmd1="adb shell cat /proc/net/xt_qtaguid/stats | findstr %s"%cm
me1_shou=os.popen(cmd1).read.split[5]#接受
me2_shou=os.popen(cmd1).read.split[7]#上傳
cmd2="adb shell cat /proc/net/xt_qtaguid/stats | findstr %s"%cm
me1_xia=os.popen(cmd1).read.split[5]#接受
me2_xia=os.popen(cmd1).read.split[7]#上傳
liulang_sum_1=(int(me1_shou)+int(me2_shou))#過程產生流量計算為執行後的流量-執行前的流量,
liulang_sum_xia=(int(me1_xia)+int(me2_xia))
liulang_sum=int(liulang_sum_xia)-int(liulang_sum_1)
me1=int(me1_xia)-int(me1_shou)
me2=int(me2_xia)-int(me2_shou)
return me1 ,me2,liulang_sum
def caijicpu(packagename):#這裡採集的cpu時候可以是執行操作採集 就是-n -d 刷新間隔
cpu="adb shell top -n 1| findstr %s"%packagename
re_cpu=os.popen(cpu).read.split[2]
return re_cpu
def getnencun(packagename):#Total 的 PSS 信息內存
cpu="adb shell dumpsys meminfo %s"%packagename
re_cpu=os.popen(cpu).read.split[118]
return re_cpu
def huoqufps(packagename):
cmd ="adb shell dumpsys gfxinfo packagename "#獲取fps
def adb_monkey(packagename,s_num,throttle,pct_touch,pct_motion,pct_trackball,pct_nav,pct_syskeys,pct_appswitch,num,logfilepath):
cmden="adb shell monkey -p %s -s %s --throttle %s --pct-touch %s --pct-motion %s --pct-trackball %s --pct-trackball %s --pct-syskeys %s --pct-appswitch %s --ignore-crashes --ignore-timeouts --monitor-native-crashes -v -v -v %s >%s"%(packagename,s_num,throttle,pct_touch,pct_motion,pct_trackball,pct_nav,pct_syskeys,pct_appswitch,num,logfilepath)
os.popen(cmden)
def huoqushebeizhuangtai:
cmd1="adb get-state"
devices_status=os.popen(cmd1).read.split[0]
return devices_status
if __name__ == "__main__":
print(getnencun("com.tencent.mobileqq"))
目前所有的數據的採集全部使用adb 來採集的,採集後呢,我這裡使用了zuilow的分割辦法來取值。
from ab_python import starttime_app,adb_monkey,huoqushebeizhuangtai,caijicpu,liulang,getnencun
import tkinter ,threading
from time import sleep
from tkinter import messagebox,ttk
from tkinter import *
from py_excel import qidongceshi,getcpu
def qidongapp:
start_tim=
cishu=
status_shebei=huoqushebeizhuangtai
if status_shebei =="device":
packname=baoming_t.get("0.0",END)
acti=activ_t.get("0.0",END)
cish=cishu_ac.get
if len(acti)<=1 or len(packname)<=1:
messagebox.showinfo("提醒","包命或者包名activity不能為空")
else:
if len(cish)<=1:
messagebox.showinfo("提醒","次數不能為空")
else:
i=0
e1["state"]= "normal"
e1.delete(1.0,tkinter.END)
sum=0
for i in range(int(cish)):
start_time=starttime_app(packagename=packname,packagenameactivicy=acti)
start_tim.append(int(start_time[1]))
cishu.append(i)
if start_time is None:
messagebox.showwarning("警告","請檢查您輸入的包或者包的啟動activity")
break
text="第%s次啟動時間:%s"%(i+1,start_time[1])
sum+=int(start_time[1])
e1["state"]= "normal"
e1.insert(tkinter.END,text)
e1.insert(tkinter.END,"
")
e1.see(END)
btn_start["state"]= "disabled"
e1.insert(tkinter.END,("平均用時:%s"%(sum/int(cish))))
qidongceshi(cishu=cishu,start=start_tim)
messagebox.showinfo("提示","測試報告已經生成,請到當前目錄查看")
e1["state"]= "disabled"
btn_start["state"]= "normal"
messagebox.showinfo("通知","測試已經完成")
else:
messagebox.showerror("警告","設備連接異常")
def monkey_app:
status_shebei=huoqushebeizhuangtai
if status_shebei =="device":
try:
packname=baoming_t1.get("0.0",END).split[0]
zhongzi=zhongzi_t.get("0.0",END).split[0]
time=time_t.get.split[0]
touch=touch_t.get("0.0",END).split[0]
huadong=huadong_t.get("0.0",END).split[0]
guiji=guiji_t.get("0.0",END).split[0]
xitong=xitong_t.get("0.0",END).split[0]
acti=acti_t.get("0.0",END).split[0]
event=event_t.get("0.0",END).split[0]
log=log_t.get("0.0",END).split[0]
danghang=danghang_t.get("0.0",END).split[0]
if len(packname)<=5:
messagebox.showwarning("提醒","請正確填寫包名")
if int(touch)+int(huadong)+int(guiji)+int(danghang)+int(xitong)+int(acti) >100:
messagebox.showerror("提醒","您輸入的所有的事件的比例和不能超過100%")
# btn_monkey["state"]= "disabled"
adb_monkey(packagename=packname,s_num=zhongzi,throttle=time,pct_touch=touch,pct_motion=huadong,pct_trackball=guiji,pct_nav=danghang,pct_syskeys=xitong,pct_appswitch=acti,num=event,logfilepath=log)
# messagebox.showinfo("通知",("測試完畢,日誌存放:%s"%log))
# btn_monkey["state"]= "normal"
except :
messagebox.showwarning("警告","必須填寫monkey相關數據")
else:
messagebox.showwarning("警告","設備連接異常 請重新連接設備!")
def cpu_app:
status_shebei=huoqushebeizhuangtai
if status_shebei =="device":
xingneng_bao=xingneng_baoming.get("0.0",END).split[0]
xing=xing_t.get
if len(xingneng_bao)<=5:
messagebox.showwarning("警告","請檢查您的包名")
cishu_list=
cpu_list=
rescv_list=
send_list=
total_list=
pass_list=
i=0
for i in range(int(xing)):
nen_cun=getnencun(xingneng_bao)
rescv,send,liulang_sum=liulang(xingneng_bao)
cpu_caiji=caijicpu(xingneng_bao)
neicun_t["state"]= "normal"
pass_list.append(int(nen_cun))
neicun_t.insert(tkinter.END,("Pass值:%s"%nen_cun))
neicun_t.insert(tkinter.END,"
")
neicun_t.see(END)
neicun_t["state"]= "disabled"
cpu_t["state"]= "normal"
cpu_list.append(int(cpu_caiji.split("%")[0]))
cpu_t.insert(tkinter.END,("CPU佔有率:%s"%cpu_caiji))
cpu_t.insert(tkinter.END,"
")
cpu_t.see(END)
cpu_t["state"]= "disabled"
liulang_t["state"]= "normal"
total_list.append(int(liulang_sum))
rescv_list.append(int(rescv))
send_list.append(int(send))
liulang_t.insert(tkinter.END,("總流量:%sk,上傳流量:%sk,下載流量:%sk"%(liulang_sum,rescv,send)))
liulang_t.insert(tkinter.END,"
")
liulang_t.see(END)
liulang_t["state"]= "disabled"
xingneng_btn["state"]= "disabled"
i+=1
cishu_list.append(int(i))
getcpu(cishu=cishu_list,start_cpu=cpu_list,recv_list=rescv_list,send_list=send_list,total_list=total_list,Pass_list=pass_list)
xingneng_btn["state"]= "normal"
messagebox.showinfo("提醒","測試完畢,測試報告已經生成!")
else:
messagebox.showwarning("警告","設備連接異常 請重新連接設備!")
def teread:
for i in range(1):
t=threading.Thread(target=cpu_app,args=)
t.start
def teread_start:
for i in range(1):
t=threading.Thread(target=qidongapp,args=)
t.start
if __name__ == "__main__":
try:
status_shebei=huoqushebeizhuangtai
if status_shebei =="device":
root=tkinter.Tk
root.title("雷子的安卓adb小工具")
# root.geometry("1000x900")
# root.resizable(width=False, height=False)
tkinter.Label(root,text="性能參數展示",fg="red",font=("黑體", 15, "bold"),).grid(row=0,column=3)
cpu_t=tkinter.Text(root,height=5,width=30)
cpu_t.grid(row=1,column=2)
liulang_t=tkinter.Text(root,height=5,width=40)
liulang_t.grid(row=1,column=4)
liulang_t.see(END)
neicun_t=tkinter.Text(root,height=5,width=30)
neicun_t.grid(row=3,column=2)
neicun_t.see(END)
suji_ev=[50,100,150,200,300]#這裡還原可以增加可以選擇的次數
xing_t=ttk.Combobox(root,values=suji_ev,width=5)
xing_t.current(0)
xing_t.grid(row=1,column=6)
tkinter.Label(root,text="cpu").grid(row=2,column=2)
tkinter.Label(root,text="參數次數").grid(row=1,column=5)
tkinter.Label(root,text="流量").grid(row=2,column=4)
tkinter.Label(root,text="內存").grid(row=6,column=2)
tkinter.Label(root,text="包名:").grid(row=7,column=1)
xingneng_baoming=tkinter.Text(root,height=1,width=30)
xingneng_baoming.grid(row=7,column=2)
xingneng_btn=tkinter.Button(root,text="開始測試",font=("黑體", 15, "bold"),command=teread)
xingneng_btn.grid(row=7,column=3)
tkinter.Label(root,text="啟動時間測試",fg="red",height=2,font=("黑體", 15, "bold")).grid(row=8,column=3)
tkinter.Label(root,text="測試包名").grid(row=9,column=1)
baoming_t=tkinter.Text(root,height=1,width=30)
baoming_t.grid(row=9,column=2)
tkinter.Label(root,text="測試包Activity").grid(row=9,column=3)
activ_t=tkinter.Text(root,height=1,width=30)
activ_t.grid(row=9,column=4)
tkinter.Label(root,text="測試次數").grid(row=9,column=5)
num=[10,20,30,50,100]
cishu_ac=ttk.Combobox(root,values=num,state="readonly",width=5)
cishu_ac.current(0)
cishu_ac.grid(row=9,column=6)
tkinter.Label(root,text="啟動時間展示").grid(row=10,column=1)
e1 = tkinter.Text(root,width=30,height=10, state="disabled")
e1.grid(row=10,column=2,padx=20,pady=30)
btn_start=tkinter.Button(root,text="測試",font=("黑體", 15, "bold"),command=teread_start)
btn_start.grid(row=10,column=3)
tkinter.Label(root,text="Monkey 測試",fg="red",font=("黑體", 15, "bold")).grid(row=11,column=3)
tkinter.Label(root,text="測試包名:").grid(row=12,column=1)
baoming_t1=tkinter.Text(root,height=1,width=30)
baoming_t1.insert("0.0",0)
baoming_t1.grid(row=12,column=2)
tkinter.Label(root,text="隨機種子數:").grid(row=12,column=3)
zhongzi_t=tkinter.Text(root,height=1,width=30)
zhongzi_t.grid(row=12,column=4)
zhongzi_t.insert("0.0",0)
tkinter.Label(root,text="時間間隔:").grid(row=12,column=5)
suji_event=[500,1000,1500,2000,3000]
time_t=ttk.Combobox(root,values=suji_event,width=5)
time_t.current(0)
time_t.grid(row=12,column=6)
tkinter.Label(root,text="導航事件百分比:").grid(row=13,column=1)
danghang_t=tkinter.Text(root,height=1,width=30)
danghang_t.insert("0.0",0)
danghang_t.grid(row=13,column=2)
tkinter.Label(root,text="觸摸事件百分比:").grid(row=13,column=3)
touch_t=tkinter.Text(root,height=1,width=30)
touch_t.grid(row=13,column=4)
touch_t.insert("0.0",0)
tkinter.Label(root,text="滑動事件百分比:").grid(row=14,column=1)
huadong_t=tkinter.Text(root,height=1,width=30)
huadong_t.grid(row=14,column=2)
huadong_t.insert("0.0",0)
tkinter.Label(root,text="軌跡球事件百分比:").grid(row=14,column=3)
guiji_t=tkinter.Text(root,height=1,width=30)
guiji_t.grid(row=14,column=4)
guiji_t.insert("0.0",0)
tkinter.Label(root,text="系統按鍵百分比:").grid(row=15,column=1)
xitong_t=tkinter.Text(root,height=1,width=30)
xitong_t.grid(row=15,column=2)
xitong_t.insert("0.0",0)
tkinter.Label(root,text="activity之間的切換百分比:").grid(row=15,column=3)
acti_t=tkinter.Text(root,height=1,width=30)
acti_t.grid(row=15,column=4)
acti_t.insert("0.0",0)
tkinter.Label(root,text="事件量:").grid(row=16,column=1)
event_t=tkinter.Text(root,height=1,width=30)
event_t.insert("0.0",0)
event_t.grid(row=16,column=2)
tkinter.Label(root,text="日誌存放路徑:").grid(row=16,column=3)
log_t=tkinter.Text(root,height=1,width=30)
log_t.grid(row=16,column=4)
log_t.insert("0.0","F:\monekey.txt")
btn_monkey=tkinter.Button(root,text="啟動Monkey測試",font=("黑體", 15, "bold"),command=monkey_app)
btn_monkey.grid(row=17,column=3)
root.mainloop
else:
print(status_shebei)
print("設備未連接或者連接異常")
except Exception as e:
print(e)
print("請檢查您是否連接設備")
圖形界面呢,我使用python原生的tkinter,這裡值得是問我,你開線程,開一個開多個行嗎,沒有問題,可以去修改試試就知道,下面的關於Excel報告的Excel我就不展示了,xlsxwriter庫,可以去看看官方很多例子。
展示效果圖。
展示測試報告
代碼放在GitHub,歡迎star 傳送門
辛苦了有心人士可以給下,激勵我前進
有疑問可以加我qq:952943386或者我的qq群194704520
※Java國際化之(五)——自定義國際化formatter註解實現
※shell腳本調用C語言之字元串切分函數——strtok
※20+行代碼使用es5 Object.defineProperty 實現簡單的watch功能
※ASP.NET MVC5(四):數據註解和驗證
※一道C語言安全編碼題目
TAG:達人科技 |
※Nokia 8開啟Android 8.1 beta版測試
※微軟開啟編譯和測試Windows 10 Redstone 5
※從論文到測試:Facebook Detectron開源項目初探
※紅米 5 Plus和Iphone x防水測試
※Essential Phone 安卓8.1系統開啟測試
※OnePlus5安卓8.1Oreo現在通過 Open Beta 6進行測試
※PE/Win 10/Xbox One測試版
※Golem發布Golem Brass Beta測試版本
※Google Chrome測試版支持原生Windows10通知
※第一個iOS 11.4 Public Beta公共測試版發布
※可關閉 iPhone 降速功能的選項出現在 iOS11.3 beta 測試版
※Canonical邀請Ubuntu 18.04用戶測試反饋視頻播放性能
※Linux下使用Speedtest測試網速
※python下的測試利器pytest
※蘋果推送iOS 11.4開發者測試版beta1:加入ClassKit平台
※Windows Server 2016 與 Linux 的一些網路性能測試
※2018全球Sneakerhead文化等級測試試題-PG2篇
※極限測試:華為mate10Pro把蘋果iPhone X秒成渣
※曝微軟Surface Phone測試中:搭載驍龍845
※Imec 與 Cadence 成功流片首款 3nm 測試晶元