我正在尝试使用 pytest-html 和 selenium 生成一个自包含的 html 报告。我一直在尝试将屏幕截图嵌入到报告中,但它们没有显示出来。
我的 conftest.py 看起来像这样
@pytest.fixture()
def chrome_driver_init(request, path_to_chrome):
driver = webdriver.Chrome(options=opts, executable_path=path_to_chrome)
request.cls.driver = driver
page_object_init(request, driver)
driver.get(URL)
driver.maximize_window()
yield driver
driver.quit()
# Hook that takes a screenshot of the web browser for failed tests and adds it to the HTML report
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item):
pytest_html = item.config.pluginmanager.getplugin("html")
outcome = yield
report = outcome.get_result()
extra = getattr(report, "extra", [])
if report.when == "call":
feature_request = item.funcargs['request']
driver = feature_request.getfixturevalue('chrome_driver_init')
nodeid = item.nodeid
xfail = hasattr(report, "wasxfail")
if (report.skipped and xfail) or (report.failed and not xfail):
file_name = f'{nodeid}_{datetime.today().strftime("%Y-%m-%d_%H_%M")}.png'.replace("/", "_").replace("::", "_").replace(".py", "")
driver.save_screenshot("./reports/screenshots/"+file_name)
extra.append(pytest_html.extras.image("/screenshots/"+file_name))
report.extra = extra
我确信问题出在图像的路径上,我尝试了很多 str 组合,os.path 和 pathlib,但没有任何效果。屏幕截图保存在预期的位置,我可以像打开任何其他图像一样打开它。它只是没有显示在报告上。
<div class="image"><img src="data:image/png;base64,screenshots\scr_tests_test_example_TestExample_test_fail_example_2022-01-18_16_26.png"/></div>
编辑:额外澄清。我曾尝试在其中使用绝对路径,但它一直在 HTML 文件中extra.append
给我一个错误。Cant Resolve File
我的绝对路径是(编辑了一些个人详细信息)C:\Users\c.Me\OneDrive - Me\Documents\GitHub\project\build\reports\screenshots\filename.png
我用'/'和'\'都试过了
还有我的文件结构
project
├───build
│ ├───reports
│ ├───screenshots
│ ├───filename.png
| ├───report.html
| ├───run.py # I am running the test suite from here
├───scr
| ├───settings.py
│ ├───tests
│ ├───confest.py
运行.py
if __name__ == "__main__":
os.system(f"pytest --no-header -v ../scr/tests/ --html=./reports/Test_Report_{today}.html --self-contained-html")
对于先知,今天可能会保佑我要得到Cannot Resolve Directory
错误,我的代码如下
file_name = f'{nodeid}_{datetime.today().strftime("%Y-%m-%d_%H_%M")}.png'.replace("/", "_").replace("::", "_").replace(".py", "")
img_path = os.path.join(REPORT_PATH, 'screenshots', file_name)
driver.save_screenshot(img_path)
extra.append(pytest_html.extras.image(img_path))
该变量REPORT_PATH
是从 settings.py 导入的(参见上面的目录树),并由
PROJ_PATH = Path(__file__).parent.parent
REPORT_PATH = PROJ_PATH.joinpath("build\reports")
如果我img_path.replace("\\", "/")
将错误更改为Cannot Resolve File