我正在尝试建立一个 pytest-bdd 分裂测试自动化框架。
我的功能文件中有三个场景,.py 文件中有相关步骤定义。
在我的conftest.py 文件中,我尝试使用钩子进行场景设置和拆卸。
我想打开 chrome 并导航到场景设置中的 URL,然后在场景拆卸中关闭 chrome。所以每个场景都是从chrome没有打开的位置开始的。
对于第一个场景来说效果很好。 Chrome 打开,导航到 URL,功能文件中的场景运行并传递,然后 Chrome 关闭。
第二个和第三个场景失败:
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=57918): Max retries exceeded with url: /session/60a9e9ff53deb3f8a77fd1dbdfd1ba92/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001D67EF33090>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
这是conftest.py代码
"""conftest"""
import pytest
from splinter import Browser, Config
APP_URL = "https://webapp_url"
browser_config = Config(fullscreen=True)
browser = Browser('chrome', config=browser_config)
@pytest.hookimpl
def pytest_bdd_before_scenario(scenario):
"""scenario set up tasks"""
print("Before scenario: " + str(scenario.name))
browser.visit(APP_URL)
@pytest.hookimpl
def pytest_bdd_after_scenario(scenario):
"""scenario clean tasks"""
print("After scenario: " + str(scenario.name))
browser.quit()
据我了解, browser.quit() 行会关闭浏览器,但由于某种原因,浏览器不会打开场景 2 和 3。我认为 browser.quit() 可能正在结束会话,然后不会重新启动启动并尝试重新启动它,但没有成功,或者至少我的尝试没有成功。
我检查了一些 WinError 10061 帖子,它们主要是由 driver.quit() 出现两次引起的,但这里的情况并非如此。
我怀疑我对 python 和 pytest 的理解让我失望了。
我错过了什么?
经过更多的挖掘和尝试后,我不确定我想要做什么可以用钩子来完成。 我已经成功地使用夹具产生了所需的结果。
@pytest.fixture(名称=“浏览器”) deffixture_browser(): """浏览器固定装置""" browser_config = 配置(全屏=True) b = 浏览器('chrome', config=browser_config) b.访问(APP_URL) 产量b b.退出()