cdu*_*dub 4 python selenium screenshot pytest pytest-html
我正在尝试使用 pytest-html 和 selenium 生成一个独立的 html 报告。我一直在尝试将屏幕截图嵌入到报告中,但它们没有显示。\n
我的 conftest.py 看起来像这样
\n@pytest.fixture()\ndef chrome_driver_init(request, path_to_chrome):\n driver = webdriver.Chrome(options=opts, executable_path=path_to_chrome)\n request.cls.driver = driver\n page_object_init(request, driver)\n driver.get(URL)\n driver.maximize_window()\n yield driver\n driver.quit()\n\n\n# Hook that takes a screenshot of the web browser for failed tests and adds it to the HTML report\n@pytest.hookimpl(hookwrapper=True)\ndef pytest_runtest_makereport(item):\n pytest_html = item.config.pluginmanager.getplugin("html")\n outcome = yield\n report = outcome.get_result()\n extra = getattr(report, "extra", [])\n if report.when == "call":\n feature_request = item.funcargs[\'request\']\n driver = feature_request.getfixturevalue(\'chrome_driver_init\')\n nodeid = item.nodeid\n xfail = hasattr(report, "wasxfail")\n if (report.skipped and xfail) or (report.failed and not xfail):\n file_name = f\'{nodeid}_{datetime.today().strftime("%Y-%m-%d_%H_%M")}.png\'.replace("/", "_").replace("::", "_").replace(".py", "")\n driver.save_screenshot("./reports/screenshots/"+file_name)\n extra.append(pytest_html.extras.image("/screenshots/"+file_name))\n report.extra = extra\nRun Code Online (Sandbox Code Playgroud)\n我确信问题出在图像的路径上,并且我尝试了很多 str 组合、os.path 和 pathlib 但没有任何效果。屏幕截图已保存在预期位置,我可以像任何其他图像一样打开它。它只是没有显示在报告上。
\n<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>\nRun Code Online (Sandbox Code Playgroud)\n编辑:额外澄清。我尝试在 中使用绝对路径,但它一直在 HTML 文件中extra.append 给我一个错误。Cant Resolve File我的绝对路径是(编辑了一些个人详细信息)C:\\Users\\c.Me\\OneDrive - Me\\Documents\\GitHub\\project\\build\\reports\\screenshots\\filename.png我已经尝试过 \'/\' 和 \'\\\'
还有我的文件结构
\nproject\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80build\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80reports\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80screenshots\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80filename.png\n| \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80report.html\n| \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80run.py # I am running the test suite from here\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80scr\n| \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80settings.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80tests\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80confest.py\nRun Code Online (Sandbox Code Playgroud)\n运行.py
\nif __name__ == "__main__":\n os.system(f"pytest --no-header -v ../scr/tests/ --html=./reports/Test_Report_{today}.html --self-contained-html")\nRun Code Online (Sandbox Code Playgroud)\n对于先知,今天可能会保佑我\n为了得到错误,Cannot Resolve Directory我的代码如下
file_name = f\'{nodeid}_{datetime.today().strftime("%Y-%m-%d_%H_%M")}.png\'.replace("/", "_").replace("::", "_").replace(".py", "")\nimg_path = os.path.join(REPORT_PATH, \'screenshots\', file_name)\ndriver.save_screenshot(img_path)\nextra.append(pytest_html.extras.image(img_path))\nRun Code Online (Sandbox Code Playgroud)\n该变量REPORT_PATH是从 settings.py 导入的(参见上面的目录树),并由以下命令创建
PROJ_PATH = Path(__file__).parent.parent\nREPORT_PATH = PROJ_PATH.joinpath("build\\reports")\nRun Code Online (Sandbox Code Playgroud)\n另一个有趣的事实是,如果我将img_path.replace("\\\\", "/")错误更改为Cannot Resolve File
cdu*_*dub 10
在这段痛苦的旅程中我学到了很多东西。主要是我知道我是个白痴。问题是我想制作一个独立的 HTML。Pytest-html 无法按预期将图像添加到独立报告中。在此之前,您必须先将图像转换为其文本 base64 版本。所以我所有的欠债的答案就是一行代码。
@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", "")
img_path = os.path.join(REPORT_PATH, "screenshots", file_name)
driver.save_screenshot(img_path)
screenshot = driver.get_screenshot_as_base64() # the hero
extra.append(pytest_html.extras.image(screenshot, ''))
report.extra = extra
Run Code Online (Sandbox Code Playgroud)
感谢先知对这次朝圣的指导。现在我必须休息了。
| 归档时间: |
|
| 查看次数: |
3343 次 |
| 最近记录: |