当前位置:首页>python>python playwright--pytest-playwright、pytest-base-url插件编写用例

python playwright--pytest-playwright、pytest-base-url插件编写用例

  • 2026-10-11 05:42:50
python playwright--pytest-playwright、pytest-base-url插件编写用例

这篇文章主要介绍了python playwright--pytest-playwright、pytest-base-url插件编写用例,它提供上下文隔离,开箱即用地在多个浏览器配置上运行,它继承了pytest框架,以及支持playwright的一些基础使用,需要的朋友可以参考下

前言

官方的 pytest-playwright 插件可以编写端到端测试。它提供上下文隔离,开箱即用地在多个浏览器配置上运行。它继承了pytest框架,以及支持playwright的一些基础使用。

一、安装插件

pip install pytest-playwright

二、编写用例

安装这个插件后,无需再自定义fixture,直接使用内置的前置Page即可

from playwright.sync_api import Page, expectdef test_login_01(page: Page):    page.goto("http://192.168.64.209:8008/#/login")

说明:直接在用例函数里声明一个Page,格式:def test_login_01(page: Page),即可跳转访问页面

三、运行用例

1、右键运行

2、命令行运行

指定运行某个py用例

pytest testcases\tedt_pyplay.py

运行全部用例

pytest

四、内置fixture

这些固定装置在测试功能中请求时创建,并在测试结束时销毁

Function scope:

context:用于测试的新浏览器上下文
page:用于测试的新浏览器页面

Session scope:
playwright:playwright实例
browser_type:当前浏览器的BrowserType实例
browser:Playwright 启动的浏览器实例
browser_name: 浏览器名称作为字符串
browser_channel: 浏览器通道作为字符串
is_chromium, is_webkit, is_firefox: 相应浏览器类型的布尔值

自定义fixture:
对于browser和context ,使用以下fixture来自定义启动选项

browser_type_launch_args:覆盖browser_type.launch()的启动参数,返回一个字典
browser_context_args:覆盖browser.new_context()的选项,返回一个字典

五、全局配置base_url

1、安装 pytest-base-url

pip install pytest-base-url

2、项目根目录新建pytest.ini文件

3、pytest.ini文件里配置base_url

[pytest]base_url = http://192.168.41.20:4444

4、使用base_url的全局url

from playwright.sync_api import Page, expectdef test_login_01(page: Page,base_url):    page.goto(base_url+"/#/login")

说明:配置后,可直接获取到base_url的,前提是先安装插件,然后在pytest.ini文件里配置,最后在用例里应用即可

到此这篇关于python playwright--pytest-playwright、pytest-base-url插件编写用例的文章就介绍到这了

您可能感兴趣的文章:

使用pytest结合Playwright实现页面元素在两个区域间拖拽功能

使用Playwright+Pytest构建Web UI自动化测试框架完整示例

Python中的自动化测试与质量保障详解

Python pytest 框架通关指南:自动化测试不再难

Java环境搭建Selenium代码自动化测试框架

Fiddler如何抓取手机APP数据包

fiddler抓包小技巧之自动保存抓包数据的实现方法分析【可根据需求过滤】

软件测试之使用Fiddler实现弱网测试

python软件测试Jmeter性能测试JDBC Request(结合数据库)的使用详解

Python+request+unittest实现接口测试框架集成实例

po+selenium+unittest自动化测试项目实战

软件测试中的移动端的埋点测试(干货)

测试入门以及pytest入门

python ui自动化测试

Python测试框架之pytest详解

使用postman传递list集合后台springmvc接收

基于postman实现http接口测试过程解析

pytest fixtures装饰器的使用和如何控制用例的执行顺序

Python测试框架:pytest学习笔记

python单元测试框架pytest的使用示例

Pytest单元测试框架如何实现参数化

今天也要点一键哦❤️❤️

  "赞"、"在看"、

最新文章

随机文章