yaml可以存储各种数据:对象、数字、纯量:
data:(这是对象)
user:‘这是名字’
paswword:‘密码’
数组:- A
纯量:
int:
-123
# 导入库 import yaml from selenium import webdriver import time # 打开yaml文件 file = open('data.yaml', encoding='utf-8') # 加载yaml中的数据 b = yaml.load(file, Loader=yaml.FullLoader) # 初始化浏览器 driver = webdriver.Chrome() # 打开网页 url = 'https://cnooc.bccastle.com/admin/#/login' driver.get(url) # 执行操作 for i in range(0, 3): # print(b[i]['data']['username']) # print(b[i]['data']['password']) # print(b[i]['data']['code']) # print(b[i]['check']) # 提取yaml数据并输入 code = b[i]['data']['code'] # 清理文本框的内容 driver.find_element_by_xpath('//*[@id="app"]/section/main/div/div/div[2]/div[1]/input').clear() # 输入用户名 driver.find_element_by_xpath('//*[@id="app"]/section/main/div/div/div[2]/div[1]/input').send_keys(b[i]['data']['username']) #输入账号 # 清理密码文本框内容 driver.find_element_by_xpath('//*[@id="app"]/section/main/div/div/div[2]/div[2]/input').clear() # 输入密码 driver.find_element_by_xpath('//*[@id="app"]/section/main/div/div/div[2]/div[2]/input').send_keys(b[i]['data']['password']) #输入密码 # 点击登陆 driver.find_element_by_xpath('//*[@id="app"]/section/main/div/div/div[3]/button').click() # 点击登陆 # 检查点,根据code值0判断应该走的检查数据 time.sleep(3) if code == 0: result = driver.find_element_by_class_name('el-message__content').text result = result.find('无效的登录凭证') elif code == 1: result = driver.find_element_by_xpath('//*[@id="app"]/section/main/div/div/div[2]/div[3]/div/span').text result = result.find('用户名/手机号/邮箱不能为空') else: result = driver.find_element_by_xpath('//*[@id="app"]/div/section/section/section/main/div/div/div[1]').text result = result.find('系统首页') # 给出测试结果 if result == b[i]['check']: print(b[i]['datail']) print('测试通过') else: print(b[i]['datail']) print('测试失败') driver.quit() # 关闭浏览器
这是yaml中的数据
- datail: 用户名为空 data: username: '' password: P@ssw0rd1234 code: 1 check: -0 - datail: 用户名错误 data: username: admin1 password: P@ssw0rd1234 code: 0 check: -0 - datail: 登陆成功 data: username: admin password: P@ssw0rd1234 code: 2 check: -0