【Airtest】使用aircv实现局部截图

本人在一家游戏公司上班,担任测试一职,公司使用Unity引擎开发游戏,所以会运用到一些技术,提高工作效率,在这里想跟大家分享一下。


为什么要使用这项技术?

  1. 方便后期维护

    当策划调整游戏界面的时候,减少维护自动化用例的时间,大大提升效率

  2. 增加稳定性

    因为是根据元素坐标截图的,所以截图的位置非常准确,还可以随意控制

示例代码如下:

from airtest.core.api import *
from airtest.aircv import *
from poco.drivers.unity3d import UnityPoco
def testsnapshot(elm,dvaluey1,dvaluex2,dvaluey2,dvaluex1):
    """
    :param elm: 截图位置的元素
    :param dvaluey1: 需要偏移的坐标值
    :param dvaluex2: 需要偏移的坐标值
    :param dvaluey2: 需要偏移的坐标值
    :param dvaluex1: 需要偏移的坐标值
    """
    # 获取设备屏幕分辨率(横屏)
    height = G.DEVICE.display_info['width']
    width = G.DEVICE.display_info['height']
    print(width,height)   # 打印屏幕分辨率,供调试
    bound =elm.get_bounds()  # 获取元素的bounds值(右上、右下、左下、左上),这里的值为相对坐标
    print(bound)  # 打印bounds值,供调试
    # 把四个bounds值分别提取出来,dvalue是让测试人员填写的误差值,无误差直接填0
    # 将 相对坐标 转换成 绝对坐标
    y1 = (bound[0]-dvaluey1) * height # 右上
    x2 = (bound[1]-dvaluex2) * width # 右下
    y2 = (bound[2]-dvaluey2) * height # 左下
    x1 = (bound[3]-dvaluex1) * width # 左上
    print(y1,x2,y2,x1)   # 打印经过处理后的bounds值(绝对坐标),供调试
    # 将bounds值的数据类型转为 str,如果不是str,底下aircv.crop_image函数在调用的时候会报错
    yy1 = str(round(y1))
    xx2 = str(round(x2))
    yy2 = str(round(y2))
    xx1 = str(round(x1))
    screen = G.DEVICE.snapshot(quality=99)      # 手机全屏截图
    a = aircv.crop_image(screen,(xx1,yy1,xx2,yy2)) # 开始区域截图
    SavePath = 'D:\\testpic'  # 保存图片地址
    filename = 'testpic.png'    # 图片名称
    filepath = os.path.join(SavePath, filename)
    aircv.imwrite(filepath, a, 99)
if __name__ == '__main__':
    poco = UnityPoco()
    testsnapshot(poco("WindowRoot").offspring("GunSetting").offspring("ShotgunFireMode").child("Bg"),0,0.073,0,0.069)
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享