def lanuch_app(self): ''' 冷启动多次计算平均时间 :return: ''' lanuch_result = 'fail' try: while self.lanuch_loop > 0: self.clear_app() lanuch_cmd = 'adb -s {} shell am start -W {}/{}'.\ format(self.device_name,self.pck_name,self.lanuch_activity) lanuch_info = subprocess.Popen(lanuch_cmd,shell=True, stdout=subprocess.PIPE).stdout.readlines() logger.info('启动app命令:{}'.format(lanuch_cmd)) for line in lanuch_info: if re.findall('TotalTime',line): lanuch_time = str(line).split(':')[1].strip() logger.info('本次冷启动时间:{}'.format(lanuch_time)) self.lanuch_timelist.append(lanuch_time) self.lanuch_loop -=1 time.sleep(3) lanuch_time = self.get_avg_time(self.lanuch_timelist) lanuch_result = "{}".format(round(lanuch_time/1000,2)) except Exception as e: logger.info('启动时间计算异常!:{}'.format(e)) finally: write_file(self.lanuch_app_log, lanuch_result, is_cover=True)
def uninstall_app(self): """ 仅卸载app :return: """ uninstall_result = 'fail' try: uninstall_cmd = 'adb -s {} uninstall {}'.format( self.device_name, self.pck_name) subprocess.call(uninstall_cmd, shell=True) no_uninstall = True uninstall_recordtime = 0 while no_uninstall: find_cmd = 'adb -s {} shell pm list packages | grep {}'.format( self.device_name, self.pck_name) #logger.info('查询app安装命令:{}'.format(find_cmd)) result = subprocess.Popen( find_cmd, shell=True, stdout=subprocess.PIPE).stdout.readlines() if not re.findall(self.pck_name, str(result)): logger.info('app卸载成功!') no_uninstall = False uninstall_result = 'success' else: logger.info('app卸载中!') uninstall_recordtime += 1 if uninstall_recordtime >= self.max_time: logger.info('app卸载超过最长时间:!'.format(self.max_time)) no_uninstall = False except Exception as e: logger.info('app卸载异常!'.format(e)) finally: write_file(self.uninstall_app_log, uninstall_result, is_cover=True)
def write_activity_back(activity): ''' 写备份运行的activity :return: ''' try: result = read_file(run_activity_path_back) if result != '': if not re.findall(activity,result): write_file(run_activity_path_back,activity + '\n') else: logger.info("已经存在activity!") else: write_file(run_activity_path_back, activity + '\n') except Exception as e: logger.error("备份运行的activity写入异常!{}".format(e))
def get_all_activitys(self): ''' 获取app中所有activity :return: ''' activity_list = [] try: cmd = "aapt dump xmltree {} AndroidManifest.xml".format( self.apkpath) result = os.popen(cmd).readlines() for line in result: if re.findall('Activity', line) and re.findall('Raw', line): activity = line.split("Raw:")[1].strip().replace( '"', '').replace(')', '') activity_list.append(activity) write_file(all_activity_path, activity_list) except Exception as e: logger.error('获取所有activity异常!{}'.format(e))
def install_app(self): """ 先卸载旧app,再安装新app :return: """ install_result = 'fail' try: uninstall_cmd = 'adb -s {} uninstall {}'.format( self.device_name, self.pck_name) subprocess.call(uninstall_cmd, shell=True) logger.info('卸载app命令:{}'.format(uninstall_cmd)) install_cmd = 'adb -s {} install {}'.format( self.device_name, self.app_path) subprocess.Popen(install_cmd, shell=True) logger.info('安装app命令:{}'.format(install_cmd)) starttime = self.record_time() endtime = 0 no_install = True install_recordtime = 0 while no_install: find_cmd = 'adb -s {} shell pm list packages | grep {}'.format( self.device_name, self.pck_name) #logger.info('查询app安装命令:{}'.format(find_cmd)) result = subprocess.Popen( find_cmd, shell=True, stdout=subprocess.PIPE).stdout.readlines() time.sleep(1) if re.findall(self.pck_name, str(result)): logger.info('app安装成功!') no_install = False endtime = self.record_time() install_time = str(endtime - starttime) logger.info('app安装耗时:{}s'.format(install_time)) install_result = "{}".format(install_time) else: logger.info('app安装中!') install_recordtime += 1 if install_recordtime >= self.max_time: logger.info('app安装超过最长时间:{}!'.format(self.max_time)) no_install = False except Exception as e: logger.info('app安装异常:{}'.format(e)) finally: write_file(self.install_app_log, install_result, is_cover=True)
def test_login(self): ''' 登录测试 :return: ''' login_result = 'fail' try: self.appium_driver = AppiumDriver(self.device_name, self.pck_name, self.lanuch_activity) self.driver = self.appium_driver.start_appium() time.sleep(3) self.driver.implicitly_wait(5) logger.info("启动app中.....") # if self.driver.find_elements(By.XPATH,self.allow): # self.driver.find_element(By.XPATH,self.allow).click() # elif self.driver.find_elements(By.XPATH,self.allow_zn): # self.driver.find_element(By.XPATH,self.allow_zn).click() # elif self.driver.find_elements(By.XPATH,self.sure): # self.driver.find_element(By.XPATH,self.sure).click() flag = True while flag: if self.driver.find_elements(By.XPATH, self.allow): self.driver.find_element(By.XPATH, self.allow).click() elif self.driver.find_elements(By.XPATH, self.allow_zn): self.driver.find_element(By.XPATH, self.allow_zn).click() elif self.driver.find_elements(By.XPATH, self.sure): self.driver.find_element(By.XPATH, self.sure).click() elif self.driver.find_elements(By.XPATH, self.skip): self.driver.find_element(By.XPATH, self.skip).click() else: flag = False break login_result = 'success' logger.info('登录成功') except Exception as e: logger.info('登录测试异常:{}'.format(e)) finally: self.appium_driver.kill_appium() write_file(logintest_app_log, login_result, is_cover=True)
output = process.read() res = output.split() num = re.findall("processor", str(res)) return len(num) def get_cpu(self): ''' 统计cpu的占用率 :return: ''' cpu = 0 try: cmd = "adb -s {} shell dumpsys cpuinfo | grep {}".format( self.device_name, self.pck_name) result = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE).stdout.readlines() for line in result: if re.findall(self.pck_name, line): cpu = line.split()[0].replace('%', '') break except Exception, e: logger.error("获取cpu失败:{}".format(e)) finally: current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) info = current_time + ',' + str(cpu) + ',' + self.activity + '\n' write_file(cpu_path, info, is_cover=False) if __name__ == '__main__': GetCPU('192.168.56.101:5555', 'xxxx', 'com.tencent.news').get_cpu()
cmd = 'adb -s %s shell cat /proc/uid_stat/%s/tcp_rcv' % ( self.device_name, uid) # 下载流量 pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout downdata = int(pipe.read().split('/')[0]) total = (format( float(updata + downdata) / float(1024 * 1024), '.3f')) except Exception, e: logger.error('获取真机流量失败:%s' + str(e)) finally: current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 发生时间 info = current_time + ',' + str( total) + ',' + self.activity + ',' + '\n' write_file(network_path, info, is_cover=False) def simu_network(self): ''' 获取模拟器的流量 获取上传和下载的流量 :return: ''' total = '' try: pid = get_app_pid(self.device_name, self.pck_name) cmd = 'adb -s %s shell cat /proc/%s/net/dev' % (self.device_name, pid) # 获取流量命令 pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
try: cmd = "adb -s %s shell dumpsys gfxinfo %s" % (self.device_name, self.pck_name) result = os.popen(cmd).read().strip() frames = [x for x in result.split('\n') if validator(x)] frame_count = len(frames) jank_count = 0 vsync_overtime = 0 render_time = 0 for frame in frames: time_block = re.split(r'\s+', frame.strip()) if len(time_block) == 3: try: render_time = float(time_block[0]) + float( time_block[1]) + float(time_block[2]) except Exception as e: render_time = 0 if render_time > 16.67: jank_count += 1 if render_time % 16.67 == 0: vsync_overtime += int(render_time / 16.67) - 1 else: vsync_overtime += int(render_time / 16.67) fps = int(frame_count * 60 / (frame_count + vsync_overtime)) except Exception, e: logger.error("获取fps失败:{}".format(e)) finally: current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) info = current_time + ',' + str(fps) + ',' + self.activity + '\n' write_file(fps_path, info, is_cover=False)
class GetMem(): def __init__(self, device_name, activity, pck_name): self.device_name = device_name self.pck_name = pck_name self.activity = activity def get_mem(self): ''' 获取内存 :return: ''' mem = '' try: cmd = "adb -s {} shell dumpsys meminfo {}".format( self.device_name, self.pck_name) result = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE).stdout.readlines() for line in result: if line.startswith(' Dalvik Heap'): mem = float(line.split()[3]) / 1024 mem = round(mem, 2) except Exception, e: logger.error("获取内存失败:{}".format(e)) finally: current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) info = current_time + ',' + str(mem) + ',' + self.activity + '\n' write_file(mem_path, info, is_cover=False)