How to run pytest-selenium with xdist and use cli arg driver?

328 Views Asked by At

I am trying to run multiple tests in parallel using xdist and determine the webdriver based on the user's command line argument such as --driver Remote/Chrome/etc

Ultimately, I am trying throw this into docker so that it can be used in a gitlab pipeline.

Here is what I've tried..

conftest.py

@pytest.fixture(scope="session")
def setup(request, selenium):
    # Get cli specified driver
    driver = selenium
    driver.get(os.environ.get('server'))

    # Collection
    session = request.node
    for item in session.items:
        cls = item.getparent(pytest.Class)
        setattr(cls.obj, "driver", driver)

    # Teardown when tests finish
    yield driver
    driver.close()

test_login.py

@pytest.mark.usefixtures("setup")
class Test:
    def test_loginValid(self, selenium):
        util.loginEnv(selenium)

Which gives me the error..

========================================================================= ERRORS =========================================================================
_________________________________________________________ ERROR at setup of Test.test_loginValid _________________________________________________________ ScopeMismatch: You tried to access the 'function' scoped fixture 'selenium' with a 'session' scoped request object, involved factories
tests\conftest.py:37:  def setup(request, selenium)
..\appdata\local\programs\python\python37-32\lib\site-packages\pytest_selenium\pytest_selenium.py:205:  def selenium(driver)

If I attempt to remove the scope from the setup fixture, I get the following error from conftest.py

# Collection
        session = request.node
>       for item in session.items:
E       AttributeError: 'Function' object has no attribute 'items'

tests\conftest.py:59: AttributeError

-------------------------------------------------------------------- pytest-selenium --------------------------------------------
Driver log: ...\pytest-426\test_loginValid0\driver.log
URL: *** CENSORED ***
WARNING: Failed to gather log types: Message: unknown command: Cannot call non W3C standard command while in W3C mode

Doing this method has worked previously without the xdist collection. How can I make this work if it is possible and what am I doing wrong?

0

There are 0 best solutions below