Bird
0
0

You want to run tests on multiple browsers using fixtures. How can you modify the fixture to accept a browser name parameter and setup the correct WebDriver?

hard📝 Application Q8 of 15
Selenium Python - Test Framework Integration (pytest)
You want to run tests on multiple browsers using fixtures. How can you modify the fixture to accept a browser name parameter and setup the correct WebDriver?
A@pytest.fixture(params=["chrome", "firefox"]) def browser(request): if request.param == "chrome": driver = webdriver.Chrome() else: driver = webdriver.Firefox() yield driver driver.quit()
B@pytest.fixture def browser(browser_name): if browser_name == "chrome": driver = webdriver.Chrome() else: driver = webdriver.Firefox() yield driver driver.quit()
C@pytest.fixture(params=["chrome", "firefox"]) def browser(): driver = webdriver.Chrome() if params == "chrome" else webdriver.Firefox() yield driver driver.quit()
D@pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit()
Step-by-Step Solution
Solution:
  1. Step 1: Use pytest parametrize feature in fixture

    Using @pytest.fixture(params=[...]) allows running tests with different parameters automatically.
  2. Step 2: Access parameter via request.param

    The fixture function accepts a request argument to get the current parameter value.
  3. Step 3: Setup driver based on parameter and yield

    Driver is created conditionally, yielded to test, then quit after test.
  4. Final Answer:

    Fixture with params and request.param to select browser. -> Option A
  5. Quick Check:

    Use params and request.param for multiple browsers [OK]
Quick Trick: Use @pytest.fixture(params=...) and request.param [OK]
Common Mistakes:
  • Not using request.param to access parameter
  • Trying to pass parameter directly without params
  • Ignoring yield and quit order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes