Creating Fixtures for Selenium Global configuration

Posted by Omar Ortega on Friday, January 17, 2025

Fixtures in pytest are functions that define a context for a test, they are super helpful in adding context for a test, in our case defining context for tests that are using the Selenium Webdriver. This is super powerful for designing complex tests. Those functions are accessible through arguments in the test functions.

Let’s see how we can implement those fixture functions in our framework. First we need to create a new file conftest.py, next we need to import the pytest library, and add the pytest decorator:

from pytest import fixture

@fixture(scope='function')

This decorator contains a scope parameters, this is a component that will help us to manage or to reuse the fixtures across the tests, the scopes we can select are: function, class, module, package and session.

For now I will be focusing on two of them; function and session, but what is it about?, well… it is super important for us, because it will determine if we want to have a separate browser session for every function or session, meaning that if we setup the scope as function, we are going to have a browser session per function, that is going to fulfill the need to have tests functions that are not depending on any other test. Ok… with all this information lets add our chrome_browser function as fixture.

from pytest import fixture
from selenium import webdriver

@fixture(scope='function')
def chrome_browser():
    browser = webdriver.Chrome()
    yield browser

Now it is time to import the webdriver class library from selenium and to instantiate a ChromeDriver, the function will return the browser variable, which is an instance of the chromedriver, but what about the yield keyword?, well.. that is also one of the nice pytest features, the teardown process on closing the driver and the browser will be automatically triggered within pytest, but do not get confused, it is not going to be triggered once the driver is returned, the process is going to be triggered after all the commands in the test functions are done and there is nothing else to do than to close the browser and close the session, isn’t that nice? 😀.

It is time to implement the fixture in the test case, open the test file and remove all the imports, and add the fixture method signature as an argument to your test function like this:

def test_home_page_loaded(chrome_browser):
    chrome_browser.get("https://the-internet.herokuapp.com/")
    title = chrome_browser.title
    print(title)
    assert "The Internet" in title

The chrome_browser variable is now the instance of your chromedriver, you can now call all the selenium actions/functions that you normally do with selenium 😎.

Now we can create as many test cases as we want and have a separate browser for each one of them!, without creating chromedriver objects, that is great!

But, how can we run our tests?…. the same way as you normally do with pytest:

pytest -verbose

Pytest will locate the confest.py file and run the fixture for the chromedriver, next it will locate the test and that is it!, the configuration process has been improved 🤓.

I will be updating/maintaing the Automation Framework on Github, here is the link Pytest Selenium GH Project