【发布时间】:2021-05-29 08:54:50
【问题描述】:
我试图让相同的测试在不同的浏览器中运行。每个浏览器都可以完美运行(["Chrome"] 或 ["Firefox"]),但是如果 supportedBrowsers 数组接收两个元素,则在第二次迭代中 yield 根本不会做任何事情,执行不会转到测试和夹具的其余部分都没有,浏览器站在那里打开。我错过了什么?
@pytest.fixture(scope='module')
def driver():
url = "http://localhost:1234/"
supportedBrowsers = ["Chrome", "Firefox"]
for x in supportedBrowsers:
if x == "Firefox":
option = webdriver.firefox.options.Options()
driverObj = webdriver.Firefox(executable_path=GeckoDriverManager().install())
elif x == "Chrome":
option = webdriver.chrome.options.Options()
driverObj = webdriver.Chrome(ChromeDriverManager().install())
option.headless = True
driverObj.implicitly_wait(10) # seconds
driverObj.get(url)
yield driverObj
driverObj.quit()
【问题讨论】:
-
你试过
@pytest.yield_fixture吗? (docs.pytest.org/en/reorganize-docs/yieldfixture.html)