Bird
0
0

Which of the following is the correct syntax to yield a WebDriver instance in a pytest fixture for setup and teardown?

easy📝 Syntax Q3 of 15
Selenium Python - Test Framework Integration (pytest)
Which of the following is the correct syntax to yield a WebDriver instance in a pytest fixture for setup and teardown?
Adef browser(): driver = webdriver.Chrome() driver.quit() yield driver
Bdef browser(): driver = webdriver.Chrome() return driver driver.quit()
Cdef browser(): driver = webdriver.Chrome() yield driver driver.quit()
Ddef browser(): yield webdriver.Chrome() driver.quit()
Step-by-Step Solution
Solution:
  1. Step 1: Understand yield usage in fixtures

    Yield pauses the fixture to provide the driver to tests, then resumes to run teardown.
  2. Step 2: Check correct order of setup, yield, and teardown

    Driver must be created before yield and quit after yield to close browser properly.
  3. Final Answer:

    def browser(): driver = webdriver.Chrome() yield driver driver.quit() -> Option C
  4. Quick Check:

    Yield driver between setup and teardown [OK]
Quick Trick: Yield driver after setup, quit after yield [OK]
Common Mistakes:
  • Calling driver.quit() before yield
  • Using return instead of yield
  • Yielding before driver creation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes