"""
-
@Time : 2019/8/28 19:50
@Auth : linux超
@File : conftest.py
@IDE : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ : 28174043@qq.com
@GROUP: 878565760
-
"""
import pytest
from selenium import webdriver
from py._xmlgen import html
driver = None
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""當測試失敗的時候,自動截圖,展示到html報告中"""
outcome = yield
pytest_html = item.config.pluginmanager.getplugin('html')
report = outcome.get_result()
extra = getattr(report, 'extra', [])
# 如果你生成的是web ui自動化測試,請把下面的代碼注釋打開,否則無法生成錯誤截圖
# if report.when == 'call' or report.when == "setup":
# xfail = hasattr(report, 'wasxfail')
# if (report.skipped and xfail) or (report.failed and not xfail): # 失敗截圖
# file_name = report.nodeid.replace("::", "_") + ".png"
# screen_img = capture_screenshot()
# if file_name:
# html = '
' % screen_img# extra.append(pytest_html.extras.html(html))
# report.extra = extra
extra.append(pytest_html.extras.text('some string', name='Different title'))
report.description = str(item.function.__doc__)
report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape") # 解決亂碼
def capture_screenshot():
'''截圖保存為base64'''
return driver.get_screenshot_as_base64()
def pytest_configure(config):
# 添加接口地址與項目名稱
config._metadata["項目名稱"] = "Linux超博客園自動化測試項目v1.0"
config._metadata['接口地址'] = 'https://www.cnblogs.com/linuxchao/'
# 刪除Java_Home
config._metadata.pop("JAVA_HOME")
@pytest.mark.optionalhook
def pytest_html_results_summary(prefix):
prefix.extend([html.p("所屬部門: xx測試中心")])
prefix.extend([html.p("測試人員: Linux超")])
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
cells.insert(1, html.th('Description'))
cells.pop(-1) # 刪除link列
@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
cells.insert(1, html.td(report.description))
cells.pop(-1) # 刪除link列
@pytest.fixture(scope='session')
def driver():
global driver
print('--open browser--')
driver = webdriver.Firefox()
yield driver
print('--close browser--')
driver.quit()