When we are automating Web applications, it is important to have a good configuration process that helps us on selecting the web browser we want to use for testing.
I like the idea of selecting program options from the command line, like the ones we have for several tools we already use. When we execute our pytest tests and we want to have more information:
pytest -verbose
The -verbose option is passed to the pytest program as a command line option that is configuring pytest to provide a more verbose output, we can implement the same within our framework and have a way to select a web browser from our own list.
The config file
The config python file is the one that is going to host the algorithm to know if the browser option was passed through the command line and if the browser is correct.
The first step is to create a config.py file and to host it for now in the same project’s root directory.
The config file will have all this code inside, do not worry I will explain it 😅.
class Config:
def __init__(self, browser):
SUPPORTED_BROWSERS = ['chrome', 'firefox']
print(browser)
if browser == None or browser.lower() not in SUPPORTED_BROWSERS:
raise Exception(f'{browser} please select a browser with the option --browser (supported browsers: {SUPPORTED_BROWSERS})')
self.browser_type = {
'chrome': 'chrome_browser',
'firefox': 'firefox_browser'
}[browser]
- A class Config: this is going to help us to define a function that is being called on run time, the function will have a set list of supported browsers and the logic to know if the option we are passing is a valid one, if the option is not in the SUPPORTED_BROWSER list, then an exception is raised telling us that we need to select a valid option.
SUPPORTED_BROWSERS = ['chrome', 'firefox']
if browser == None or browser.lower() not in SUPPORTED_BROWSERS:
raise Exception(f'{browser} please select a browser with the option --browser (supported browsers: {SUPPORTED_BROWSERS})')
- We can now have a dictionary with the parameters, in our case two parameters, one for chrome, and the other for firefox, the nice thing about this, it is that we can return the browser type.
Browser’s Option
Pytest contains a parser function that can be called on run time to setup options from command line, we can call it as argument:
In the conftest.py, we create a function for adding the browser’s option:
def pytest_addoption(parser):
parser.addoption(
"--browser",
action="store",
help="browser to run the test against"
)
The addoption function will set up the –browser option and the action to be stored, we can even create a help message for the user to know what the option is for.
We can add the new fixture on the browser_type function which is the one getting the option on run time.
@fixture(scope="function")
def browser_type(request):
return request.config.getoption("--browser")
The final part!, the browser function will tie it all together:
@fixture(scope='function')
def browser(browser_type):
cfg = Config(browser_type).browser_type
match cfg:
case "chrome_browser":
return webdriver.Chrome()
case "firefox_browser":
return webdriver.Firefox()
This function will get the option from the Config class and after the parameter is returned, it will instantiate the webdriver as Chrome or Firefox, it will be used automatically in our tests.
Isn’t that cool??!! 🕶️
We can now put it all to work, by running the pytest command with our own –browser option:
pytest --browser chrome
We can select firefox:
pytest --browser Firefox
The framework is now improved to run on different web browsers!
I will be updating/maintaing the Automation Framework on Github, here is the link Pytest Selenium GH Project