测试基础 Android 和 iOS 一键截图命令

xiaoxiao · October 27, 2022 · 3461 hits

Android 一键截图

adb shell screencap -p /sdcard/screenshot_android.jpg && adb pull /sdcard/screenshot_android.jpg && adb shell rm -r /sdcard/screenshot_android.jpg

或者按当前时间命名:

name=`date +"%Y%m%d%H%M%S".jpg` && adb shell screencap -p /sdcard/DCIM/$name && adb pull /sdcard/DCIM/$name && adb shell rm -r /sdcard/DCIM/$name

iOS 一键截图

注意,需要先安装:pip3 install -U "tidevice[openssl]"

tidevice screenshot screenshot_ios.jpg

其它

如果 Android 需要直接指定坐标点截图,则可以用下面脚本 screenshot.py:

#!/usr/bin/env python3
# -*- coding: utf-8 -*- 
# File    : screenshot.py
# @Time   : 2022/10/26 20:30
# @Author : xiaoxiao

import os
import subprocess
import time

import click
from PIL import Image


@click.command()
@click.option('-s', '--serial', type=str, help='the android device serial number or ios device udid')
@click.option('-i', '--image_name', default=str(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))) + ".jpg" type=str, help='save screenshot name')
@click.option('-b', '--bounds', type=str, help='screenshot position')
def screenshot(serial, image_name, bounds):
    if serial:
        adb = "adb -s %s " % serial
    else:
        adb = "adb"
    if "." not in image_name:
        image_name = str(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))) + ".jpg"
    cmd = "%s shell screencap -p /sdcard/%s && %s pull /sdcard/%s && %s shell rm -r /sdcard/%s" % (adb, image_name, adb, image_name, adb, image_name)
    subprocess.getoutput(cmd)
    print(cmd)
    if bounds:
        points = bounds.split(",")
        points_list = []
        for v in points:
            points_list.append(int(v))
        image_bounds = tuple(points_list)
        if type(image_bounds) != tuple or len(image_bounds) != 4:
            print("输入的坐标点不正确,请重新输入!格式如:150,200,450,500")
            return
        image_path = os.getcwd() + "/" + image_name
        img = Image.open(image_path)
        img = img.convert("RGB")
        crop = img.crop(image_bounds)
        crop.save(image_path)
    print("截图完成:%s" % image_name)


if __name__ == '__main__':
    screenshot()

执行命令如下:

1、默认用当前时间点命名截图:python3 screenshot.py
2、重命名截图:python3 screenshot.py -i 重命名.jpg
3、指定设备截图:python3 screenshot.py -s {device_id}
4、指定区域截图:python3 screenshot.py -b 150,200,450,500
注意:-b的参数分别为left, upper, right, and lower pixel,输入时注意逗号之间不要有空格
No Reply at the moment.
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up