提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
在编写app自动化测试时,需要收集adb日志,并开启手机录屏,同时对app执行某些特定操作;代码如下,实际运行时出现了以下问题:
1、程序一直不能运行完,一直卡在log收集中(adb logcat不关闭问题)
2、录屏不能覆盖smoke操作(record、smoke线程没有同步)
while not self._stop_event.is_set():
# 在这里添加您的日志收集代码
command_collect = "adb logcat -c && adb logcat >" + self.filepath
# print("创建日志文件:"+self.filepath)
command_exit = "adb logcat -d"
print("开始收集日志")
# result = subprocess.getoutput(command_collect)
# print(result)
# subprocess.run(command_exit, shell=True)
# 启动adb日志收集
logcat_process = subprocess.Popen(command_collect, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# 收集日志
logcat_output = subprocess.getoutput(command_collect)
# 停止adb日志收集
logcat_process.kill()
# with open(self.filepath, 'w') as f:
# print("dakaiwenjian "+self.filepath)
# # subprocess.call(['adb', 'logcat'], stdout=f)
# # result = subprocess.getoutput(command_collect)
# f.write(result)
# # pyautogui.hotkey('ctrl', 'c')
time.sleep(1)
print("结束日志收集")
提示:以下是本篇文章正文内容,下面案例可供参考
一、多线程及线程同步方式
参考博客:python的多线程及线程同步方式_python线程池线程同步-CSDN博客
二、解决办法
1.解决日志不停续写问题
代码如下(示例):
做了两个修改:
增加event实践,等待日志手机完成后,set停止
使用open打开文件,在单独打开adb,解决日志不停续写问题,详情请见:adb logcat不能关闭的问题(进程再启动进程的问题)_adb logcat怎么停止-CSDN博客
command_collect = "adb logcat -v time"
with open(self.filepath, 'w') as f:
print(111)
# print("创建日志文件:"+self.filepath)
command_exit = "adb logcat -d"
# 启动adb日志收集
logcat_process = subprocess.Popen(command_collect, stdout=f, stderr=subprocess.PIPE)
# logcat_exit_process = subprocess.Popen(command_exit,stdout=subprocess.PIPE,stderr=subprocess.PIPE,
# shell=True)
self._stop_event.wait()
logcat_process.terminate()
# logcat_exit_process.terminate()
time.sleep(1)
print("结束日志收集")
f.close()
2.录屏不能覆盖smoke操作(record、smoke线程没有同步)
代码如下(示例):
这个没有办法修改,查询资料后才发现,是因为adb自带的screenrecord方法最长只能录到3min,而且有部分比较新的安卓系统不支持此方法;所以正考虑替换成openCV相关的东西。
# 通过adb在手机上录制截屏
command_start_record = "adb shell screenrecord --time-limit 10 /sdcard/" + filename
有一个属性--time-limit,范围0~180s
总结
如上