def test_web(selenium):
selenium.get("https://www.baidu.com")
assert 1 == 2
终端直接运行测试用例:
(.venv) PS C:\Users\batytao\PycharmProjects\PythonProject> pytest --driver Edge --html report.html (注:pytest 执行的文件名的前缀是test,则pytest 可以不跟文件名,如果前缀不是test,则需跟文件名,如: pytest selenium_pytest.py --driver Edge --html report.html )
提示错误:ERROR test_selenium_pytest.py::test_web - _pytest.config.exceptions.UsageError: --driver must be specified
解决:
这个错误表明在使用Selenium+Pytest进行Web自动化测试时,没有正确指定浏览器驱动(driver)参数。
实测验证ok:
from selenium import webdriver
from selenium.webdriver.edge.service import Service
def test_web():
# 指定驱动路径
service = Service(executable_path=r'C:\Program Files\edgedriver\MicrosoftWebDriver.exe')
# 添加detach选项防止浏览器闪退
options = webdriver.EdgeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Edge(options=options, service=service)
driver.get("https://www.baidu.com")
assert 1 == 1
driver.quit()