selenium用例执行过程采集操作形成测试报告上的回复

在代码执行的过程中不断的进行截图,把截图拼接成gif动态图,放在测试报告上

1、每条用例执行启动一个线程,这个线程会每隔0.3秒进行截图

  • 项目下创建一个临时目录video用来存储所有截图以及gif动态图

  • 封装不断截图的方法,每隔0.3秒进行截图

    python 复制代码
    # 封装一个普通函数,实现每隔0.3秒截图
    # dr代表核心操作对象
    # worker_id代表当前进程id,主要是用来在多进程并发时区分不同进程下的截图
    def shot(dr,worker_id):
        global shot_flag
        shot_flag = True # 全局变量,用来标识每条用例截图的开始和结束,用例结束后把shot_flag变为fasle
        # 每次截图开始前,清除当前进程下临时图片及gif动态图
        for img in os.listdir(f'{project_path}/video'):
            # img就是拿到的每个文件名称
            if img.startswith(worker_id):
                os.remove(f'{project_path}/video/{img}')
        i = 0
        while shot_flag:
            try:
                dr.get_screetshot_as_file(f'{project_path}/video/{worker_id}_{i}.png')
                time.sleep(0.3)
                i += 1
            except:
                return
  • 没条用例开始执行的时候开启一个线程,执行截图

    python 复制代码
    @pytest.fixture(scope='function',autouse=True)
    def case_setup(worker_id):
        # 创建线程
        # target表示该线程要执行的动作,只写函数名称就行
        # args指的是要执行这个函数时需要的入参
        thd = threading.Thread(target=shot,args=(DriverOperate.globalDriverOperate,worker_id))
        thd.start()
  • 将图片按照顺序拼接成gif动态图,把当前用例形成的gif动态图放在测试报告上

python 复制代码
@pytest.fixture(scope='function',autouse=True)
def case_teardown(worker_id,common_info):
    yield
    global shot_flag
    shot_flag=False
    # 完成当前用例临时图片的拼接,形成gif动态图
    # 1.得到当前用例所有的临时图片名称
    img_list = []# 存储多个图片名称
    # 图片名称规则 gw0_0.png/gw0_1.png/gw0_2.png
    for img in os.listdir(f'{project_path}/video'):
        if img.startswith(worker_id) and img.endswith('.png'):
            img_list.append(img)

    # img_list = ['gw0_0.png','gw0_1.png','gw0_11.png','gw0_31.png']
    # 从目录得到的所有图片名称的排序上可能不对,拼接gif必须按照顺序来
    # 因此我们主要针对img_list中的图片名称进行排序
    img_list.sort(key=lambda name: int(name.split('_')[1][:-4]))
    # 完成图片拼接,需要用到一个图片操作的第三方库
    # pip  install pillow -i https://pypi.doubanio.com/simple
    first_img = Image.open(f'{project_path}/video/{img_list[0]}')
    eles_img = [] # 存储除了第一张图以外的其他图片的二进制对象
    for img in img_list[1:]:
        cur_img = Image.open(f'{project_path}/video/{img}')
        eles_img.append(cur_img)
    # 完成拼接
    first_img.save(f'{project_path}/video/{worker_id}_record.gif',
                   append_images=eles_img,
                   duration=300,# 每隔多长时间播放一张图片,单位是毫秒
                   save_all=True,
                   loop=1 # 表示循环播放次数
                   )

    # 将生成的gif动态图放入到allure测试报告上
    with open(f'{project_path}/video/{worker_id}_record.gif',mode='rb') as f:
        allure.attach(f.read(),'执行回放',attachment_type=allure.attachment_type.GIF)
    # 每次用例执行结束后,清除当前进程下临时图片及gif动态图
    for img in os.listdir(f'{project_path}/video'):
        # img就是拿到的每个文件名称
        if img.startswith(worker_id):
            os.remove(f'{project_path}/video/{img}')
相关推荐
Run Freely9373 小时前
接口测试-postman-全局变量与环境变量
测试工具·postman
我的xiaodoujiao6 小时前
从 0 到 1 搭建 Python 语言 Web UI自动化测试学习系列 8--基础知识 4--常用函数 2
前端·python·测试工具·ui
程序员小远9 小时前
常用的测试用例
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
(时光煮雨)10 小时前
【Python进阶】Python爬虫-Selenium
爬虫·python·selenium
依旧很淡定2 天前
Selenium(Python)创建Chrome浏览器实例
chrome·python·selenium
小熊出擊3 天前
【pytest】finalizer 执行顺序:FILO 原则
python·测试工具·单元测试·pytest
云闲不收3 天前
接口请求工具对比 apifox apipost swagger postman等
测试工具·postman
sitellla3 天前
Testify Go测试工具包入门教程
git·测试工具·其他·golang
我的xiaodoujiao3 天前
从 0 到 1 搭建 Python 语言 Web UI自动化测试学习系列 9--基础知识 5--常用函数 3
前端·python·测试工具·ui
可可南木3 天前
ICT 数字测试原理 8 - -VCL 的测试参数
开发语言·功能测试·测试工具·pcb工艺