簡體   English   中英

如何使用 pytest selenium 截取測試錯誤的屏幕截圖

[英]How to take screenshot on test error using pytest selenium

我有 pytest 掛鈎,可以在測試失敗時截屏。 對於狀態為失敗的測試,selenium 會截取屏幕截圖。 但是,當在 pytest 夾具內測試失敗時,測試狀態為error ,並且 selenium 不會截圖。 夾具測試失敗時如何截取屏幕截圖?

這是一個 pytest 掛鈎,可在測試失敗時截取屏幕截圖。

def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()

    if report.when == "call":
        xfail = hasattr(report, "wasxfail")
        if (report.skipped and xfail) or (report.failed and not xfail):
            time.sleep(1)
            DRIVER.save_screenshot(screenshot_file)

目前您只檢查報告是否在call階段生成,即測試 function。

包括setup階段,即在測試之前執行的任何夾具,應該可以解決問題——

def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()

    if report.when in ("setup", "call"):
        xfail = hasattr(report, "wasxfail")
        if (report.skipped and xfail) or (report.failed and not xfail):
            time.sleep(1)
            DRIVER.save_screenshot(screenshot_file)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM