# 声明一个黑名单
def black_wrapper(fun):
def run(*args, **kwargs):
basepage = args[0]
try:
return fun(*args, **kwargs)
except Exception as e:
for black in black_list:
eles = basepage.driver.find_elements(*black)
if len(eles) > 0:
eles[0].click()
return fun(*args, **kwargs)
raise e
return run
@black_wrapper
def find(self, by, locator)
return self.driver.find_element(by, locator)
异常截图
- 封装
screenshot()
截图方法
- 出现异常时调用截图
# 封装截图方法
def screenshot(self, path):
self.driver.save_screenshot(path)
...
# 声明一个黑名单
def black_wrapper(fun):
def run(*args, **kwargs):
basepage = args[0]
try:
return fun(*args, **kwargs)
except Exception as e:
basepage.screenshot("tmp.png")
for black in black_list:
eles = basepage.driver.find_elements(*black)
if len(eles) > 0:
eles[0].click()
return fun(*args, **kwargs)
raise e
return run
生成报告
import logging
def black_wrapper(fun):
def run(*args, **kwargs):
basepage = args[0]
try:
logging.info(f"查找元素:{args[2]}")
return fun(*args, **kwargs)
except Exception as e:
basepage.screenshot("tmp.png")
with open("./tmp.png", 'rb') as f:
picture_data = f.read()
allure.attach(picture_data, attachment_type=\
allure.attachment_type.PNG)
for black in black_list:
eles = basepage.driver.find_elements(*black)
if len(eles) > 0:
eles[0].click()
return fun(*args, **kwargs)
raise e
return run
数据驱动
@pytest.mark.parametrize('参数1,参数2', [
('参数1-值1', '参数2-值1'),
('参数1-值2', '参数2-值2'),
])
总结
- 异常处理(弹窗黑名单),异常截图
- 日志记录
- 报告生成
- 测试数据的数据驱动