def write_html(): try: #获取图片路径 new_report = gl.newreport() mhtml = new_report + 'test.html' f = open(mhtml, 'w') f.close() Fobj = open(mhtml, encoding='utf-8') data = Fobj.read() Fobj.close() # soup=BeautifulSoup(data,'lxml') write_report = open(mhtml, 'a+') # 将png写到html用,用cid content-id方式 write_report.write("</br><strong>" + "TVsports Ram chart:" + "</strong></br>") write_report.write("<img src='cid:%s'/></br>" % gl.memtotall) write_report.write("<img src='cid:%s'/></br>" % gl.cputotall) write_report.write("<img src='cid:%s'/></br>" % gl.cpuppdata) write_report.write("<img src='cid:%s'/></br>" % gl.memppdata) write_report.write("<img src='cid:%s'/></br>" % gl.cpuguard) write_report.write("<img src='cid:%s'/></br>" % gl.memguard) # write_report.write("<img src='cid:ppdata'/>" + "</br>") write_report.close() except Exception as e: traceback.print_exc() atp_log.error('写入html失败') atp_log.error(traceback.format_exc()) raise e
def catchMemCpu(): # 文件的存储路径,不能写在方法外,导入包时会先执行这些代码 IPPORT = gl.ipport filepath = gl.newreport() cpuppdatarpath = filepath + gl.cpuppdata + '.csv' cpuguardpath = filepath + gl.cpuguard + '.csv' cputotallpath = filepath + gl.cputotall + '.csv' memppdatapath = filepath + gl.memppdata + '.csv' memguardpath = filepath + gl.memguard + '.csv' memtotallpath = filepath + gl.memtotall + '.csv' usedrampath = filepath + gl.usedram + '.csv' try: #获取当前时间 ctime=time.strftime('%Y%d%m%H%M%S',time.localtime()) #adb指令 cmdcpuinfo='adb -s '+IPPORT+' shell dumpsys cpuinfo |findstr com.pptv.tvsports' mem_a='adb -s '+IPPORT+' shell dumpsys meminfo com.pptv.tvsports.a |findstr TOTAL' mem_ppdata = 'adb -s '+IPPORT+' shell dumpsys meminfo com.pptv.tvsports:ppdata |findstr TOTAL' mem_guard = 'adb -s '+IPPORT+' shell dumpsys meminfo com.pptv.tvsports:guard |findstr TOTAL' #打开系统,发送指令 cpuinfo=os.popen(cmdcpuinfo) for eachline in cpuinfo.readlines(): #在返回的cpu中进行过滤,注意带上:,将结果写入文件中 if 'com.pptv.tvsports.a:' in eachline: a_cpu=eachline.strip(' ').split(' ')[0].strip('%') print(a_cpu) writexlwt(cputotallpath,ctime,a_cpu) elif 'com.pptv.tvsports:ppdata' in eachline: ppdata_cpu=eachline.strip(' ').split(' ')[0].strip('%') # cpuprovider.append(provider_cpu) writexlwt(cpuppdatarpath,ctime,ppdata_cpu) elif 'com.pptv.tvsports:guard:' in eachline: # cpuguard.append(ctime) guard_cpu=eachline.strip(' ').split(' ')[0].strip('%') # cpuguard.append(guard_cpu) writexlwt(cpuguardpath,ctime,guard_cpu) #将com.pptv.tvsport.a的进程的mem写进内存中,内存取的是总内存 meminfo_a = os.popen(mem_a).read() if len(meminfo_a)>0: a_mem=meminfo_a.split( )[1] writexlwt(memtotallpath,ctime,a_mem) # 将com.pptv.tvsport.ppdata的进程的mem写进内存中,内存取的是总内存 meminfo_ppdata=os.popen(mem_ppdata).read() if len(meminfo_ppdata)>0: ppdata_mem=meminfo_ppdata.split( )[1] writexlwt(memppdatapath,ctime,ppdata_mem) # 将com.pptv.tvsport.guard的进程的mem写进内存中,内存取的是总内存 meminfo_guard=os.popen(mem_guard).read() if len(meminfo_guard)>0: guard_mem=meminfo_guard.split( )[1] writexlwt(memguardpath,ctime,guard_mem) # global t # t = threading.Timer(2, catchMemCpu) # t.start() except Exception as e: traceback.print_exc() atp_log.error(traceback.format_exc()) raise e
def monkey_android(self, IPPORT, number): filepath = gl.newreport() monkeyresult = os.popen( 'adb -s ' + IPPORT + ' shell monkey -p com.pptv.tvsports -s 2992 --pct-nav 30 --pct-syskeys 30 --pct-majornav 30 --pct-appswitch 10 --throttle 300 --ignore-timeouts --ignore-crashes --ignore-security-exceptions -v -v -v ' + number).read() print(monkeyresult) file_monkey = open(filepath + '\\monkey.log', 'w', encoding='utf-8') file_monkey.write(monkeyresult) file_monkey.close()
def send_email(): try: #写html write_html() #邮件的配置项:发件人,收件人,标题,用户及密码 sender = '*****@*****.**' reciever = ['*****@*****.**'] subject = 'Tvsports Daily Stability Auto Test 3.7.1 on ' + gl.ipport smtpserver = 'mail.pptv.com' username = '******' password = '******' # username=gl.email_name # password=gl.email_password #获取测试报告的路径 new_report = gl.newreport() #创建一个带附件的邮件实例 msg = MIMEMultipart() f = open(new_report + 'test.html', 'rb') mail_body = f.read() f.close() part = MIMEText(mail_body, 'html', 'utf-8') msg.attach(part) #打开图片目录 pnglist = [gl.cputotall, gl.cpuppdata, gl.memtotall, gl.memppdata] for png in pnglist: fp_png = open(new_report + png + '.png', 'rb') msgImage = MIMEImage(fp_png.read()) fp_png.close() msgImage.add_header('Content-ID', '<%s>' % png) msg.attach(msgImage) # 以测试报告作为文件内容 # f1=open(new_report+'result.html','rb') # mail_body1=f1.read() # f1.close() # 将BeautifulReport # part1=MIMEText(mail_body1,'html','utf-8') # msg.attach(part1) msg['Subject'] = Header(subject, 'utf-8') smtp = smtplib.SMTP() server = SMTP_SSL(host=gl.smtp_server) smtp.connect(smtpserver, 587) smtp.set_debuglevel(1) smtp.login(username, password) smtp.sendmail(sender, reciever, msg.as_string().encode('ascii')) smtp.quit() except Exception as e: traceback.print_exc() atp_log.error('邮件发送失败') atp_log.error(traceback.format_exc()) raise e
def readlog(): try: #获取日志路径 new_report=gl.newreport() print(new_report) global logerrornumber logpath=new_report+device+'_'+'.log' error_log_path=new_report+'error.log' #将错误日志写到error中 error_file=open(error_log_path,'w',encoding='utf-8') logcat_file=open(logpath,'r',encoding='utf-8',errors='ignore') #统计错误日志数量 logerrornumber=0 for line in logcat_file.readlines(): if 'FATAL EXCEPTION' in line or 'Out of memmory' in line or 'Application Not Responding:com.pptv.tvsports' in line or 'ANR in com.pptv.tvsports' in line or 'D GlobalCrashHandler' in line or 'Record By Bugly' in line or 'not enough mem'in line or 'crash occur' in line or 'anr error' in line or 'CRASH:com.pptv.tvsports.a' in line: print('error') logerrornumber+=1 error_file.write(line) error_file.close() logcat_file.close() f_error=open(error_log_path,'r',encoding='utf-8') txt=f_error.read() #统计出现各类异常的次数 global countFatal global countOutofMemory global countANR global countCrash patt=re.compile('Process com.pptv.tvsports.a(.*?) has died') counter=collections.Counter(patt.findall(txt)) countCrash=sum(counter.values())+txt.count('crash occur')+txt.count('CRASH:com.pptv.tvsports.aa') countOutofMemory=txt.count('Out of memory')+txt.count('not enough mem') countFatal=txt.count('FATAL EXCEPTION') countANR=txt.count("Application Not Responding: com.pptv.tvsports")+txt.count("ANR in com.pptv.tvsports") + txt.count('anr error') print(countFatal) print(countOutofMemory) print(countANR) print(countCrash) f_error.close() except Exception as e: traceback.print_exc() atp_log.error(traceback.format_exc()) raise e
def paint(picname, title): try: #获取图片所在路径 global newreportpath newreportpath = gl.newreport() print('.........draw picture.......') plt.figure(picname) filename = newreportpath + picname + '.csv' #获取csv中的数据,转化为x,y轴数据 x, y = get_weight_data(filename) x = range(len(x)) maxy = int(max(y)) average = int(sum(y) / len(y)) #plot绘制折线图,设置标题,线条尺寸和颜色 plt.plot(x, y, label=title, linewidth=1, color='r') if 'CPU' in picname: # 设置图片的名称 plt.title('Max:' + str(maxy) + '% Average:' + str(average) + '%') #设置x轴的标签 plt.xlabel(title) #设置y轴标签 plt.ylabel('CPU Percent') else: plt.title('Max:' + str(maxy) + 'KB Average:' + str(average) + 'KB') plt.xlabel(title) plt.ylabel('PSS(KB)') #设置y轴高度 if picname == 'a': plt.ylim(0.0, 400000) elif picname == 'MEM_a': plt.ylim(0.0, 450000) elif 'CPU' in picname: plt.ylim(0.0, 250) else: plt.ylim(0.0, 150000) #将图片保存在本地 plt.savefig(newreportpath + picname + '.png') except Exception as e: traceback.print_exc() atp_log.error(traceback.format_exc()) raise e
def getlog(): try: ipport = gl.ipport #获取测试路径 filepath=gl.newreport() #获取测试机器机型 global device device=os.popen('adb -s '+ipport+' shell getprop ro.product.model').read().strip() print(device) #获取android版本 androidVersion=os.popen('adb -s '+ipport+' shell getprop to.build.version.release').read().strip() #生成log目录 filename=filepath+device+'_'+'.log' log_file=open(filename,'w',encoding='utf-8') #抓取log,并保存到log中 logcmd='adb -s '+ipport+' logcat -v threadtime' os.system('adb -s '+ipport+' logcat -c ') time.sleep(3) global Poplog Poplog=subprocess.Popen(logcmd,stdout=log_file,stderr=subprocess.PIPE) except Exception as e: traceback.print_exc() atp_log.error(traceback.format_exc()) raise e
reportPath = gl.reportpath() #将测试case加载到uinitest的suite中 suite = TestSuite() testcases = (testMonkey, ) for test_class in testcases: tests = unittest.TestLoader().loadTestsFromTestCase(test_class) suite.addTest(tests) #创建测试文件目录 if not os.path.isdir(reportPath): os.mkdir(reportPath) #开始抓取日志 logs.getlog() #开始抓取cpu,mem catches() #运行测试用例,使用beautifulreport生成测试报告 resultpath = gl.newreport() run = bf(suite) run.report(filename=gl.report_name, description='test', log_path=resultpath) # fp = open(report_abspath, 'wb') # runner = HtmlTestRunner.HTMLTestRunner(stream=fp, output=reportPath,report_title='test report',descriptions='test result') # runner.run(suite) # fp.close() time.sleep(10) #停止抓取cpu,mem catchstop() #将抓取的cpu,mem绘制成图片 print('.....strt draw pics.......') paint('CPU_a', 'com.pptv.tvsports.a') paint('CPU_ppdata', 'com.pptv.tvsports:ppdata')