使用 pytest 为 selenium python 自动化测试生成 html 和 json 报告

generate html and json reports for selenium python automation test using pytest

我有一个 selenium python 自动化测试,它工作正常,现在我想生成 Html 和 JSON 报告,并使用 pytest 在报告中截屏。我是自动化新手 python 所以我不太了解它是如何完成的。

以下是我的代码

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:
        driver.get(url)
    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 test_screenshot.py --url "https://www.netflix.com/in/"

测试用例通过。如何生成 HTML 和 JSON 报告? 我试过这个

pytest -v -s --json-report --json-report-indent=4 --json-report-file=report/report.json --html=report/report.html test_screenshot.py

但是遇到这个错误

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...] pytest: error: unrecognized arguments: --json-report --json-report-indent=4 --json-report-file=report/report.json inifile: None

您需要安装这两个库:https://pypi.org/project/pytest-json-report/ & https://pypi.org/project/pytest-html/