Sobes.tech
Senior

Where should you close the browser when you take it out into a fixture?

sobes.tech AI

Answer from AI

If the browser is initialized in a fixture (for example, in setup), it should be closed in the corresponding part of the fixture that is called after the tests — usually teardown or fixture finalizer. This guarantees that the browser will be closed correctly after all tests using this fixture have run, and it will not remain open, which could lead to resource leaks.

Example in Python with pytest:

import pytest
from selenium import webdriver

@pytest.fixture(scope="session")
def browser():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()  # Close the browser after all tests

Thus, the browser is created once per session and closed after all tests using this fixture are completed.