1
全局文件 的完整代码
import pytest
from selenium import webdriver
from py._xmlgen import html
_driver = None
# 测试失败时添加截图和测试用例描述(用例的注释信息)
@
def pytest_runtest_makereport(item):
"""
当测试失败的时候,自动截图,展示到 HTML 报告中
:param item:
"""
pytest_html = ('html')
outcome = yield
report = _result()
extra = getattr(report, 'extra', [])
if == 'call' or == "setup":
xfail = hasattr(report, 'wasxfail')
if ( and xfail) or ( and not xfail):
file_name = ("::", "_") + ".png"
screen_img = _capture_screenshot()
if file_name:
html = '<div><img src="data:image/png;base64,%s" alt="screenshot"
style="width:600px;height:300px;" ' \
'onclick="()" align="right"/></div>' %
screen_img
((html))
= extra
= str(.__doc__)
= ("utf-8").decode("unicode_escape")
@(optionalhook=True)
def pytest_html_results_table_header(cells):
(1, ('Description'))
(2, ('Test_nodeid'))
(2)
()
软件测试任务驱动教程
2
@(optionalhook=True)
def pytest_html_results_table_row(report, cells):
(1, ())
(2, ())
(2)
()
def _capture_screenshot():
"""
截图保存为 base64
:return:
"""
return _screenshot_as_base64()
@(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = _result()
= str(.__doc__)
# = ("utf-8").decode("unicode_escape") # 设置编
码显示中文
# 这里设置的级别是模块级别,也就是每个测试文件运行一次
# 可以设置为 session,全部用例执行一次,但是针对 126 邮箱的话
# 登录次数太多会需要验证,如果验证就没法执行用例了,这里没有对验证处理(处
理比较复杂)
@(scope='module')
def driver():
global _driver
print('------------open browser------------')
_driver = ()
_window()
yield _driver
print('------------close browser------------')
()