我有一個 selenium-python 自動化測驗,我正在使用 Pytest 生成 HTML/JSON 報告。我想在 HTML/JSON 報告中添加回應時間,這可能嗎?
以下是我的代碼
test_screenshot.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pytest_html
from selenium.common.exceptions import InvalidSessionIdException
def test_Openurl(setup):
driver = setup["driver"]
url = setup["url"]
try:
before_time = datetime.now().strftime('%H%M%S%f') # Timestamp
driver.get(url)
now_time = datetime.now().strftime('%H%M%S%f') # Timestamp
response_time = int(now_time) - int(before_time)
except Exception as e:
print(e.message)
assert driver.current_url == URL
driver.save_screenshot("ss.png")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.save_screenshot("ss1.png")
driver.close()
Conftest.py
import pytest
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
def pytest_addoption(parser):
parser.addoption("--url", action="store", default="https://google.com/")
@pytest.fixture()
def setup(pytestconfig):
s = Service("C:/Users/Yash/Downloads/chromedriver_win32/chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.maximize_window()
yield {"driver":driver, "url": pytestconfig.getoption("url")}
以下是我用來生成報告的命令
pytest -v -s --json-report --json-report-indent=4 --json-report-file=report/report.json --html=report/report.html test_screenshot.py
uj5u.com熱心網友回復:
您可以使用 JSON 報告的一些掛鉤來執行此操作。在您的 conftest.py 檔案中的這些內容:
@hookimpl(optionalhook=True)
def pytest_json_runtest_metadata(call):
"""
fixture from the pytest-json-report plugin that will add your info
"""
if call.when != 'call':
return {}
# collect the start and finish times in ISO format for the US/Eastern timezone
start_iso_dt = timezone('US/Eastern').localize(datetime.fromtimestamp(call.start)).isoformat()
stop_iso_dt = timezone('US/Eastern').localize(datetime.fromtimestamp(call.stop)).isoformat()
return {'start': start_iso_dt, 'stop': stop_iso_dt
這將出現在您的 json_report 元資料中。(我的代碼需要美國/東部時區,您顯然可以進行相應調整,或??者只是在此函式中進行差異計算,然后return {'response_time': mydiff}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/433129.html
