簡介:在某些特殊場景中,已通過adb進行連接,則可進行截圖,錄屏,抓取日志操作。
相關攻略:
adb:5秒完成查看App包名和啟動頁Activity
adb重啟或關機手機命令
adb:安卓手機USB調試模式
adb:win10系統下安裝
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)
微信公眾號:玩轉測試開發
歡迎關注,共同進步,謝謝!