wxPython制作跑monkey工具(Python3)

一. wxPython制作跑monkey工具python文件源代码内容Run Monkey.py如下:

#!/usr/bin/env python

import wx
import os
import sys
import time

from threading import Thread


#执行adb命令函数
#使用到的线程:RunMonkeyThread(),KillMonkeyThread(),ExportLogThread()
def excuOrder(orderName):
    c = os.system(orderName)
    print(c)
    return c

#将指定内容写入指定文件(写入monkey日志报错信息)
#使用到的函数:findException()
def writeFile(FileName, content):
    FName = open(FileName, 'a')
    FName.write(content)
    FName.close()

#查找指定文件里指定字符串的个数,并输出字符串所在行的内容
#使用到的线程:ExportLogThread()
def findException(tfile,sstr):
    try:
        lines=open(tfile,'r').readlines()
        flen=len(lines)-1
        acount = 0
        fileException = "%s_%s" % (tfile,sstr)
        tfileException = "%s.txt" % fileException
       
        writeFile(tfileException,"%s keywords:\n" % fileException)
        for i in range(flen):
            if sstr in lines[i]:
                lineException = '\t%s\n'% lines[i]

writeFile(tfileException,lineException)
                acount+= 1

writeFile(tfileException,"%s  frequency:%s" % (fileException,acount))
        print('Please check Exception keywords in the "%s"\n' % tfileException)
    except Exception as e:
        print(e)

class RunMonkeyThread(Thread):


    def __init__(self):
        #线程实例化是立即启动
        Thread.__init__(self)
        self.logNameST = logNameST
        self.start()
       
    def run(self):
        print("Start running monkey ...\n")
        self.packageName=packageText.GetValue()
        self.MonkeyTime=MTText.GetValue()
        self.MonkeyCount=MCText.GetValue()
        self.strTime = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
        self.logName = '%s_%s_monkey.log'%(self.packageName,self.strTime)
        open(r"logname.txt",'w').write(self.logName)
        self.logNameST.SetLabel("%s" % self.logName)
       
        self.orderName1='adb shell "monkey -p %s --throttle %s --ignore-crashes --monitor-native-crashes \
                            --ignore-security-exceptions --ignore-timeouts --ignore-native-crashes --pct-syskeys\
                            0 --pct-nav 20 --pct-majornav 20 --pct-touch 40 --pct-appswitch 10 -v -v -v %s\
                            > /sdcard/%s&" '% (self.packageName,self.MonkeyTime,self.MonkeyCount,self.logName)
        excuOrder(self.orderName1)
       
        print("monkey finished.\n")
       

class KillMonkeyThread(Thread):


    def __init__(self):
        #线程实例化时立即启动
        Thread.__init__(self)
        self.start()
       
    def run(self):
        #杀死进程的两种命令
        #1. ps|grep monkey |awk '{print $2}' |xargs kill -9
        #2. PID=`ps |grep monkey|awk '{print $2}'`;kill -9 $PID;
        self.orderName2 = 'adb shell "ps|grep monkey |awk \'{print $2}\' |xargs kill -9"'
        excuOrder(self.orderName2)
        time.sleep(2)
        print ("Kill monkey success!")

class ExportLogThread(Thread):


    def __init__(self):
        #线程实例化时立即启动
        Thread.__init__(self)
        self.start()
       
    def run(self):
        self.logo = os.path.isfile('logname.txt')
        self.LogNameList = []
        if(self.logo):
            self.Logname_file = open('logname.txt','r')
            self.Lognames = self.Logname_file.readlines()
            self.Logname_file.close()
            for self.Logname in self.Lognames:
                self.LogNameList = self.Logname.split("_")
                self.LogFileName = self.LogNameList[0] + "_" + self.LogNameList[1]
               
                self.orderName4 = "adb pull /sdcard/%s ./MonkeyLog_%s/%s" % (self.Logname,self.LogFileName,self.Logname)
                excuOrder(self.orderName4)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/fa209430e5d2d3c4e2b62c108018d36b.html