YAML (YAML Ain't Markup Language) is a human-readable data serialization language designed for simple data exchange between programming languages. Its clean syntax makes it ideal for configuraton files, data serialization, and API design. Many languages, including Python, Java, and Ruby, support YAML through libraries like PyYAML.
YAML Syntax Overview
- Use indentation (spaces, not tabs) to denote hierarchy.
- Key-value pairs use a colon (
:) followed by a space. - Lists use a dash (
-) with a space. - Comments start with
#. - Support for anchors and references.
Common Data Types
Strings
name: "John"
address: 长沙
Strings can be quoted or unquoted.
Numbers
age: 30
Booleans
isStudent: true
isTeacher: false
Lists
fruits:
- apple
- banana
- orange
Dictionaries
person:
name: "John"
age: 30
Null
status: null
Indentation consistency is critical; use either 2 or 4 spaces through out the file.
Python and YAML
Install the PyYAML library to work with YAML in Python:
pip install pyyaml
Reading YAML Files
Sample d.yaml:
name: "John"
addr: 长沙
age: 18
status: null
isStudent: true
fruits:
- apple
- banana
- orange
teacher:
name: "Alex"
age: 30
Python code to read the file:
import yaml
with open('d.yaml', 'r', encoding='utf-8') as f:
data = yaml.safe_load(f)
print(data)
The output converts YAML data into native Python types.
Writing YAML Files
import yaml
info = {
"Person": {
"name": "John",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
}
}
}
with open('example.yaml', 'w') as f:
yaml.dump(info, f)
YAML in Keyword-Driven Testing (KDT)
KDT uses YAML to define test steps, separating test data from test logic.
YAML Test Data
# Open page
- action: goto
params:
url: 'https://www.baidu.com'
# Input search term
- action: sendkeys
params:
locator: ['id', 'kw']
value: 'python'
# Click search button
- action: click
params:
locator: ['id', 'su']
# Assert result
- action: assert_text_contains
params:
locator: ['id', 'content_left']
excepted: 'python'
Base Page Class
from selenium.webdriver import Chrome, ActionChains
class BasePage:
def __init__(self, driver: Chrome):
self.driver = driver
def goto(self, url):
self.driver.get(url)
def click(self, locator):
element = self.driver.find_element(*locator)
try:
element.click()
except:
ActionChains(self.driver).click(element).perform()
def sendkeys(self, value, locator=None):
if locator:
element = self.driver.find_element(*locator)
element.send_keys(value)
else:
ActionChains(self.driver).send_keys(value).perform()
def assert_text_contains(self, locator, expected):
element = self.driver.find_element(*locator)
assert expected in element.text
Test Case
import time
import yaml
from selenium import webdriver
from basepage import BasePage
# Load test steps from YAML
with open('test_keyword.yaml', 'r', encoding='utf-8') as f:
steps = yaml.safe_load(f)
def test_kdt():
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
page = BasePage(driver)
for step in steps:
action = step['action']
params = step['params']
method = getattr(page, action)
method(**params)
time.sleep(2)
driver.quit()
The YAML-driven approach makes test scripts more maintainable by keeping test data out side the code.