简介:在某些特殊场景中,已通过adb进行连接,则可进行截图,录屏,抓取日志操作。
相关攻略:
案例1:截图
# 截图
adb shell screencap /sdcard/202206280928.png
# 拉取截图
adb pull sdcard/202206280928.png ./
案例2:录屏,按Ctrl+C停止录制
# 录屏
adb shell screenrecord /sdcard/202206280932.mp4
# 拉取截图
adb pull /sdcard/202206280932.mp4 ./
案例3:抓取日志,按Ctrl+C停止抓取
adb logcat -v time >.\log"%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%-%time:~3,2%-%time:~6,2%.txt"
改造成Python脚本形式:
# -*- coding: utf-8 -*-
# time: 2022/6/28 10:18
# file: adb_test.py
# author: tom
# 公众号: 玩转测试开发
import os
import time
import datetime
import subprocess
import threading
class ADB:
def __init__(self, device, target_path):
self.target_path = target_path
subprocess.Popen(f"adb connect {device}")
def screenshot(self):
# 使用 && 隔开。
file_name = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
step1 = rf"cd {self.target_path}"
step2 = f"adb shell screencap /sdcard/{file_name}.png"
step3 = f"adb pull sdcard/{file_name}.png ./"
command = fr"{step1} && {step2} && {step3}"
os.popen(command)
def video(self, record_time):
# 采集
file_name = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
step1 = rf"cd {self.target_path}"
step2 = f"adb shell screenrecord --time-limit {record_time} /sdcard/{file_name}.mp4"
command = fr"{step1} && {step2}"
os.system(command)
# 拉取
step4 = rf"adb pull sdcard/{file_name}.mp4 ./"
command2 = fr"{step1} && {step4}"
os.system(command2)
if __name__ == '__main__':
device = "192.168.xxx.xxx"
target_path = r"C:\adbTest\test"
adb = ADB(device, target_path)
adb.screenshot()
adb.video(10)
微信公众号:玩转测试开发
欢迎关注,共同进步,谢谢!