使用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

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Playwright+Pytest是一种常用的自动化测试框架组合,用于编写和执行Web应用程序的自动化测试。下面是一个简单的示例,展示了如何使用PlaywrightPytest进行自动化测试: 首先,确保已经安装了PlaywrightPytest模块。可以使用以下命令进行安装: ``` pip install playwright pytest ``` 接下来,创建一个pytest测试文件,例如 `test_example.py`,并导入所需的模块: ```python import pytest from playwright.sync_api import Playwright, sync_playwright # 使用fixture装饰器,创建一个Playwright实例 @pytest.fixture(scope="session") def playwright() -> Playwright: with sync_playwright() as playwright: yield playwright # 编写一个基本的测试用例 def test_example(playwright): # 启动一个浏览器实例 browser = playwright.chromium.launch() # 创建一个页面对象 page = browser.new_page() # 导航到目标网页 page.goto("https://www.example.com") # 断言页面标题 assert page.title() == "Example Domain" # 关闭浏览器 browser.close() ``` 在上面的示例中,使用`@pytest.fixture`装饰器创建了一个Playwright实例,作为测试用例的前置条件。然后,使用`playwright`夹具作为参数传递给测试用例函数。在测试用例中,使用Playwright的功能来启动浏览器、创建页面对象、导航到网页,并进行断言验证。 最后,使用以下命令运行测试: ``` pytest test_example.py ``` 这是一个简单的Playwright+Pytest自动化框架的示例,你可以根据实际需求扩展和定制测试用例。希望对你有帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KrityCat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值