多设备monkey测试工具_基于Tkinter GUI操作

V1.0版本

先看下效果吧:

简单的介绍下:

1.设备列表区域:用于展示当前连接的所有设备

2.monkey参数区域:用于显示即将执行的monkey参数

3.日志区域:用于显示必要日志(其实没啥卵用,就是看着高大上点)

操作流程:

1.刷新设备,获取当前连接的所有设备(必须)

2.填写monkey参数:

  a).package:手动填写需要跑的APP包名;自动获取,需要现在左侧点击选择一台设备,打开设备上需要获取包名的应用,然后点击“获取包名”即可。

  b).seed:随机值,默认取当前日期,这个纯属个人习惯

  c).Throttle:操作间隔,大家都懂。。。

  d).Count:总的操作次数,我默认的50万,感觉这个差不多吧。

 3.点击“RUN”就好了,所有设备都会跑起来

4.没做离线功能,所以还是需要连着数据线的

结果查看:

  同目录下会生成一个result文件夹,里面包括Crash日志、内存日志、monkeylog信息,直接取就好了

代码:

import glob
import json
import logging
import os
import re
import time
import tkinter as tk
import tkinter.messagebox

window = tk.Tk()

window.title("Demo APP")

width = 640
height = 730

# 设置窗口适应属性
window.resizable(width=False, height=False)
window.attributes("-alpha",0.7)
window.attributes('-toolwindow', False, 
                '-alpha', 1, 
                '-fullscreen', False, 
                '-topmost', True)
# window.overrideredirect(True)  

# 获取当前屏幕分辨率
screenwidth = window.winfo_screenwidth()  
screenheight = window.winfo_screenheight() 

# 根据分辨率设置窗口大小
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/1, (screenheight-height)/5)
window.geometry(alignstr)

# ----frame-----
frame_main1 = tk.Frame(window, bg = "#F2F2F2", height=100, width=360)
frame_main1.grid(column=1,row=1,padx=10,pady=10)

frame_main2 = tk.Frame(window, bg = "#F2F2F2", height=100, width=400)
frame_main2.grid(column=2,row=1)

frame_main3 = tk.Frame(window, bg = "#F2F2F2", height=200, width=370)
frame_main3.grid(column=1,row=2)

frame_main4 = tk.Frame(window, bg ="#F2F2F2", height=200, width=360)
frame_main4.grid(column=2,row=2)

frame_main5 = tk.Frame(window, bg = "#F2F2F2", height=300, width=720)
frame_main5.grid(column=1,row=3,columnspan=2,padx=15,pady=15)

# -----menu菜单功能,暂时不要后续添加--------
# meun_main = tk.Menu()
# # meun_main.grid()
# filemenu = tk.Menu(meun_main, tearoff=1)
# meun_main.add_cascade(label='File', menu=filemenu)
# filemenu.add_command(label='Monkey')
# filemenu.add_command(label='Screen')
# filemenu.add_command(label='Instructions')
# filemenu.add_command(label='About')
# window.config(menu = filemenu)

# -------Log缓存区域,显示自定义日志--------
lable_log=tk.Label(frame_main5,text="日志区",font = ("微软雅黑",14))
lable_log.grid(column=1,row=1)

text_log = tk.Text(frame_main5,bg="#5B5B5B",fg="#FFFFFF")
text_log.grid(column=1,row=2)


def clear_log():
    text_log.delete("1.0","end")

button_clearLog = tk.Button(frame_main5,text="Clear",font = ("微软雅黑",8),bg="#836FFF", fg= "#FFFFFF",command=clear_log)
button_clearLog.grid(column=2,row=2,sticky="n")

# text_log.insert("end","adasd21dadd")


# ------主页lable显示引导---------------
# 刷新设备说明
lable_main1 = tk.Label(frame_main1, justify="left", height=5, width=35, bg="#E5E5E5", font = ("微软雅黑",10), text="准备工作:\n1.进入开发者选项打开测试机USB调试功能\n2.点击刷新设备\n3.手机端确认USB调试(若未提示则跳过此步骤)\n4.设备连接成功")
lable_main1.grid()
# monkey参数说明
lable_main2 = tk.Label(frame_main2, justify="left", height=5, width=40, bg="#E5E5E5", font = ("微软雅黑",10), text="参数说明:\n1.Package:包名,可点击获取,也可以手动输入\n2.Seeds:随机值,默认取当天日期,也可手动输入\n3.Throttle:点击频率,默认300ms,可手动输入\n4.Count:总操作数,默认50万,可手动修改")
lable_main2.grid()

# ----listbox用于显示当前设备列表-----
lb = tk.Listbox(frame_main3,height=9, width=28, bg="#CDCDB4", fg="#551A8B")
lb.grid(column=0,row=0,padx=10,pady=10)


# -----frame4上的参数配置-----
lab1 = tk.Label(frame_main4,text="配置项",bg="#F2F2F2",font=(11))
lab2 = tk.Label(frame_main4,text="Package",bg="#F2F2F2" )
lab3 = tk.Label(frame_main4,text="Seeds",bg="#F2F2F2" )
lab4 = tk.Label(frame_main4,text="Throttle",bg="#F2F2F2" )
lab5 = tk.Label(frame_main4,text="Count",bg="#F2F2F2" )
lab6 = tk.Label(frame_main4,text="参数",bg="#F2F2F2",font=(11) )  #第二列标题哦


lab1.grid(column=1,row=1,padx=5,pady=5)
lab2.grid(column=1,row=2,padx=5,pady=5,sticky="w")
lab3.grid(column=1,row=3,padx=5,pady=5,sticky="w")
lab4.grid(column=1,row=4,padx=5,pady=5,sticky="w")
lab5.grid(column=1,row=5,padx=5,pady=5,sticky="w")
lab6.grid(column=2,row=1,padx=1,pady=1)


# -----entry monkey参数配置-------
entry1 = tk.Entry(frame_main4)
entry2 = tk.Entry(frame_main4)
entry3 = tk.Entry(frame_main4)
entry4 = tk.Entry(frame_main4)

entry1.grid(column=2,row=2,padx=5,pady=1,sticky="w")
entry2.grid(column=2,row=3,padx=5,pady=1,sticky="w")
entry3.grid(column=2,row=4,padx=5,pady=1,sticky="w")
entry4.grid(column=2,row=5,padx=5,pady=1,sticky="w")


# ------一些可能用到的全局变量------
select_var = ""
pkg_details = ""
button_state = ""
model_name = ""
# 创建一个空list存储设备
list_device = []
# monkey配置参数
p = ""
s = ""
t = ""
c = ""

def find_and_init():
    # def find_devices():
    global list_device,model_name
    list_device.clear()
    logging.basicConfig(level=logging.INFO)
    d_lists = os.popen("adb devices").readlines()
    logging.error(d_lists)
    d_lists.remove(d_lists[len(d_lists)-1])
    d_lists.remove(d_lists[0])
    lb.delete("0","end")
    logging.info(d_lists)
    # time.sleep(3)
    if len(d_lists)==0:
        lb.insert("end","未检测到设备,请连接测试机...")
        text_log.insert("end","未检测到设备,请连接测试机...\n")
        text_log.yview_moveto(1)
        text_log.update()
    elif "unauthorized" in d_lists[0]:
        lb.insert("end","请在手机上点击允许USB调试...")
        text_log.insert("end","请在手机上点击允许USB调试...\n")
        text_log.yview_moveto(1)
        text_log.update()
    else:
        
        text_log.insert("end","设备已连接,请选择设备进行后续操作!\n\n")
        text_log.yview_moveto(1)
        text_log.update()
        for i in d_lists:
            reg_serial = re.findall("(.*?)device", i)
            model_org = os.popen("adb -s "+str(reg_serial[0])+" shell getprop ro.product.model").readlines()
            logging.info(reg_serial[0].replace("\t","")+":"+model_org[0].replace("\n",""))

            # 将设备信息添加到listbox中
            per_list = reg_serial[0].replace("\t","")
            print("设备列表为:",per_list)
            os.system("adb -s "+str(per_list)+" logcat -c")
            lb.insert("end",model_org[0].replace("\n","").replace(" ","")+":"+per_list)
            model_name = model_org[0].replace("\n","").replace(" ","")
            list_device.append(per_list)


# ------读写配置文件-----------
    # def init_config():
    entry1.delete(0,"end")
    entry2.delete(0,"end")
    entry3.delete(0,"end")
    entry4.delete(0,"end")
    # time.sleep(3)
    if os.path.exists("config.json"):
        with open('config.json',encoding="utf-8") as ff:
            json_data = json.load(ff)
            entry1.insert("end",json_data["pkg"])
            entry2.insert("end", time.strftime("%Y%m%d%H%M%S", time.localtime())[2:8])
            entry3.insert("end", json_data["thr"])
            entry4.insert("end", json_data["total"])
            
    else:
        config_file = open('config.json','w',encoding='utf-8')
        # json_data = {"pkg":"请在此处填写包名...","sd":time.strftime("%Y%m%d%H%M%S", time.localtime())[2:8],"thr":"300","total":"500000"}
        json_data = {"pkg":"请在此处填写包名...", "thr":"300","total":"500000"}
        json.dump(json_data,config_file,ensure_ascii=False)
        config_file.close()

    # p=entry1.get()
    # s=entry2.get()
    # t=entry3.get()
    # c=entry4.get()

#  -----------按钮事件--------
# 刷新设备按钮
button = tk.Button(frame_main3,text="刷新设备",font = ("微软雅黑",15),bg="#8FBC8F", fg= "#FFFFFF", command=find_and_init)
button.grid(column=0,row=1)


# --------获取当前包名---------
def get_pkg():
    global select_var
    global pkg_details
    logging.info(select_var)
    # global select_var
    selected=lb.get(lb.curselection())

    print("我也不知道这里会不会报错 ",selected)

    print("原始的",selected,type(selected))
    reg_device=".*?:(.*)"
    select_device = re.findall(reg_device, selected)
    print("选择的设备是",select_device[0])
    select_var=select_device[0]
    
    try:
        cur = os.popen("adb -s "+str(select_var)+" shell dumpsys window | findstr mCurrentFocus").readline()
        result=re.findall("mCurrentFocus=Window{.*?u0 (.*?)/.*?}", cur)
        pkg_details = result[0]
        logging.info(cur)

    except IndexError:
        entry1.delete(0,"end")
        entry1.insert("end","获取失败...")
        tk.messagebox.showerror("Error了吧,哈哈哈!","请注意:\n  1.确认设备已经连接。\n  2.需要获取包名的应用处于前台状态!")
        entry1.delete(0,"end")
        entry1.insert("end","点击重试...")

    except TypeError:
        entry1.delete(0,"end")
        entry1.insert("end","未选择设备哦!")
        tk.messagebox.showerror("Error,这个不是Bug...","没有选择设备哦!")
        entry1.delete(0,"end")
        entry1.insert("end","点击重试...")


    else:
        
        entry1.delete(0,"end")
        entry1.insert("end",pkg_details)
        text_log.insert("end","获取包名成功!!!\n\n")
        text_log.yview_moveto(1)
        text_log.update()

def run_monkey():

    now_time = time.strftime("%Y%m%d%H%M%S", time.localtime())[4:12]

    if not os.path.exists("./Result/"+str(now_time)):
        text_log.insert("end","创建必要资源文件夹....\n\n")
        text_log.yview_moveto(1)
        text_log.update()
        os.makedirs("./Result/"+str(now_time))

    if not os.path.exists("./script/"+str(now_time)):
        text_log.insert("end","创建必要资源文件夹....\n\n")
        text_log.yview_moveto(1)
        text_log.update()
        os.makedirs("./script/"+str(now_time))
    global p,s,t,c
    p=entry1.get()
    s=entry2.get()
    t=entry3.get()
    c=entry4.get()

    global button_state
    button_state = "disabled"
    print(pkg_details)
    text_log.insert("end","即将开始初始化资源,请稍等...\n")
    if len(list_device)>0:

        if entry1.get() in (u"请在此处填写包名...",""):
            tk.messagebox.showerror("你又错了!!!","不出意外的话你包名肯定不对!!")

        else:

            for d in list_device:
                bat_monkey = str(d[:8])+"_monkey_"+str(pkg_details)+".bat"
                with open ("script\\"+str(now_time)+"\\"+bat_monkey, "w") as f:
                    text_log.insert("end","生成"+str(d)+"---monkey脚本...\n")
                    text_log.yview_moveto(1)
                    text_log.update()
                    f.write('@echo off\necho start  monkey test\ntitle Monkey\nadb -s '+str(d)+' shell monkey -s '+str(s)+' -p '+str(p)+' --throttle '+str(t)+' --ignore-crashes --ignore-native-crashes --ignore-security-exceptions --ignore-timeouts --monitor-native-crashes -v -v -v '+str(c)+' > ./Result/'+str(now_time)+'/'+str(d[:8])+'_monkey.log')
                bat_fc = str(d[:8])+"_fc_"+str(pkg_details)+".bat"
                with open ("script\\"+str(now_time)+"\\"+bat_fc, "w") as ff:
                    text_log.insert("end","生成"+str(d)+"---crash监控脚本...\n")
                    text_log.yview_moveto(1)
                    text_log.update()
                    ff.write('@echo off\necho Start collecting crash logs\ntitle Crash\nadb -s '+str(d)+' logcat -s AndroidRuntime > ./Result/'+str(now_time)+'/'+str(d[:8])+'_crash.log')
                bat_memory = str(d[:8])+"_memory_"+str(pkg_details)+".bat"
                with open ("script\\"+str(now_time)+"\\"+bat_memory, "w") as fff:
                    text_log.insert("end","生成"+str(d)+"---内存监控脚本...\n\n")
                    text_log.yview_moveto(1)
                    text_log.update()
                    fff.write('@echo off\necho Start collecting memory logs\ntitle Memory\n:memory\nadb -s '+str(d)+' shell dumpsys meminfo '+str(p)+' | findstr TOTAL: > ./Result/'+str(now_time)+'/'+str(d[:8])+'_memory.log\nping -n 30 127.0.0.1>nul\ngoto memory')
            
            bat_finally = glob.glob("script\\"+str(now_time)+"\\*.bat")
            print("最终的bat有",bat_finally)
            time.sleep(2)
            text_log.insert("end","一切准备就绪,即将启动所有骚操作!!!\n\n")
            text_log.yview_moveto(1)
            text_log.update()

            text_log.insert("end","\n包名:"+p)
            text_log.insert("end","\nseeds值:"+s)
            text_log.insert("end","\nthrottle值:"+t)
            text_log.insert("end","\n总测试数:"+c)
            
            for gogo in bat_finally:
                os.system("start "+gogo)
                time.sleep(1)
    else:
        # len(list_device)==0:
        tk.messagebox.showerror("你又错了!!!","请注意:\n  1.确认设备已经连接。\n  2.确认包名不为空!!")

# 获取包名按钮
button_getpkg = tk.Button(frame_main4,text="获取包名", fg= "#FFFFFF",font = ("",9),bg="#8FBC8F",command=get_pkg)
button_getpkg.grid(column=3,row=2,padx=5,pady=5,sticky="w")
notice_lable = tk.Label(frame_main4,text="获取包名前,\n先选择设备。", justify="left",fg="red")
notice_lable.grid(column=3,row=3)

button_make_res = tk.Button()

# Monkey启动按钮
if button_state == "disabled":
    button_runmonkey = tk.Button(frame_main4,text="Run!!!",state="disabled", fg= "#FFFFFF",font = ("微软雅黑",15),bg="#8FBC8F",command=run_monkey)
    button_runmonkey.grid(column=2,row=7,padx=25,pady=5,sticky="w")
else:
    button_runmonkey = tk.Button(frame_main4,text="Run!!!", fg= "#FFFFFF",font = ("微软雅黑",15),bg="#8FBC8F",command=run_monkey)
    button_runmonkey.grid(column=2,row=7,padx=25,pady=5,sticky="w")
    button_state = "disabled"

window.mainloop()

V2.0版本 

更新日志:

1.新增支持离线脱机测试,monkey开启后等待5s左右拔掉数据线即可

2.新增支持一键导出测试结果

3.暂不支持多设备同时执行了

import glob
import json
import logging
import os
import re
import time
import tkinter as tk
import tkinter.messagebox
from tkinter.filedialog import askdirectory

import _thread

window = tk.Tk()

window.title("便捷monkey测试工具")

width = 670
height = 730

# 设置窗口适应属性
window.resizable(width=False, height=False)
window.attributes("-alpha",0.7)
window.attributes('-toolwindow', False, 
                '-alpha', 1, 
                '-fullscreen', False, 
                '-topmost', True)
# window.overrideredirect(True)  

# 获取当前屏幕分辨率
screenwidth = window.winfo_screenwidth()  
screenheight = window.winfo_screenheight() 

# 根据分辨率设置窗口大小
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/1, (screenheight-height)/5)
window.geometry(alignstr)


# ----frame-----
frame_main1 = tk.Frame(window, bg = "#F2F2F2", height=100, width=360)
frame_main1.grid(column=1,row=1,padx=10,pady=10)

frame_main2 = tk.Frame(window, bg = "#F2F2F2", height=100, width=400)
frame_main2.grid(column=2,row=1)

frame_main3 = tk.Frame(window, bg = "#F2F2F2", height=200, width=370)
frame_main3.grid(column=1,row=2)

frame_main4 = tk.Frame(window, bg ="#F2F2F2", height=200, width=360)
frame_main4.grid(column=2,row=2)

frame_main5 = tk.Frame(window, bg = "#F2F2F2", height=300, width=720)
frame_main5.grid(column=1,row=3,columnspan=2,padx=15,pady=15)

# 选择路径的frame
# frame_main6 = tk.Frame(window, bg = "#F2F2F2", height=300, width=720)
# frame_main6.grid(column=1,row=4,columnspan=2,padx=15,pady=15)

# -----menu菜单功能,暂时不要后续添加--------
# meun_main = tk.Menu()
# # meun_main.grid()
# filemenu = tk.Menu(meun_main, tearoff=1)
# meun_main.add_cascade(label='File', menu=filemenu)
# filemenu.add_command(label='Monkey')
# filemenu.add_command(label='Screen')
# filemenu.add_command(label='Instructions')
# filemenu.add_command(label='About')
# window.config(menu = filemenu)

# -------Log缓存区域,显示自定义日志--------
# lable_log=tk.Label(frame_main6,text="日志区",font = ("微软雅黑",14))
# lable_log.grid(column=1,row=1,sticky="W")


# 控制台输出显示的位置
text_log = tk.Text(frame_main5,bg="#5B5B5B",fg="#FFFFFF")
text_log.grid(column=1,columnspan=3,row=2)

# ----------导出日志模块------------
path = ""
local_location = ""
def selectPath():
    global path, local_location
    path = askdirectory()
    print("选择的位置:"+path)
    local.delete(0,"end")
    local.insert("end",path)
    local_location=path
    text_log.insert("end","==========================注意==========================\n\n1、导出日志暂不支持多台设备同时操作,请连接一台设备操作\n\n2、相同路径导出会覆盖原结果,请手动备份或选择不同目录\n\n\n")
    text_log.yview_moveto(2)
    text_log.update()

local = tk.Entry(frame_main5, textvariable = path)
local.grid(row = 1, column = 2, sticky="W")
local.insert("end"," 点击选择导出路径......")
tk.Button(frame_main5, text = "选择路径:",bg="#AB82FF", command = selectPath).grid(row = 1, column = 1, sticky="E")


def pull_log():
    print("本地位置:"+local_location)
    os.system("adb pull sdcard/Auto/crash.log "+local_location)
    os.system("adb pull sdcard/Auto/meminfo.log "+local_location)
    os.system("adb pull sdcard/Auto/monkey.log "+local_location)
    os.system("adb pull sdcard/Auto/meminfomini.log "+local_location)
    text_log.insert("end","\n导出成功,请去目录下查看~~~~~\n")
    text_log.yview_moveto(1)
    text_log.update()

tk.Button(frame_main5, text = "导出结果",bg="#AB82FF", command = pull_log).grid(row = 1, column = 3, sticky="W")


def clear_log():
    text_log.delete("1.0","end")

button_clearLog = tk.Button(frame_main5,text="Clear",font = ("微软雅黑",8),bg="#836FFF", fg= "#FFFFFF",command=clear_log)
button_clearLog.grid(column=4,row=2,sticky="n")

# text_log.insert("end","adasd21dadd")


# ------主页lable显示引导---------------
# 刷新设备说明
lable_main1 = tk.Label(frame_main1, justify="left", height=5, width=35, bg="#E5E5E5", font = ("微软雅黑",10), text="准备工作:\n1.进入开发者选项打开测试机USB调试功能\n2.点击刷新设备\n3.手机端确认USB调试(若未提示则跳过此步骤)\n4.设备连接成功")
lable_main1.grid()
# monkey参数说明
lable_main2 = tk.Label(frame_main2, justify="left", height=5, width=40, bg="#E5E5E5", font = ("微软雅黑",10), text="参数说明:\n1.Package:包名,可点击获取,也可以手动输入\n2.Seeds:随机值,默认取当天日期,也可手动输入\n3.Throttle:点击频率,默认300ms,可手动输入\n4.Count:总操作数,默认50万,可手动修改")
lable_main2.grid()

# ----listbox用于显示当前设备列表-----
lb = tk.Listbox(frame_main3,height=9, width=28, bg="#CDCDB4", fg="#551A8B")
lb.grid(column=0,row=0,padx=10,pady=10)


# -----frame4上的参数配置-----
lab1 = tk.Label(frame_main4,text="配置项",bg="#F2F2F2",font=(11))
lab2 = tk.Label(frame_main4,text="Package",bg="#F2F2F2" )
lab3 = tk.Label(frame_main4,text="Seeds",bg="#F2F2F2" )
lab4 = tk.Label(frame_main4,text="Throttle",bg="#F2F2F2" )
lab5 = tk.Label(frame_main4,text="Count",bg="#F2F2F2" )
lab6 = tk.Label(frame_main4,text="参数",bg="#F2F2F2",font=(11) )  #第二列标题哦


lab1.grid(column=1,row=1,padx=5,pady=5)
lab2.grid(column=1,row=2,padx=5,pady=5,sticky="w")
lab3.grid(column=1,row=3,padx=5,pady=5,sticky="w")
lab4.grid(column=1,row=4,padx=5,pady=5,sticky="w")
lab5.grid(column=1,row=5,padx=5,pady=5,sticky="w")
lab6.grid(column=2,row=1,padx=1,pady=1)


# -----entry monkey参数配置-------
entry1 = tk.Entry(frame_main4)
entry2 = tk.Entry(frame_main4)
entry3 = tk.Entry(frame_main4)
entry4 = tk.Entry(frame_main4)

entry1.grid(column=2,row=2,padx=5,pady=1,sticky="w")
entry2.grid(column=2,row=3,padx=5,pady=1,sticky="w")
entry3.grid(column=2,row=4,padx=5,pady=1,sticky="w")
entry4.grid(column=2,row=5,padx=5,pady=1,sticky="w")



# ------一些可能用到的全局变量------
select_var = ""
pkg_details = ""
button_state = ""
model_name = ""
# 创建一个空list存储设备
list_device = []
# monkey配置残水
p = ""
s = ""
t = ""
c = ""

def find_and_init():
    # def find_devices():
    global list_device,model_name
    list_device.clear()
    logging.basicConfig(level=logging.INFO)
    d_lists = os.popen("adb devices").readlines()
    logging.error(d_lists)
    d_lists.remove(d_lists[len(d_lists)-1])
    d_lists.remove(d_lists[0])
    lb.delete("0","end")
    logging.info(d_lists)
    # time.sleep(3)
    if len(d_lists)==0:
        lb.insert("end","未检测到设备,请连接测试机...")
        text_log.insert("end","未检测到设备,请连接测试机...\n")
        text_log.yview_moveto(1)
        text_log.update()
    elif "unauthorized" in d_lists[0]:
        lb.insert("end","请在手机上点击允许USB调试...")
        text_log.insert("end","请在手机上点击允许USB调试...\n")
        text_log.yview_moveto(1)
        text_log.update()
    else:
        
        text_log.insert("end","设备已连接,请选择设备进行后续操作!\n\n")
        text_log.yview_moveto(1)
        text_log.update()
        for i in d_lists:
            reg_serial = re.findall("(.*?)device", i)
            model_org = os.popen("adb -s "+str(reg_serial[0])+" shell getprop ro.product.model").readlines()
            logging.info(reg_serial[0].replace("\t","")+":"+model_org[0].replace("\n",""))

            # 将设备信息添加到listbox中
            per_list = reg_serial[0].replace("\t","")
            print("设备列表为:",per_list)
            os.system("adb -s "+str(per_list)+" logcat -c")
            lb.insert("end",model_org[0].replace("\n","").replace(" ","")+":"+per_list)
            model_name = model_org[0].replace("\n","").replace(" ","")
            list_device.append(per_list)


# ------读写配置文件-----------
    # def init_config():
    entry1.delete(0,"end")
    entry2.delete(0,"end")
    entry3.delete(0,"end")
    entry4.delete(0,"end")
    # time.sleep(3)
    if os.path.exists("config.json"):
        with open('config.json',encoding="utf-8") as ff:
            json_data = json.load(ff)
            entry1.insert("end",json_data["pkg"])
            entry2.insert("end", time.strftime("%Y%m%d%H%M%S", time.localtime())[2:8])
            entry3.insert("end", json_data["thr"])
            entry4.insert("end", json_data["total"])
            
    else:
        config_file = open('config.json','w',encoding='utf-8')
        # json_data = {"pkg":"请在此处填写包名...","sd":time.strftime("%Y%m%d%H%M%S", time.localtime())[2:8],"thr":"300","total":"500000"}
        json_data = {"pkg":"请在此处填写包名...", "thr":"300","total":"500000"}
        json.dump(json_data,config_file,ensure_ascii=False)
        config_file.close()

    # p=entry1.get()
    # s=entry2.get()
    # t=entry3.get()
    # c=entry4.get()

#  -----------按钮事件--------
# 刷新设备按钮
button = tk.Button(frame_main3,text="刷新设备",font = ("微软雅黑",15),bg="#8FBC8F", fg= "#FFFFFF", command=find_and_init)
button.grid(column=0,row=1)


# --------获取当前包名---------
def get_pkg():
    global select_var
    global pkg_details
    logging.info(select_var)
    # global select_var
    selected=lb.get(lb.curselection())

    print("我也不知道这里会不会报错 ",selected)

    print("原始的",selected,type(selected))
    reg_device=".*?:(.*)"
    select_device = re.findall(reg_device, selected)
    print("选择的设备是",select_device[0])
    select_var=select_device[0]
    
    try:
        cur = os.popen("adb -s "+str(select_var)+" shell dumpsys window | findstr mCurrentFocus").readline()
        result=re.findall("mCurrentFocus=Window{.*?u0 (.*?)/.*?}", cur)
        pkg_details = result[0]
        logging.info(cur)

    except IndexError:
        entry1.delete(0,"end")
        entry1.insert("end","获取失败...")
        tk.messagebox.showerror("Error了吧,哈哈哈!","请注意:\n  1.确认设备已经连接。\n  2.需要获取包名的应用处于前台状态!")
        entry1.delete(0,"end")
        entry1.insert("end","点击重试...")

    except TypeError:
        entry1.delete(0,"end")
        entry1.insert("end","未选择设备哦!")
        tk.messagebox.showerror("Error,这个不是Bug...","没有选择设备哦!")
        entry1.delete(0,"end")
        entry1.insert("end","点击重试...")


    else:
        entry1.delete(0,"end")
        entry1.insert("end",pkg_details)
        text_log.insert("end","获取包名成功,请在包名栏查看确认!!!\n\n")
        text_log.yview_moveto(1)
        text_log.update()

def run_monkey():

    now_time = time.strftime("%Y%m%d%H%M%S", time.localtime())[4:12]

    if not os.path.exists("./script/"+str(now_time)):
        text_log.insert("end","创建必要资源文件夹....\n\n")
        text_log.yview_moveto(1)
        text_log.update()
        os.makedirs("./script/"+str(now_time))
    global p,s,t,c
    p=entry1.get()
    s=entry2.get()
    t=entry3.get()
    c=entry4.get()

    global button_state
    button_state = "disabled"
    print(pkg_details)
    text_log.insert("end","即将开始初始化资源,请稍等...\n")
    if len(list_device)>0:

        if entry1.get() in (u"请在此处填写包名...",""):
            tk.messagebox.showerror("你又错了!!!","不出意外的话你包名肯定不对!!")

        else:
            sh_monkey = "_monkey_"+str(pkg_details)+".sh"
            with open ("script\\"+str(now_time)+"\\"+sh_monkey, "w") as f:
                text_log.insert("end","生成monkey脚本...\n")
                text_log.yview_moveto(1)
                text_log.update()
                f.write('#!/system/bin/sh\na=`monkey -s '+str(s)+' -p '+str(p)+' --throttle '+str(t)+' --ignore-crashes --ignore-native-crashes --ignore-security-exceptions --ignore-timeouts --monitor-native-crashes -v -v -v '+str(c)+' > /sdcard/Auto/monkey.log`;b=`sh common.sh &`')
                

            sh_fc = "_fc_"+str(pkg_details)+".sh"
            with open ("script\\"+str(now_time)+"\\"+sh_fc, "w") as ff:
                text_log.insert("end","生成crash监控脚本...\n")
                text_log.yview_moveto(1)
                text_log.update()
                ff.write('#!/system/bin/sh\nlogcat -s AndroidRuntime>>crash.log')


            sh_memory = "_memory_"+str(pkg_details)+".sh"
            with open ("script\\"+str(now_time)+"\\"+sh_memory, "w") as fff:
                text_log.insert("end","生成内存监控脚本...\n")
                text_log.yview_moveto(1)
                text_log.update()
                fff.write('#!/system/bin/sh\nwhile true;do if [ -e "1" ];then echo `date`" -- monkey finished!!!">>meminfo.log;break;else dumpsys meminfo '+str(p)+'>>meminfo.log;sleep 30;continue;fi; done')
                

            # sh_memorymini = "_memorymini_"+str(pkg_details)+".sh"
            # with open ("script\\"+str(now_time)+"\\"+sh_memorymini, "w") as ffff:
            #     text_log.insert("end","生成内存监控脚本mini...\n\n")
            #     text_log.yview_moveto(1)
            #     text_log.update()
            #     ffff.write('#!/system/bin/sh\nwhile true;sleep 30; do if [ -e "1" ];then echo `date`" -- monkey finished!!!">>meminfo.log;break;else dumpsys meminfo '+str(p)+' | grep TOTAL:>>meminfomini.log;continue;fi; done')
       
       
        #这里区分设备,后面从这里开始  
        for d in list_device:

            os.system("adb -s "+str(d)+" shell rm -r sdcard/Auto/")
            text_log.insert("end","初始化设备环境...\n")
            text_log.yview_moveto(1)
            text_log.update()

            os.system('adb -s '+str(d)+' shell "cd sdcard && mkdir Auto')   
            text_log.insert("end","创建资源文件夹...\n")
            text_log.yview_moveto(1)
            text_log.update()


            sh_finally = glob.glob("script\\"+str(now_time)+"\\*.sh")
            for sh in sh_finally:
                print("=============")
                print("adb -s "+str(d)+" push "+"script\\"+str(now_time)+"\\"+str(sh)+" sdcard/Auto")
                os.system("adb -s "+str(d)+" push "+str(sh)+" sdcard/Auto")
            text_log.insert("end","推送测试脚本到设备...\n")
            print("=============\n\n\n")
            text_log.yview_moveto(1)
            text_log.update()

            os.system("adb -s "+str(d)+" push "+"script\\common.sh sdcard/Auto")
            text_log.insert("end","推送测试脚本到设备......\n")
            text_log.yview_moveto(1)
            text_log.update()


            # 多线程启动执行命令
            def monkey_thread():
                text_log.insert("end","启动monkey......\n")
                text_log.yview_moveto(1)
                text_log.update()
                logging.info('adb -s '+str(d)+' shell "cd sdcard && cd Auto && sh '+str(sh_monkey)+' &"')
                os.system('adb -s '+str(d)+' shell "cd sdcard && cd Auto && sh '+str(sh_monkey)+' &"')
                time.sleep(2)

            def crash_thread():
                text_log.insert("end","启动crash脚本......\n")
                text_log.yview_moveto(1)
                text_log.update()
                logging.info('adb -s '+str(d)+' shell "cd sdcard && cd Auto && sh '+str(sh_fc)+' &"')
                os.system('adb -s '+str(d)+' shell "cd sdcard && cd Auto && sh '+str(sh_fc)+' &"')
                time.sleep(2)

            def mem_thread():
                text_log.insert("end","启动内存脚本......\n")
                text_log.yview_moveto(1)
                text_log.update()
                logging.info('adb -s '+str(d)+' shell "cd sdcard && cd Auto && sh '+str(sh_memory)+' &"')
                os.system('adb -s '+str(d)+' shell "cd sdcard && cd Auto && sh '+str(sh_memory)+' &"')
            #     # os.system('adb -s '+str(d)+' shell "cd sdcard && cd Auto && sh '+str(sh_memorymini)+' &"')
                time.sleep(2)


            # monkey_thread()
            # crash_thread()
            # mem_thread()

            _thread.start_new_thread(monkey_thread, ())  
            _thread.start_new_thread(crash_thread, ())
            _thread.start_new_thread(mem_thread, ())

            logging.info("执行完毕!!!!!!!")
            time.sleep(5)
            text_log.insert("end","\n当前设备执行完毕,确认monkey开始后可拔掉数据线,执行下一台设备......\n")
            text_log.yview_moveto(1)
            text_log.update()

            # time.sleep(2)
            # print("开始启动监控脚本")
            # _thread.start_new_thread(common_thread, ())
            # os.system('adb -s '+str(d)+' shell "cd sdcard && cd Auto && sh common.sh &"')
            
            text_log.insert("end","一切准备就绪,即将启动所有骚操作!!!\n\n")
            text_log.yview_moveto(1)
            text_log.update()

            text_log.insert("end","==============================\n包名:"+p)
            text_log.insert("end","\nseeds值:"+s)
            text_log.insert("end","\nthrottle值:"+t)
            text_log.insert("end","\n总测试数:"+c+"\n\n==============================\n\n更新动态关注:https://blog.csdn.net/botree_chan/article/category/9091329\n\n")
            text_log.yview_moveto(3)
            text_log.update()

    else:
        # len(list_device)==0:
        tk.messagebox.showerror("你又错了!!!","请注意:\n  1.确认设备已经连接。\n  2.确认包名不为空!!")

# 获取包名按钮
button_getpkg = tk.Button(frame_main4,text="获取包名", fg= "#FFFFFF",font = ("",9),bg="#A020F0",command=get_pkg)
button_getpkg.grid(column=3,row=2,padx=5,pady=5)
notice_lable = tk.Label(frame_main4,text="要获取包名,\n请先选设备。", fg="red")
notice_lable.grid(column=3,row=3)

button_make_res = tk.Button()

# Monkey启动按钮
if button_state == "disabled":
    button_runmonkey = tk.Button(frame_main4,text="执行测试",state="disabled", fg= "#FFFFFF",font = ("微软雅黑",15),bg="#8FBC8F",command=run_monkey)
    button_runmonkey.grid(column=2,row=7,padx=25,pady=5,sticky="w")
else:
    button_runmonkey = tk.Button(frame_main4,text="执行测试", fg= "#FFFFFF",font = ("微软雅黑",15),bg="#8FBC8F",command=run_monkey)
    button_runmonkey.grid(column=2,row=7,padx=25,pady=5,sticky="w")
    button_state = "disabled"


window.mainloop()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值