第一种方式
import pytest
from selenium import webdriver
import os
import allure
_driver = None
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
'''
获取每个用例状态的钩子函数
:param item:
:param call:
:return:
'''
outcome = yield
rep = outcome.get_result()
if rep.when == "call" and rep.failed:
mode = "a" if os.path.exists("failures") else "w"
with open("failures", mode) as f:
if "tmpdir" in item.fixturenames:
extra = " (%s)" % item.funcargs["tmpdir"]
else:
extra = ""
f.write(rep.nodeid + extra + "\n")
if hasattr(_driver, "get_screenshot_as_png"):
with allure.step('添加失败截图...'):
allure.attach(_driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)
第二种方式
def screenshot_allure(func):
def get_err_screenshot(self,*args,**kwargs):
try:
func(self,*args,**kwargs)
except Exception as e:
png = self.driver.get_screenshot_as_png()
name = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
allure.attach(png, name, allure.attachment_type.PNG)
raise e
return get_err_screenshot
@screenshot_allure
def step(self,data,run_case):
tc_id = run_case[TestSteps.STEP_TC_ID]
steps = data.get_steps_by_tc_id(tc_id)
allure.dynamic.feature(run_case[TestCases.CASES_NOTE])
allure.dynamic.story(run_case[TestCases.CASES_DESC])
allure.dynamic.title(run_case[CaseData.DATA_CASE_ID]+"-"+run_case[CaseData.DATA_NAME])
for step in steps:
log.debug("执行步骤{}".format(step))
elements = step[TestSteps.STEP_ELEMENT_NAME]
element = data.get_elements_by_element(step[TestSteps.STEP_TC_ID], elements)
log.debug("元素信息{}".format(element))
operate = self.get_keyword(step[TestSteps.STEP_OPERATE])
if operate:
param_value = dict()
action_method = getattr(Action(self.driver),operate)
log.debug("该关键字是{}".format(operate))
by = element[Elements.ELE_BY]
value = element[Elements.ELE_VALUE]
send_value = step[TestSteps.STEP_DATA]
expect = run_case[CaseData.DATA_EXPECT_RESULT]
param_value["by"] = by
param_value["value"] = value
param_value["expect"] = expect
if send_value:
data_input = run_case[CaseData.DATA_INPUT]
send = self.str_to_dict(data_input)
param_value["send"] = send[send_value]
with allure.step(step[TestSteps.STEP_NAME]):
action_method(**param_value)
else:
log.error("没有operate信息:{}".format(operate))