Python&selenium 自動(dòng)化測(cè)試框架之用例執(zhí)行失敗截圖功能
用例執(zhí)行失敗后自動(dòng)截圖到指定文件夾,并在allure報(bào)告中失敗用例中自動(dòng)顯示該截圖。
需要用到pytest中的hook函數(shù),如下:
該代碼可直接復(fù)用膛堤,無(wú)需修改
# 用例失敗后自動(dòng)截圖
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""
獲取每個(gè)用例的鉤子函數(shù)
:param item:
:param call:
:return:
"""
outcome = yield
rep = outcome.get_result()
# 以下為實(shí)現(xiàn)異常截圖的代碼:
# rep.when可選參數(shù)有call、setup、teardown恳谎,
# call表示為用例執(zhí)行環(huán)節(jié)、setup憋肖、teardown為環(huán)境初始化和清理環(huán)節(jié)
# 這里只針對(duì)用例執(zhí)行且失敗的用例進(jìn)行異常截圖
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")
item.name = item.name.encode("utf-8").decode("unicode-escape")
file_name = '{}.png'.format(str(round(time.time() * 1000)))
path = os.path.join(PRPORE_SCREEN_DIR, file_name)
driver.save_screenshot(path)
if hasattr(driver, "get_screenshot_as_png"):
with allure.step("添加失敗截圖"):
# get_screenshot_as_png實(shí)現(xiàn)截圖并生成二進(jìn)制數(shù)據(jù)
# allure.attach直接將截圖二進(jìn)制數(shù)據(jù)附加到allure報(bào)告中
allure.attach(driver.get_screenshot_as_png(), "失敗截圖", allure.attachment_type.PNG)
logger.info("錯(cuò)誤頁(yè)面截圖成功因痛,圖表保存的路徑:{}".format(path))
注意:因?yàn)樯鲜龇椒ㄖ凶詈笠恍杏玫搅薲river岸更,所以在conftest.py中初始化時(shí)需要定義全局driver鸵膏。
整體conftest.py如下:
import os
import allure
import pytest
from webdriver_helper import get_webdriver
driver = None
@pytest.fixture(scope='session',name="mydriver")
def driver(request):
global driver
driver = get_webdriver()
driver.maximize_window()
driver.implicitly_wait(5)
def end():
driver.quit()
request.addfinalizer(end) # 終結(jié)函數(shù)
# 這里為什么不用yield呢因?yàn)閥ield不能return,addfinalizer這個(gè)功能可以實(shí)現(xiàn)餓yield功能一樣
# 而且可以return參數(shù)傳給后面的用例
return driver
# 用例失敗后自動(dòng)截圖
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""
獲取每個(gè)用例的鉤子函數(shù)
:param item:
:param call:
:return:
"""
outcome = yield
rep = outcome.get_result()
# 以下為實(shí)現(xiàn)異常截圖的代碼:
# rep.when可選參數(shù)有call怎炊、setup谭企、teardown,
# call表示為用例執(zhí)行環(huán)節(jié)评肆、setup债查、teardown為環(huán)境初始化和清理環(huán)節(jié)
# 這里只針對(duì)用例執(zhí)行且失敗的用例進(jìn)行異常截圖
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")
item.name = item.name.encode("utf-8").decode("unicode-escape")
file_name = '{}.png'.format(str(round(time.time() * 1000)))
path = os.path.join(PRPORE_SCREEN_DIR, file_name)
driver.save_screenshot(path)
if hasattr(driver, "get_screenshot_as_png"):
with allure.step("添加失敗截圖"):
# get_screenshot_as_png實(shí)現(xiàn)截圖并生成二進(jìn)制數(shù)據(jù)
# allure.attach直接將截圖二進(jìn)制數(shù)據(jù)附加到allure報(bào)告中
allure.attach(driver.get_screenshot_as_png(), "失敗截圖", allure.attachment_type.PNG)
logger.info("錯(cuò)誤頁(yè)面截圖成功,圖表保存的路徑:{}".format(path))
使用命令執(zhí)行測(cè)試用例瓜挽,結(jié)果如下:
打開(kāi)測(cè)試報(bào)告: