iOS自动化录屏在Chrome浏览器打不开处理方法

98 阅读1分钟

在iOS自动化测试时,生成的allure测试报告当有视频日志时,iOS的录屏chrome打不开,safari可以,这是因为iPhone的录屏压缩格式是:mjpeg ,chrome可能不支持;将ffmpeg转换到H264即可正常打开

@pytest.fixture(scope="module", autouse=True)
def index_reset(driver_init, request):
    driver = driver_init
    driver.start_recording_screen(**record_options)
    log.info(f"{request.module.__name__} 开始录屏")
    yield
    log.info(f"{request.module.__name__} 运行结束")
    log.info("index reset teardown")

    video_base64 = driver.stop_recording_screen()
    video = base64.b64decode(video_base64)
    mp4_file_name = f"./outputs/videos/{run_uuid}_{request.module.__name__}.mp4"
    with open(mp4_file_name, "wb"as file:
        file.write(video)
    output_file_name = f"./outputs/videos/{run_uuid}_{request.module.__name__}_new.mp4"
    # 使用FFmpeg处理视频: ffmpeg -i a6d4eb0e60b8cab1.mp4 -c:v libx264 -crf 23 -c:a aac a6d4eb0e60b8cab1_new.mp4
    try:
        subprocess.run([
            'ffmpeg',
            '-i', mp4_file_name,
            '-c:v''libx264',
            '-crf''23',
            '-c:a''aac',
            output_file_name
        ], check=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        print(f"视频处理完成,保存为: {output_file_name}")
    except subprocess.CalledProcessError as e:
        print(f"FFmpeg处理失败: {e.stderr.decode()}")
    allure.attach.file(output_file_name,
                       name=f"{request.module.__name__}_new录屏视频",
                       attachment_type=allure.attachment_type.MP4,
                       extension="mp4")

driver_init代码

"""
config.py文件
capabilities_ios = dict(
    platformName='iOS',
    automationName='XCUITest',
    udid='XXXXX-XXX-XXX',  # 设备名
    bundleId='com.xxx.com',  # app入口
)
record_options = {
    # 'timeLimit': '180',  # 录屏时长限制,单位秒
    'videoSize': '1080x1920',  # 视频分辨率
    'bitRate': 400000,  # 视频比特率
    'fps': 15  # 帧率
}
"""
@pytest.fixture(scope="session", autouse=True)
def driver_init(request):
    ...
    driver = webdriver.Remote(appium_server_url, options=XCUITestOptions().load_capabilities(capabilities_ios))
    yield driver
    log.info("session driver quit")
    driver.quit()

这样,iOS自动化录屏文件在Chrome浏览器也可以查看了。

公众号:自动化测试实战