学自动化测试的时候,使用百度的方法不知道为啥都不生效。后面在conftest.py里面整了个蠢方法。
使用conftest.py的pytest_runtest_makereport方法,在用例失败的时候截图和把录屏放到报告里面。
关掉页面的时候,录屏文件可能还会被占用,导致加上去一个空文件,所以要等没被占用的时候再添加
import os
import sys
import time
import allure
import pytest
from allure_commons.types import AttachmentType
# 目录位置可以自己定义,我用的是父目录的父目录
dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def is_file_locked(filepath) -> bool:
"""
检测文件是否被占用
"""
locked = False
try:
with open(filepath, 'rb') as fp:
pass
except:
locked = True
finally:
if fp and not fp.closed:
os.close(fp.fileno())
return locked
def wait_for_file_unlock(filepath, timeout=5):
for i in range(timeout * 2):
if is_file_locked(filepath):
break
else:
time.sleep(0.5)
@pytest.hookimpl(t