[Python] How to select only one Webdriver to run the test

Discussão em 'Python' iniciado por Stack, Julho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am learning selenium with Pytest and have configured conftest.py like this

    import pytest
    from selenium import webdriver
    from pages.homepage import HomePage

    def pytest_addoption(parser):
    parser.addoption('--platform', action='store', default='Chrome')

    options=webdriver.EdgeOptions()
    options.use_chromium=True
    Edge_driver = webdriver.Edge(options=options)
    Chrome_driver = webdriver.Chrome()

    @pytest.fixture
    def platform(request):

    plat = request.config.getoption('platform').lower()

    if plat not in ['MicrosoftEdge', 'Chrome']:
    raise ValueError('--platform value must be MicrosoftEdge or Chrome')
    return plat

    @pytest.fixture
    def driver():
    if platform == 'MicrosoftEdge':
    driver = Edge_driver
    else:
    driver = Chrome_driver

    yield driver
    driver._platform = platform
    driver.quit()


    when I type in the command line this

    pytest --platform=MicrosoftEdge mytest.py

    There are two browser opened, one is MicrosoftEdge the other is Chrome. Though the test only runs on the MicrosoftEdge, but why the Chrome is also opened without directing to any website in this case?

    How can I make sure there is only one browser opened with the driver defined in the command line?

    Continue reading...

Compartilhe esta Página