0

我有一个 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

但收到此错误

错误:用法:pytest [options] [file_or_dir] [file_or_dir] [...] pytest:错误:无法识别的参数:--json-report --json-report-indent=4 --json-report-file=report/报告.json inifile:无

4

1 回答 1

1

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

于 2022-02-08T07:13:02.893 回答