使用Playwright+Pytest+Yaml+Allure搭建UI自动化测试用例框架
本人在学习搭建playwright的UI自动化框架时,发现网上讲解的搭建过程不详细且极容易报错。便写下该篇文章,便于记录及后续本人与读者使用。
安装软件包
pip install pyaml
pip install playwright
python -m playwright install chromium
pip install allure-pytest
pip install pytest
pip install pytest-playwright
下载allure并配置环境
官网下载地址:https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/
开源地址
Gitee:Playwright+Pytest+Allure测试UI自动化框架
注:欢迎大家进入Gitee进行lssues与优化代码,完善自动化框架。
目录
common(公共方法层)
testcase(用例层)
data(数据层)
log(日志层)
reports(报告层)
common公共方法层
该层用于存放用例常调用的函数
action.py(复杂动作封装)
# 点击输入内容
def click_fill(page, local, fills):
page.locator(local).click()
page.locator(local).fill(fills)
attach.py(结果处理动作封装)
import allure
from playwright.sync_api import Page
'''
readAttach:
入参(page, 路径[./log/screenshot/], 文件名[test_playwright])
'''
# 结果截图与Allure提取
def readAttach(page: Page, paths, fileName):
page.screenshot(path=paths + fileName + ".png")
with open(paths + fileName + ".png", "rb") as file:
allure.attach(file.read(), name=fileName, attachment_type=allure.attachment_type.PNG)
read_file.py(yaml文件读取)
import yaml
from setting import DIR_NAME
def read_yaml(file_path):
with open(DIR_NAME + file_path, "r", encoding="utf-8") as f:
return yaml.load(f, Loader=yaml.FullLoader)
if __name__ == "__main__":
print(read_yaml("/data/base.yaml"))
print(read_yaml("/data/base.yaml")["test_pytest"]["goto"])
testcase用例层
该层存放自动化用例并用于执行
conftest.py(fixtures)
import allure
from pytest import Item
def pytest_runtest_call(item: Item):
if item.parent._obj.__doc__:
allure.dynamic.feature(item.parent._obj.__doc__)
if item.function.__doc__:
allure.dynamic.title(item.function.__doc__)
test_基础数据.py
import allure
import pytest
from common.action import click_fill
from common.attach import readAttach
from common.read_file import read_yaml
from playwright.sync_api import Page
@allure.epic("基础数据")
@allure.title("搜索pytest")
def test_pytest(page: Page):
# 读取yaml文件
dbinfo = read_yaml("/data/base.yaml")
# 打开地址
page.goto(dbinfo["test_pytest"]["goto"])
# 搜索框输入内容
click_fill(page, "#kw", dbinfo["test_pytest"]["fills"])
# 点击'百度一下'
page.get_by_role("button", name="百度一下").click()
# 截图结果并保存到log与allure
readAttach(page, dbinfo["test_pytest"]["path"], dbinfo["test_pytest"]["fileName"])
@allure.epic("基础数据")
@allure.title("搜索allure")
def test_allure(page: Page):
dbinfo = read_yaml("/data/base.yaml")
page.goto(dbinfo["test_allure"]["goto"])
click_fill(page, "#kw", dbinfo["test_allure"]["fills"])
page.get_by_role("button", name="百度一下").click()
readAttach(page, dbinfo["test_allure"]["path"], dbinfo["test_allure"]["fileName"])
test_采购.py
import allure
from common.attach import readAttach
from playwright.sync_api import Page
@allure.epic("采购模块")
@allure.title("搜索playwright")
def test_playwright(page: Page):
page.goto("https://www.baidu.com/")
page.locator("#kw").click()
page.locator("#kw").fill("playwright")
page.get_by_role("button", name="百度一下").click()
readAttach(page, "./log/screenshot/", "test_playwright")
Data数据层
该层用于存储业务层数据变量,方便后续维护与调整。如:数据不适用能更快速找到位置更改
注:一个用例配一个yaml
base.yaml
test_pytest:
goto: https://www.baidu.com/
fills: pytest
path: ./log/screenshot/
fileName: test_pytest
test_allure:
goto: https://www.baidu.com/
fills: allure
path: ./log/screenshot/
fileName: test_allure
pytest.ini
[pytest]
# 全集测试
addopts = -vs --alluredir=./reports/tmp --clean-alluredir
# 冒烟测试
;addopts = -sv -m smoke --alluredir ./reports/apiReport --clean-alluredir
testpaths = ./testcase
python_files = test_*.py
python_classes = Test*
python_functions = test_*
markers =
smoke
run.py
import os
import pytest
if __name__ == "__main__":
pytest.main()
os.system("allure generate ./reports/tmp -o ./reports/UIReport --clean")
# 本地启动打开Allure报告HTML
os.system("allure serve ./reports/tmp")
setting.py
import os
DIR_NAME = os.path.dirname(os.path.abspath(__file__))
print(DIR_NAME)
日志层、报告层(运行后自动生成)
附分层意义的解答:
分层的目的用于代码可维护性,可行性,可持续性,简洁性。下面我列举几个例子,大家就会知道分层的意义。
例:四个层级合并在一个用例写,当登录环境地址切换 你需每个用例都调整,分层只需调整Login.py
四个层级合并在一个用例写,当变量数据全部需要调整时,你得找到每个步骤修改数据,分层只需调整test02.yaml
07f8f234-d6ac-4dfd-a512-e9a19c03dc43
本文来自博客园,作者:KrityCat,转载请注明原文链接:https://www.cnblogs.com/KrityCat/p/18093011
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)