Introduction
Automation frameworks often need information that changes depending on where the tests are executed. For example, the application URL, browser type, username, password, timeout values, and execution environment may vary between Development, QA, Staging, and Production environments.
Instead of hard-coding these values inside the test scripts, they are stored in Configuration Files. During execution, the automation framework reads the required settings from the configuration file, making the framework more flexible and easier to maintain.
Configuration files separate framework settings from test logic, allowing the same automation scripts to run in different environments simply by changing the configuration values.
In Python Selenium frameworks, configuration data is commonly stored in files such as INI, JSON, YAML, or Properties files. This topic demonstrates using an INI configuration file.
What are Configuration Files?
Configuration Files are external files that store application and framework settings required during test execution.
Instead of modifying the automation code, testers can update configuration values in one place.
Typical configuration data includes:
Application URL
Browser Name
Username
Password
Environment Name
Timeout Values
API Endpoints
Database Configuration
Example (config.ini):
[DEFAULT]
base_url=https://www.testmuai.com/selenium-playground/
browser=chrome
timeout=10
The automation framework reads these values whenever the tests are executed.
Why Use Configuration Files?
Configuration files provide several advantages:
Separate configuration from test scripts.
Avoid hard-coded values.
Easily switch between environments.
Improve framework maintainability.
Reduce code duplication.
Make updates easier.
Support reusable automation frameworks.
Improve collaboration among team members.
Common Types of Configuration Files
Some commonly used configuration file formats are:
INI Files (
config.ini)JSON Files (
config.json)YAML Files (
config.yaml)Properties Files (
config.properties)XML Configuration Files
Among these, INI, JSON, and YAML are the most commonly used in Python Selenium frameworks.
Example
from pathlib import Path
from selenium import webdriver
def test_configuration_files():
config_path = Path(__file__).with_name("config.ini")
config_path.write_text(
"[DEFAULT]\n"
"base_url=https://www.testmuai.com/selenium-playground/\n"
"browser=chrome\n",
encoding="utf-8",
)
values = {}
for line in config_path.read_text(
encoding="utf-8"
).splitlines():
if "=" in line:
key, value = line.split("=", 1)
values[key.strip()] = value.strip()
driver = webdriver.Chrome()
try:
driver.get(values["base_url"])
assert "selenium-playground" in driver.current_url
assert values["browser"] == "chrome"
finally:
driver.quit()
Understanding the Code
Import Required Modules
from pathlib import Path
from selenium import webdriver
The required modules are imported.
Pathis used to create and manage file paths.webdriveris used to launch and control the browser.
Create the Configuration File
config_path = Path(__file__).with_name("config.ini")
A configuration file named config.ini is created in the same directory as the Python file.
Note: In real-world Selenium frameworks, the configuration file is usually created once and stored inside a dedicated folder such as
config. During execution, the framework simply reads the existing configuration file instead of creating it every time.
Write Configuration Data
config_path.write_text(
"[DEFAULT]\n"
"base_url=https://www.testmuai.com/selenium-playground/\n"
"browser=chrome\n",
encoding="utf-8",
)
The following data is written into the configuration file:
[DEFAULT]
base_url=https://www.testmuai.com/selenium-playground/
browser=chrome
Here,
base_urlstores the application URL.browserspecifies the browser to be used.
Read the Configuration File
values = {}
for line in config_path.read_text(
encoding="utf-8"
).splitlines():
if "=" in line:
key, value = line.split("=", 1)
values[key.strip()] = value.strip()
The configuration file is read line by line.
Each key-value pair is extracted and stored inside a Python dictionary.
The dictionary becomes:
{
"base_url": "https://www.testmuai.com/selenium-playground/",
"browser": "chrome"
}
Launch the Browser
driver = webdriver.Chrome()
A new Chrome browser instance is created.
Note: This example directly creates a Chrome browser for simplicity. In a real automation framework, the browser value (
chrome,edge,firefox) would typically be read from the configuration file and passed to a Driver Factory to launch the appropriate browser.
Open the Application
driver.get(values["base_url"])
The application URL stored in the configuration file is opened.
Notice that the URL is not hard-coded inside the Selenium script.
Verify the URL
assert "selenium-playground" in driver.current_url
This verifies that the correct application has been opened successfully.
Verify the Browser Setting
assert values["browser"] == "chrome"
The browser value stored in the configuration file is verified.
This confirms that the configuration data has been read correctly.
Close the Browser
finally:
driver.quit()
The browser is closed after the test execution.
Practical Example
Suppose an e-commerce website has separate Development, QA, Staging, and Production environments.
Instead of modifying the Selenium scripts every time, each environment has its own configuration file containing the appropriate application URL and browser settings. The automation framework reads the required configuration file before execution.
Automation Testing Example
Consider an online banking application.
The framework stores the following information inside a configuration file:
Application URL
Browser Name
Default Timeout
Username
Password
Environment Name
When the tests start, the framework automatically loads these settings and uses them throughout the execution.
Real-World Example
Configuration files are widely used in:
Selenium Automation Frameworks
Banking Applications
E-commerce Websites
CRM Systems
ERP Applications
Healthcare Systems
Insurance Applications
Enterprise Automation Frameworks
Typical configuration values include application URLs, browser settings, execution environments, timeout values, API endpoints, database connections, and user credentials.
Advantages of Using Configuration Files
Eliminates hard-coded values.
Makes frameworks more flexible.
Simplifies environment switching.
Improves maintainability.
Reduces duplicate code.
Centralizes framework settings.
Supports reusable automation frameworks.
Makes configuration updates easier.
Common Mistakes Beginners Make
Hard-Coding Configuration Values
Avoid placing URLs, browser names, and credentials directly inside test scripts.
Storing Sensitive Information in Plain Text
Passwords and API keys should be encrypted or managed using secure secrets management tools whenever possible.
Using Multiple Configuration Files Unnecessarily
Maintain a well-organized configuration structure instead of scattering configuration values across multiple files.
Not Validating Configuration Values
Always verify that required configuration values exist before using them.
Best Practices
Store all framework settings in configuration files.
Keep configuration separate from test logic.
Use meaningful key names.
Organize configuration files inside a dedicated
configfolder.Use different configuration files for different environments.
Avoid storing sensitive information in plain text.
Validate configuration values before execution.
Conclusion
Configuration Files are an essential part of professional Selenium automation frameworks. They separate framework settings from test scripts, making automation more flexible, reusable, and maintainable. By storing application URLs, browser settings, timeout values, and environment details in external files, automation frameworks can easily support multiple environments without modifying the test code.
Frequently Asked Questions (FAQs)
What is a configuration file?
A configuration file is an external file that stores framework settings such as application URLs, browser names, timeout values, and environment information.
Why are configuration files used in Selenium automation?
Configuration files separate framework settings from automation code, making the framework easier to maintain and reuse.
Which file formats are commonly used for configuration files?
Common formats include INI, JSON, YAML, Properties, and XML files.
Can multiple environments use different configuration files?
Yes.
Professional automation frameworks often maintain separate configuration files for Development, QA, Staging, and Production environments.
Are configuration files used in real Selenium automation frameworks?
Yes.
Configuration files are a standard component of almost every professional Selenium automation framework.
Key Takeaways
Configuration Filesstore framework settings separately from test scripts.Common configuration formats include INI, JSON, YAML, Properties, and XML.
Configuration files improve framework flexibility and maintainability.
They simplify switching between different execution environments.
Professional Selenium automation frameworks rely heavily on configuration files for centralized settings management.
