代码改变世界

Android、iOS如何实现自动化录屏,超实用!

2021-07-31 08:43  zouhui  阅读(632)  评论(1编辑  收藏  举报

阅读本文大约需要1分钟。

背景

在做移动端自动化测试的过程中,有很多场景需要录制设备的屏幕视频,比如Crash现场记录,启动/页面加载耗时类的评测等,那么如何实现Android和iOS设备的屏幕录制呢?

Android

其实Android系统本身提供了一个简单的adb命令实现,虽然在有些设备上存在兼容性问题,比如华为手机出厂就删除了 screenrecord 录屏工具,不过在大部分机型上还是可以适用的(针对这个适配问题,后面会专门写一篇文章介绍一种兼容所有机型的方案),今天先来分享一下这个系统的原生实现。

这里推荐一个开源的框架adbutils,他是一个用纯Python实现的adb服务,里面对原生的screenrecord做了比较好的封装,先装依赖:

pip3 install adbutils

连接ADB Server:

import adbutils


adb = adbutils.AdbClient(host="127.0.0.1", port=5037)
print(adb.devices())

录屏方法:

# run screenrecord to record screen
r = d.screenrecord()
# sleep for a while, can not large then 3 minutes
r.stop() # stop recording
r.stop_and_pull("video.mp4") # stop recording and pull video to local, then remove video from device


# control start time manually
r = d.screenrecord(no_autostart=True)
r.start() # start record
r.stop_and_pull("video.mp4") # stop recording and pull video to local, then remove video from device

iOS

系统要求

  • iOS系统8.0以上

  • MacOS系统10.10以上

安装

下载仓库源码,目前这个自动化录屏工具是一个二进制的文件,在代码仓库的bin目录下:

git clone https://github.com/WPO-Foundation/xrecord.git

介绍

xrecord --help
-l, --list: List available capture devices.
-n, --name: Device Name.
-i, --id: Device ID.
-o, --out: Output File.
-f, --force: Overwrite existing files.
-q, --quicktime: Start QuickTime in the background (necessary for iOS recording).
-t, --time: Recording time in seconds (records until stopped if not specified).
-u, --quality: Recoding quality (low, medium, high, photo - defaults to high).
-d, --debug: Display debugging info to stderr.
-h, --help: Prints a help message.

使用

列出可捕获的设备:

$ xrecord --quicktime --list
Available capture devices:
AppleHDAEngineInput:1B,0,1,0:1: Built-in Microphone
5f355a5b183b2d2d7ba91dcfadd4c14b98504642: iPhone
CC2437519T1F6VVDH: FaceTime HD Camera

开始录屏:

$ xrecord --quicktime --name="iPhone" --out="out.mp4" --force -q -t 10

end