YAML的语法特点包括:
YAML(YAML Ain't Markup Language)支持多种数据类型,包括字符串、数字、布尔值、列表、字典和空值。下面是每种数据类型的示例代码和相应的语法规范:
name: "John"
addr: "长沙"
age: 30
isStudent: true
isTeacher: false
fruits:
- apple
- banana
- orange
person:
name: "John"
age: 30
status: null
name: "John"
addr: "长沙"
age: 18
status: null
isStudent: true
fruits:
- apple
- banana
- orange
teacher:
name: "Alex"
age: 30
import yaml
with open(file="d.yaml", mode="r",encoding='utf-8') as f:
res = yaml.safe_load(f)
print(res)
import yaml
data = {
"Person": {
"name": "John",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
}
}
}
# 将data变量存储的数据写入YAML文件
with open(file="example.yaml", mode="w") as f:
yaml.dump(data, f)
# 访问页面
- action: goto
params:
url: 'https://www.baidu.com'
# 输入python
- action: sendkeys
params:
locator: ['id','kw']
value: 'python'
# 点击搜索按钮
- action: click
params:
locator: ['id','su']
# 断言
- action: assert_text_contains
params:
locator: ['id','content_left']
excepted: 'python'
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):
'''点击操作'''
el = self.driver.find_element(*locator)
try:
el.click()
except:
ActionChains(self.driver).click(el).perform()
def sendkeys(self,value,locator=None):
'''发送文本操作'''
if locator:
# 相当于ActionChains中的send_keys_to_element(ele,value),先做点击,再做文本输入
el = self.driver.find_element(*locator)
el.send_keys(value)
else:
ActionChains(self.driver).send_keys(value).perform()
def assert_text_contains(self,locator,excepted):
'''断言文本是否包含指定的内容'''
el = self.driver.find_element(*locator)
assert excepted in el.text
import time
import yaml
from selenium import webdriver
from basepage import BasePage
# 1、读取yaml文件中的数据
with open(file='test_keyword.yaml',mode='r',encoding='utf-8') as f:
data = yaml.safe_load(f)
# 2、测试用例编写
def test_01():
# 初始化浏览器操作
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
# 实例化BasePage的对象
basepage = BasePage(driver)
# 遍历读取到的data数据
for step in data:
# 获取动作名称
method_name = step['action']
# 获取参数
params = step['params']
# 获取类中方法名
method = getattr(basepage, method_name)
# 调用方法,输入参数
method(**params) # 字典解包
time.sleep(2)
driver.quit()