我有一個 selenium python 自動化測驗,它計算回應時間并將其添加到 JSON 報告中。問題是我在打開 URL 之前和停止測驗之后計算回應時間的代碼。我想計算加載 URL 頁面所需的時間。
以下是我的代碼
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)
yield {"driver":driver, "url": pytestconfig.getoption("url")}
@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('Asia/Kolkata').localize(datetime.fromtimestamp(call.start))
stop_iso_dt = timezone('Asia/Kolkata').localize(datetime.fromtimestamp(call.stop))
response_time = (stop_iso_dt - start_iso_dt).total_seconds()
return {'response_time': str(response_time)}
我很困惑如何計算 conftest.py 中的加載時間。
uj5u.com熱心網友回復:
您需要做的是通過response_time夾具將變數的值傳遞給 pytest,以便它可以用于pytest_json_modifyreport鉤子。
由于我想將多個計時器添加到單個測驗用例中,因此我的解決方案是創建一個提供工廠的新夾具,就像這樣(盡管我在工廠中使用了自定義物件,但我猜測/希望字典像這樣會起作用):
@fixture(scope='session')
def timings(metadata):
"""
used to track stopwatch timings taken during a session
"""
stopwatches = []
def factory(stopwatch_name, response_time):
fsw = {'name': stopwatch_name, 'response_time': response_time}
stopwatches.append(fsw)
return fsw
yield factory
# add our stopwatches to the session's json_report metadata so that we can report it out
metadata['stopwatches'] = stopwatches
您的測驗類使用夾具,并且測驗用例獲得一個實體。就像是:
@mark.usefixtures('timings')
class TestFuzzyBear:
...
response_time = int(now_time) - int(before_time)
first_url_response_time = timings('first_resp_time', response_time)
現在您的所有回應時間都在 json_report 元資料的環境屬性中的字典串列中。從 pytest_json_modifyreport 鉤子中:
@hookimpl(optionalhook=True)
def pytest_json_modifyreport(json_report):
...
stopwatches = json_report['environment']['stopwatches']
現在你可以用這些資訊做你需要做的事情了。
[請注意,我已在此處粘貼并處理了資訊,因此它可能無法按書面說明作業,但它應該為您提供遵循的方向。]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/434687.html
下一篇:瀏覽器多標簽會話問題
