ui自动化-加入yaml文件管理元素
2020-08-19 本文已影响0人
wenshuang_he
介绍:
采用python3语言规则,利用pytest框架管理用例,selenium框架定位元素并执行操作,yaml文件管理元素定位路径,allure模块美化测试报告。

1、新建文件夹element_yaml

2、文件夹下,新建yaml文件

3、文件中存放元素定位

4、把打开yaml文件的方法写到conftest.py文件里面

5、测试用例中使用conftest.py文件中的方法

附上代码:
- yaml文件内容
---
#用户名
yh: //input[@placeholder="请输入用户名"]
#密码
mm: //input[@placeholder="请输入密码"]
#获得验证码
yzm: //div[@class="verificationCode"]
#输入验证码
sryzm: //input[@placeholder="请输入验证码"]
#点击登录
dl: //button[text()="登录"]
- conftest.py文件内容
import yaml
import pytest
@pytest.fixture()
def zy():
with open(file="zz./element_yaml/aa.yaml", mode="r", encoding="utf-8")as fm:
w = yaml.load(fm, Loader=yaml.FullLoader)
return w
- test测试用例内容
@allure.feature("克隆菜单包")
class Test_cdb():
@allure.story('正常登录')
@allure.severity(allure.severity_level.BLOCKER) # 优先级
@allure.description('用户名和密码正确,正常登录')
@allure.testcase("https://xxx.xxx.xxx/", "测试环境地址,请点击跳转")
def test_login(self,zy):
# 打开登录界面
driver.get('https://txqa.ziyun-cloud.com/zyconsole/')
driver.maximize_window() # 最大化浏览器
driver.implicitly_wait(8) # 设置隐式时间等待
# 输入用户名
with allure.step('输入用户名:'):
dr.send_keys('输入用户名',zy["yh"], 'admin')
# 输入密码
with allure.step('输入密码:'):
dr.send_keys('输入密码', zy['mm'], '123456')
# 获取验证码
text = dr.get_text('获取四位验证码', zy['yzm'])
# 输入验证码
with allure.step('输入验证码:'):
dr.send_keys('输入验证码',zy['sryzm'], text)
# 点击登录
with allure.step('点击登录'):
dr.click('点击登录按钮', zy['dl'])
# 异常处理
try:
text = driver.find_element_by_xpath("//span[text()='首页']").text
assert text in driver.page_source
except Exception as e:
dr.screenshot('获取异常截图', '异常截图')
log.error('登录功能报错信息' + str(e))
allure.attach("{0}".format(e), "登录功能报错日志")