pytest-pyppeteer:在pytest中运行pyppeteer
pytest-pyppeteer
pytest-pyppeteer是我写的一个 pytest 插件,支持在 pytest 中运行pyppeteer,起因是为了解决工作中的一个测试需求,现在将其开源并做简单介绍。
背景
为什么不用 selenium?
最根本的原因是 selenium 的配置比较繁琐,最常见的问题是需要保持 webdriver 和浏览器版本的一致性。
pyppeteer 的简单介绍
pyppeteer 是 puppeteer的非官方 python 版本,几乎实现了和其相同的 API,可以非常方便的去操作 Chrome 浏览器。
pyppeteer 的局限性
目前最明显的问题是没有提供跨浏览器的解决方案,最新的 puppeteer 已经提供对 Firefox 的支持,但是 pyppeteer 可能还需要一些时间。
安装
推荐使用pipenv管理虚拟环境,并替换为国内 pip 源。
$ pipenv install pytest-pyppeteer
仅支持 python >= 3.7
快速开始
用下面这个测试用例来说明:断言电影《活着》的豆瓣评分大于 9.0。
from dataclasses import dataclass
from pytest_pyppeteer.models import Browser
@dataclass(init=False)
class Elements:
"""收集所有使用到的页面元素,可以为 XPath 或者 CSS Selector。"""
# 查询输入框
query = "#inp-query"
# 点击搜索
apply = ".inp-btn > input:nth-child(1)"
# 第一条结果
first_result = (
"#root > div > div > div > div > div:nth-child(1) > div.item-root a.cover-link"
)
# 评分
rating = (
"#interest_sectl > div.rating_wrap.clearbox > div.rating_self.clearfix > strong"
)
async def test_lifetimes(pyppeteer: Browser):
page = await pyppeteer.new_page()
await page.goto('https://movie.douban.com/')
await page.type(Elements.query, "活着")
await page.click(Elements.apply)
await page.waitfor(Elements.first_result)
await page.click(Elements.first_result)
await page.waitfor(Elements.rating)
rating = await page.get_value(Elements.rating)
assert float(rating) >= 9.0
执行测试用例,看一下效果:

这里我们无需指定浏览器的路径,pytest-pyppeteer 会在对应平台默认的安装路径下搜寻 Chrome 的可执行文件。
也可以通过 --executable-path命令行选项显示的指定 Chrome 的路径。
或者,在你的conftest.py文件中指定:
@pytest.fixture(scope="session")
def executable_path(executable_path):
if executable_path is None:
return "path/to/Chrome/or/Chromium"
return executable_path
其它支持的命令行选项,包括:
--headless:使用浏览器的无头模式;--args:为浏览器指定其它参数。例如:指定代理服务器:$ pytest --args proxy-server "localhost:5555,direct://" --args proxy-bypass-list "192.0.0.1/8;10.0.0.1/8"
--window-size:指定浏览器的大小,默认是 800*600;--window-size 0 0表示最大化浏览器;
同时操作多个浏览器
用下面这个测试用例来说明:断言书籍《活着》的豆瓣评分高于其电影的评分。
import asyncio
from dataclasses import dataclass
from pytest_pyppeteer.models import Browser, Page
@dataclass(init=False)
class Elements:
"""公共对象库"""
query = "#inp-query"
apply = ".inp-btn > input:nth-child(1)"
@dataclass(init=False)
class BookElements(Elements):
url = "https://book.douban.com/"
first_result = '(//*[@class="item-root"]/a)[1]'
rating = "#interest_sectl > div > div.rating_self.clearfix > strong"
@dataclass(init=False)
class MovieElements(Elements):
url = "https://movie.douban.com/"
first_result = (
"#root > div > div > div > div > div:nth-child(1) > div.item-root a.cover-link"
)
rating = (
"#interest_sectl > div.rating_wrap.clearbox > div.rating_self.clearfix > strong"
)
async def query_rating(pyppeteer: Browser, name: str, elements: Elements):
"""获取电影或者书籍的评分。"""
page: Page = await pyppeteer.new_page()
await page.goto(elements.url)
await page.type(elements.query, name)
await page.click(elements.apply)
await page.waitfor(elements.first_result)
await page.click(elements.first_result)
await page.waitfor(elements.rating)
rating = await page.get_value(elements.rating)
return rating
async def test_multiple_pyppeteer(pyppeteer_factory):
pyppeteer1 = await pyppeteer_factory()
pyppeteer2 = await pyppeteer_factory()
movie_rating, book_rating = await asyncio.gather(
query_rating(pyppeteer1, "活着", MovieElements),
query_rating(pyppeteer2, "活着", BookElements)
)
assert book_rating >= movie_rating
通过pyppeteer_factory 可以获取多个浏览器实例,再利用 python 标准库asyncio可以很方便的执行异步操作,节省时间。
执行测试用例,看一下效果:

Github 仓库
更多功能可以访问:https://github.com/luizyao/pytest-pyppeteer,如果能帮助到你的话,可以给个 star,也欢迎提 issue 和 pr。
pytest 中文文档(v6.1.1)
之前翻译过 pytest v5.1.3 的官方文档并开源,目前计划更新到 v6.1.1 版本。
项目更多进度可以访问:https://github.com/luizyao/pytest-chinese-doc/tree/6.1.1
pytest-pyppeteer:在pytest中运行pyppeteer的更多相关文章
- 不能在Python Console中运行pytest
在Python Console中运行pytest发现报错了 这是为什么?因为Python Console已经是进入python之后的环境,就像在python自带的IDLE中运行pytest pytes ...
- pycharm运行Pytest,有没有将Pytest写入Python代码中的区别
初学pytest. 将pytest写进Python代码中 不同运行方式都可正常运行 =======================**********************========= ...
- pytest——pycharm中右击运行(run)没有问题,在terminal中运行pytest报错:E ModuleNotFoundError: No module named
参考了这个解决办法:https://blog.csdn.net/qq_36829091/article/details/82180866 我的是Windows,linux的和Windows的解决办法有 ...
- pytest(3):pytest运行参数介绍
前言 pytest 带有很多参数,可以使用 pytest --help 来查看帮助文档,下面介绍几种常用的参数: 无参数 读取路径下所有符合规则的文件,类,方法,函数全部执行.使用方法如下: py ...
- 【pytest系列】- pytest测试框架介绍与运行
如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html 前言 目前有两种纯测试的测 ...
- pytest文档2-用例运行规则
用例设计原则 文件名以test_*.py文件和*_test.py 以test_开头的函数 以Test开头的类 以test_开头的方法 所有的包pakege必须要有__init__.py文件 help帮 ...
- pytest系列(四)- pytest+allure+jenkins - 持续集成平台生成allure报告
pytest是什么 pytest是python的一款测试框架,拥有unittest的功能并比它更丰富. allure是什么 有非常多的优秀的测试框架,但却是有非常少优秀的报告工具可以展示非常清楚的用例 ...
- Pytest单元测试框架-Pytest环境安装
unittest是python自带的单元测试框架,它封装好了一些校验返回的结果方法和一些用例执行前的初始化操作,使得单元测试易于开展,因为它的易用性,很多同学也拿它来做功能测试和接口测试,只需简单开发 ...
- Pytest单元测试框架——Pytest+Allure+Jenkins的应用
一.简介 pytest+allure+jenkins进行接口测试.生成测试报告.结合jenkins进行集成. pytest是python的一种单元测试框架,与python自带的unittest测试框架 ...
随机推荐
- 设计模式之Command
由于学习hystrix的使用和原理 所以就学习了command模式https://www.jdon.com/designpatterns/command.htm Command模式是最让我疑惑的一 ...
- 《Java从入门到失业》第四章:类和对象(4.6):类路径
4.6类路径 4.6.1什么是类路径 前面我们讨论过包,知道字节码文件最终都会被放到和包名相匹配的树状结构子目录中.例如上一节的例子: 其实类还有一种存放方式,就是可以归档到一个jar文件中,jar文 ...
- 工具请求接口参数为string类型的JSON字符串时需要加转义字符模拟测试
例如postMan传String类型的json字符串请后台接口时,需要\转义
- Ubuntu18.04安装常用软件指南
安装中文版火狐浏览器 第一步:先卸载:sudo apt-get remove firefox第二步:安装:sudo apt-get install firefox第三步:设置成中文:sudo apt- ...
- MySQL中的临时表到底什么是?
Author:极客小俊 一个专注于web技术的80后 我不用拼过聪明人,我只需要拼过那些懒人 我就一定会超越大部分人! CSDN@极客小俊,原创文章, B站技术分享 B站视频 : Bilibili.c ...
- 2020 CiGA Game Jam活动总结
CiGA Game Jam 总结 今年8月14.15.16号,48小时游戏开发--Game Jam开始了.蠢新第一次参加Game Jam,今年还是线上开展,情绪复杂= = 还有一个坏消息,晓航旅游缺席 ...
- 破晓行动----带你总结JVM的知识大全(二)
JVM运行时内存 + 垃圾回收与算法
- spring aop 源码分析(三) @Scope注解创建代理对象
一.源码环境的搭建: @Component @Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON,proxyMode = ScopedP ...
- Ansys Student 2020R2中Fluent编译UDF简介
使用内建编译器 在Ansys Fluent中编译UDF一般都需要额外安装相应版本的Visual Studio编译器,VS的缺点是体量大,占空间,安装后还需要额外进行相关设置才能正常使用.而新版本的An ...
- 山寨一个Spring的@Component注解
1. 前言 我们在上一篇对Mybatis如何将Mapper接口注入Spring IoC进行了分析,有同学问胖哥这个有什么用,这个作用其实挺大的,比如让你实现一个类似@Controller的注解(或者继 ...