Skip to content

Provide a driver_factory - fixtureΒ #151

Open
@larsrinn

Description

@larsrinn

Having a driver_factory fixture additionally to the driver factory would be very useful for me (similarly to the pattern of tmpdir and tmpdir_factory):

  • Sometimes I need two seperate browser windows within one test. I couldn't find a way how to do that with the driver fixture
  • For some of our tests it would be fine, if the browser window is not closed and reopened between running them, in order to save time. However, this requires the fixture to be of a scope of session, module or class

I hacked a modification of the driver fixture in my conftest.py. Do you think this would be reasonable to include into pytest-selenium. If so, I'm happy to create pull request.

@pytest.fixture
def driver_factory(request, driver_class, driver_kwargs):
    """
    Returns a factory function that returns a WebDriver instance when called,
    based on options and capabilities
    """
    request.node.drivers = []

    def factory():
        driver = driver_class(**driver_kwargs)
        request.node.drivers.append(driver)

        event_listener = request.config.getoption('event_listener')
        if event_listener is not None:
            # Import the specified event listener and wrap the driver instance
            mod_name, class_name = event_listener.rsplit('.', 1)
            mod = __import__(mod_name, fromlist=[class_name])
            event_listener = getattr(mod, class_name)
            if not isinstance(driver, EventFiringWebDriver):
                driver = EventFiringWebDriver(driver, event_listener())

        return driver

    yield factory

    for driver in request.node.drivers:
        driver.quit()


@pytest.fixture
def driver(driver_factory):
    return driver_factory()

Usage:

def test_multiple_windows(driver_factory):
    driver1 = driver_factory()
    driver2 = driver_factory()
    # further test content

The possibility to use the driver_factory fixture in class-based-fixtures is not given yet, because the driver_class and driver-kwargs fixtures are function-scoped. But I don't see a reason why this couldn't be changed (which would also result in (slightly) faster running tests)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions