app應用啟動測試,並將資料寫入csv檔案
阿新 • • 發佈:2018-11-09
import os import subprocess import codecs import time import re from decimal import * import csv class Start_APP(object): all_info=[] def get_devicesinfo(self): devices_name=subprocess.Popen('adb shell getprop ro.product.model',stdout=subprocess.PIPE,stdin=subprocess.PIPE) # print('裝置名稱:',str(devices_name.stdout.read(),'utf-8').strip()) self.all_info.append(str(devices_name.stdout.read(),'utf-8').strip()) devices_version=subprocess.Popen('adb shell getprop ro.build.version.release',stdout=subprocess.PIPE,stdin=subprocess.PIPE) # print('android版本:',str(devices_version.stdout.read(),'utf-8').strip()) self.all_info.append(str(devices_version.stdout.read(),'utf-8').strip()) devices_resolution=subprocess.Popen('adb shell wm size',stdout=subprocess.PIPE,stdin=subprocess.PIPE) # print('解析度:',str(devices_resolution.stdout.read(),'utf-8').strip()) self.all_info.extend(re.findall(r'Physical size: (.*)',str(devices_resolution.stdout.read(),'utf-8').strip())) app_run=subprocess.Popen('adb shell dumpsys package cn.rainbow.westore',stdout=subprocess.PIPE,stdin=subprocess.PIPE) # print('app版本號:',str(app_run.stdout.read(),'utf-8'.strip())) app_version=re.findall(r"versionName=(.*)",str(app_run.stdout.read(),'utf-8').strip()) self.all_info.append(app_version[0].strip()) return self.all_info def test_app(self): end=subprocess.Popen('adb shell am force-stop cn.rainbow.westore',stdout=subprocess.PIPE,stdin=subprocess.PIPE) end.stdout.read() time.sleep(2) start=subprocess.Popen('adb shell am start -W -n cn.rainbow.westore/.ui.LauncherActivity',stdout=subprocess.PIPE, stdin=subprocess.PIPE) app_all=start.stdout.read() # print(app_all) time_app=re.findall(r"ThisTime: \d+",str(app_all)) print(time_app[0]) times_app=re.findall(r"ThisTime: (\d+)",time_app[0]) self.all_info.extend(times_app) time.sleep(5) return time_app[0],self.all_info def get_date(self): date_now=time.strftime('%Y-%m-%d') self.all_info.append(date_now) return self.all_info def write_csv(self): if os.path.isfile('test_launch.csv'): with open('test_launch.csv','a',newline='')as f: csv_write=csv.writer(f,dialect='excel') csv_write.writerow(self.all_info) else: with open('test_launch.csv','w',newline='')as f: csv_write=csv.writer(f,dialect='excel') csv_write.writerow(['裝置名稱','android版本號','解析度','app版本號','第一次','第二次','第三次','平均值','測試時間']) csv_write.writerow(self.all_info) if __name__=='__main__': S=Start_APP() S.get_devicesinfo() time_all=[] count=0 while True: time_all.append(S.test_app()[0]) count+=1 if count>=3: break # print(time_all) time_sum=0 for i in time_all: i=int(re.findall(r"\d+",i)[0]) time_sum+=i # print(time_sum) print("平均使用時間:%0.2f"%(time_sum/3)) # print("平均使用時間:",Decimal(time_sum/3).quantize(Decimal('0.00'),rounding=ROUND_HALF_UP)) avg_time=Decimal(time_sum/3).quantize(Decimal('0.00'),rounding=ROUND_HALF_UP) S.all_info.append(str(avg_time)) S.get_date() S.write_csv() print(S.all_info)