我正在嘗試使用 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
uj5u.com熱心網友回復:
我不完全確定它如何與 PyTest 一起作業,但是我們在 Java Extent Manager 上遇到了類似的問題。
在那里你必須傳遞影像檔案的絕對路徑,而不是相對路徑。
正如我在這里看到的,當前的作業目錄可以實作如下:
import pathlib
pathlib.Path().resolve()
所以,如果我理解正確,你應該改變你的代碼
extra.append(pytest_html.extras.image("/screenshots/" file_name))
到
working_root = pathlib.Path().resolve()
extra.append(pytest_html.extras.image(working_root "/screenshots/" file_name))
UPD
我認為您在reports這里缺少一個子檔案夾。
代替
working_root = pathlib.Path().resolve()
extra.append(pytest_html.extras.image(working_root "/screenshots/" file_name))
嘗試使用
working_root = pathlib.Path().resolve()
extra.append(pytest_html.extras.image(working_root "/reports/screenshots/" file_name))
uj5u.com熱心網友回復:
在這段痛苦的旅程中,我學到了很多。大多數情況下,我都知道我是個白癡。問題是我想制作一個自包含的 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
感謝先知對這次朝圣的指導。現在我必須休息了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/417987.html
標籤:
