Introduction
As automation frameworks grow, they need an efficient way to store configuration settings, environment details, test data, and execution parameters. While JSON is widely used for structured data, another popular format is YAML (YAML Ain’t Markup Language).
YAML is a human-readable data serialization format that uses indentation instead of braces and commas, making it cleaner and easier to read than JSON. Because of its simple syntax, YAML is commonly used for configuration files, test data, CI/CD pipelines, and automation frameworks.
Unlike JSON, Python does not include built-in support for YAML. To work with YAML files, we use the PyYAML library.
In Selenium automation, YAML files are frequently used to store environment configurations, browser settings, URLs, user credentials, test scenarios, and reusable test data.
In this tutorial, you’ll learn what YAML files are, why they are used in Selenium automation, how to read YAML data using Python, and how to use that data in Data-Driven Testing.
What is a YAML File?
A YAML (YAML Ain't Markup Language) file is a human-readable text file used to store structured data using key-value pairs and indentation.
Unlike JSON, YAML does not require braces {}, brackets [], or quotation marks for simple values.
Example YAML file:
message: YAML Message
browser: chrome
timeout: 10
The data can then be read by Python and used inside Selenium automation scripts.
Why Use YAML Files?
YAML files are widely used in automation frameworks because they offer several advantages:
Easy for humans to read and write.
Cleaner syntax than JSON.
Excellent for configuration files.
Supports nested data structures.
Commonly used in CI/CD pipelines.
Keeps test data separate from test scripts.
Easy to maintain.
Improves framework flexibility.
Installing PyYAML
Since Python does not include YAML support by default, install the PyYAML package using:
pip install pyyaml
After installation, it can be imported into Python scripts.
Example
import yaml
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_yaml_files(tmp_path):
yaml_path = tmp_path / "message.yaml"
yaml_path.write_text(
"message: YAML Message\n",
encoding="utf-8"
)
data = yaml.safe_load(
yaml_path.read_text(encoding="utf-8")
)
driver = webdriver.Chrome()
try:
driver.get("https://www.testmuai.com/selenium-playground/simple-form-demo")
driver.find_element(By.ID, "user-message").send_keys(data["message"])
driver.find_element(By.ID, "showInput").click()
assert driver.find_element(By.ID, "message").text == data["message"]
finally:
driver.quit()
Understanding the Code
Import Required Modules
import yaml
from selenium import webdriver
from selenium.webdriver.common.by import By
The required modules are imported.
yamlis provided by the PyYAML library and is used to read and write YAML files.webdriverlaunches the browser.Byis used to locate web elements.
Create the YAML File
yaml_path = tmp_path / "message.yaml"
A temporary YAML file named message.yaml is created.
Note: In real-world automation frameworks, YAML files are usually created beforehand and stored inside folders such as
config,resources, ortestdata. The automation script simply reads the existing file.
Write Data into the YAML File
yaml_path.write_text(
"message: YAML Message\n",
encoding="utf-8"
)
This creates the following YAML file:
message: YAML Message
Here,
messageis the key.YAML Messageis the value.
Read the YAML File
data = yaml.safe_load(
yaml_path.read_text(
encoding="utf-8"
)
)
safe_load() reads the YAML file and converts it into a Python dictionary.
The variable data now contains:
{
"message": "YAML Message"
}
safe_load() is preferred because it safely parses YAML content without executing arbitrary Python objects.
Retrieve the Required Value
data["message"]
The value associated with the key "message" is retrieved.
The retrieved value is:
YAML Message
Launch the Browser
driver = webdriver.Chrome()
A new Chrome browser instance is created.
Open the Application
driver.get(
"https://www.testmuai.com/selenium-playground/simple-form-demo"
)
The browser opens the Selenium Playground Simple Form Demo page.
Enter the YAML Data
driver.find_element(
By.ID,
"user-message"
).send_keys(
data["message"]
)
The value read from the YAML file is entered into the message textbox.
Instead of hard-coding the input value, Selenium retrieves it from the YAML configuration.
Click the Button
driver.find_element(
By.ID,
"showInput"
).click()
The Show Message button is clicked.
The application displays the entered message.
Verify the Result
assert driver.find_element(
By.ID,
"message"
).text == data["message"]
The displayed message is compared with the value read from the YAML file.
If both values match, the test passes successfully.
Close the Browser
finally:
driver.quit()
The browser is closed after the test execution.
Practical Example
Suppose an e-commerce website supports multiple environments such as Development, QA, Staging, and Production.
Instead of changing URLs inside the Selenium scripts, all environment details are stored in a YAML configuration file. Before execution, the automation framework reads the appropriate environment settings from the YAML file and launches the correct application.
Automation Testing Example
Consider an online banking application.
The automation framework stores browser name, application URL, username, password, timeout values, and environment details in a YAML configuration file. During execution, Selenium reads these settings automatically, allowing the same framework to run in different environments without changing the code.
Real-World Example
YAML files are widely used in:
Selenium Automation Frameworks
REST API Testing
CI/CD Pipelines
Jenkins Configuration
GitHub Actions
Docker Compose
Kubernetes Configuration
Enterprise Automation Frameworks
Typical YAML data includes browser settings, URLs, user credentials, execution environments, timeout values, API configurations, and deployment settings.
Advantages of Using YAML Files
Very easy to read and maintain.
Cleaner syntax than JSON.
Excellent for configuration management.
Supports nested objects and lists.
Reduces unnecessary punctuation.
Widely used in DevOps and CI/CD tools.
Separates configuration from automation code.
Improves framework maintainability.
Common Mistakes Beginners Make
Incorrect Indentation
YAML relies on indentation.
Using inconsistent spaces can cause parsing errors.
Using Tabs Instead of Spaces
YAML requires spaces for indentation.
Avoid using the Tab key.
Forgetting to Install PyYAML
Python cannot read YAML files unless the PyYAML library is installed.
Using yaml.load() Instead of yaml.safe_load()
Always use safe_load() unless you specifically need advanced YAML features.
It is safer and recommended for automation projects.
Best Practices
Use YAML for configuration files.
Keep indentation consistent.
Use meaningful key names.
Organize YAML files inside a
configortestdatafolder.Store reusable framework settings in YAML.
Use
safe_load()to read YAML files securely.Validate that required keys exist before accessing them.
Conclusion
YAML Files provide a clean and readable way to store configuration settings and structured test data in Selenium automation frameworks. Using the PyYAML library, automation scripts can easily read YAML files and use their contents during execution. Because of their simplicity and flexibility, YAML files are widely used in Selenium frameworks, CI/CD pipelines, DevOps tools, and enterprise automation projects.
Frequently Asked Questions (FAQs)
What is a YAML file?
A YAML file is a human-readable text file used to store structured data using key-value pairs and indentation.
Why are YAML files used in Selenium automation?
YAML files are mainly used to store configuration settings, environment information, browser details, and reusable test data.
Which Python library is used to read YAML files?
The PyYAML library is commonly used to read and write YAML files in Python.
Why is yaml.safe_load() preferred?
safe_load() safely parses YAML content without executing arbitrary Python objects, making it more secure for automation frameworks.
Are YAML files commonly used in professional automation frameworks?
Yes.
YAML files are widely used for configuration management, Selenium frameworks, CI/CD pipelines, Docker, Kubernetes, and DevOps automation.
Key Takeaways
YAML Filesstore structured data using indentation and key-value pairs.Python uses the PyYAML library to read and write YAML files.
yaml.safe_load()is the recommended method for reading YAML files.YAML is commonly used for configuration management and automation settings.
YAML files improve readability, maintainability, and framework flexibility.
