简体   繁体   中英

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

Google has failed me in a search for answers so I am turning here.

I am setting up an automated web testing suite using pytest and selenium. I would like to code to first check for updates to chromewebdriver before running the tests. I figured the best way to do this was using fixtures and the webdriver_manager module.

I started with the following and it worked

@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()

but having to check for any webdriver updates for every test is really slowing things down. So I attempted this but cant figure out how to get the child fixture to use the variable from the parent one. I would really like it to only run the update once and the child use the variable from then on without calling the update parent again.

#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()

I have tried different combos of scope for the two fixtures but eventually the test set up fails. Anyone know a clean way to get this to work?

Here is a thing that should optimize your code - use installing ChromeDriver in session scope, then pass the path to your fixtures. Example:

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

and then you can see that webdriver_manager calls only once:

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 ============================================================================================

PS i didn't get what the thing in the first message

request.cls.driver = driver

is, so i cutted it off from example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM