使用会话范围 pytest fixture 更新 webdriver 以供 class scope fixture 使用

Use session scope pytest fixture to update webdriver for use by class scope fixture

Google 未能找到答案,所以我转向这里。

我正在使用 pytest 和 selenium 设置自动化 Web 测试套件。我想编写代码以在 运行 测试之前首先检查 chromewebdriver 的更新。我认为最好的方法是使用固定装置和 webdriver_manager 模块。

我从以下开始,它奏效了

@pytest.fixture()
def chrome_driver_init(request):
    driver = webdriver.Chrome(options=opts, executable_path=ChromeDriverManager(path=TEST_PATH).install())
    request.cls.driver = driver
    driver.get(URL)
    driver.maximize_window()
    yield
    driver.quit()

但是每次测试都必须检查任何 webdriver 更新确实会减慢速度。所以我尝试了这个,但无法弄清楚如何让 child 夹具使用 parent 夹具中的变量。我真的希望它只 运行 更新一次并且 child 从那时起使用变量而无需再次调用更新 parent。

#Checks for latest ChromeDriver version
@pytest.fixture(scope='session')
def update_chrome_driver():
    chrome_driver = webdriver.Chrome(options=opts, executable_path=ChromeDriverManager(path=TEST_PATH).install())
    return chrome_driver

#Initalizes chrome driver and opens testing window, runs at the beginning of each test
#Closes test window at end of test
@pytest.fixture(scope='class')
def chrome_driver_init(request, update_chrome_driver):
    driver = update_chrome_driver
    request.cls.driver = driver
    driver.get(URL)
    driver.maximize_window()
    yield
    driver.quit()

我已经为这两个装置尝试了不同的示波器组合,但最终测试设置失败了。 任何人都知道一个干净的方法来让它工作吗?

这里应该优化您的代码 - 使用在会话范围内安装 ChromeDriver,然后将路径传递给您的装置。 示例:

import pytest
from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager


@pytest.fixture(scope='session')
def path_to_chrome():
    return ChromeDriverManager().install()


@pytest.fixture
def chrome_driver_init(path_to_chrome):
    driver = webdriver.Chrome(executable_path=path_to_chrome)
    driver.get('https://google.com')
    driver.maximize_window()
    yield
    driver.quit()


def test_1(chrome_driver_init):
    pass


def test_2(chrome_driver_init):
    pass

然后你可以看到 webdriver_manager 只调用了一次:

pytest tests/test_some.py 
========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 3.7.2, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/user/SOTriageProject/tests, configfile: pytest.ini
plugins: env-0.6.2, cov-3.0.0
collected 2 items                                                                                                                                                                                        

tests/test_some.py::test_1 
--------------------------------------------------------------------------------------------- live log setup ---------------------------------------------------------------------------------------------
INFO     WDM:logger.py:24 

INFO     WDM:logger.py:24 ====== WebDriver manager ======
INFO     WDM:logger.py:24 Current google-chrome version is 94.0.4606
INFO     WDM:logger.py:24 Get LATEST driver version for 94.0.4606
INFO     WDM:logger.py:24 Driver [/Users/user/.wdm/drivers/chromedriver/mac64/94.0.4606.61/chromedriver] found in cache
PASSED                                                                                                                                                                                             [ 50%]
tests/test_some.py::test_2 PASSED                                                                                                                                                                  [100%]

=========================================================================================== 2 passed in 5.96s ============================================================================================

P.S。我没听懂第一条消息的内容

request.cls.driver = driver

是,所以我把它从例子中删掉了。